xref: /linux/scripts/kconfig/confdata.c (revision 0c874100108f03401cb3154801d2671bbad40ad4)
1*0c874100SMasahiro Yamada // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
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 
771508fec8SMasahiro Yamada static char depfile_path[PATH_MAX];
781508fec8SMasahiro Yamada static size_t depfile_prefix_len;
791508fec8SMasahiro Yamada 
801508fec8SMasahiro Yamada /* touch depfile for symbol 'name' */
811508fec8SMasahiro Yamada static int conf_touch_dep(const char *name)
821508fec8SMasahiro Yamada {
831508fec8SMasahiro Yamada 	int fd, ret;
841508fec8SMasahiro Yamada 	const char *s;
851508fec8SMasahiro Yamada 	char *d, c;
861508fec8SMasahiro Yamada 
871508fec8SMasahiro Yamada 	/* check overflow: prefix + name + ".h" + '\0' must fit in buffer. */
881508fec8SMasahiro Yamada 	if (depfile_prefix_len + strlen(name) + 3 > sizeof(depfile_path))
891508fec8SMasahiro Yamada 		return -1;
901508fec8SMasahiro Yamada 
911508fec8SMasahiro Yamada 	d = depfile_path + depfile_prefix_len;
921508fec8SMasahiro Yamada 	s = name;
931508fec8SMasahiro Yamada 
941508fec8SMasahiro Yamada 	while ((c = *s++))
951508fec8SMasahiro Yamada 		*d++ = (c == '_') ? '/' : tolower(c);
961508fec8SMasahiro Yamada 	strcpy(d, ".h");
971508fec8SMasahiro Yamada 
981508fec8SMasahiro Yamada 	/* Assume directory path already exists. */
991508fec8SMasahiro Yamada 	fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1001508fec8SMasahiro Yamada 	if (fd == -1) {
1011508fec8SMasahiro Yamada 		if (errno != ENOENT)
1021508fec8SMasahiro Yamada 			return -1;
1031508fec8SMasahiro Yamada 
1041508fec8SMasahiro Yamada 		ret = make_parent_dir(depfile_path);
1051508fec8SMasahiro Yamada 		if (ret)
1061508fec8SMasahiro Yamada 			return ret;
1071508fec8SMasahiro Yamada 
1081508fec8SMasahiro Yamada 		/* Try it again. */
1091508fec8SMasahiro Yamada 		fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1101508fec8SMasahiro Yamada 		if (fd == -1)
1111508fec8SMasahiro Yamada 			return -1;
1121508fec8SMasahiro Yamada 	}
1131508fec8SMasahiro Yamada 	close(fd);
1141508fec8SMasahiro Yamada 
1151508fec8SMasahiro Yamada 	return 0;
1161508fec8SMasahiro Yamada }
1171508fec8SMasahiro Yamada 
118ad8d40cdSMichal Marek struct conf_printer {
119ad8d40cdSMichal Marek 	void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
120ad8d40cdSMichal Marek 	void (*print_comment)(FILE *, const char *, void *);
121ad8d40cdSMichal Marek };
122ad8d40cdSMichal Marek 
123c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
124c1a0f5e3SRoman Zippel 	__attribute__ ((format (printf, 1, 2)));
125c1a0f5e3SRoman Zippel 
12642368c37SMichal Marek static void conf_message(const char *fmt, ...)
12742368c37SMichal Marek 	__attribute__ ((format (printf, 1, 2)));
12842368c37SMichal Marek 
129c1a0f5e3SRoman Zippel static const char *conf_filename;
13084dd95d4SMasahiro Yamada static int conf_lineno, conf_warnings;
131c1a0f5e3SRoman Zippel 
132104daea1SMasahiro Yamada const char conf_defname[] = "arch/$(ARCH)/defconfig";
1331da177e4SLinus Torvalds 
134c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
135c1a0f5e3SRoman Zippel {
136c1a0f5e3SRoman Zippel 	va_list ap;
137c1a0f5e3SRoman Zippel 	va_start(ap, fmt);
138c1a0f5e3SRoman Zippel 	fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
139c1a0f5e3SRoman Zippel 	vfprintf(stderr, fmt, ap);
140c1a0f5e3SRoman Zippel 	fprintf(stderr, "\n");
141c1a0f5e3SRoman Zippel 	va_end(ap);
142c1a0f5e3SRoman Zippel 	conf_warnings++;
143c1a0f5e3SRoman Zippel }
144c1a0f5e3SRoman Zippel 
1455accd7f3SMasahiro Yamada static void conf_default_message_callback(const char *s)
14642368c37SMichal Marek {
14742368c37SMichal Marek 	printf("#\n# ");
1485accd7f3SMasahiro Yamada 	printf("%s", s);
14942368c37SMichal Marek 	printf("\n#\n");
15042368c37SMichal Marek }
15142368c37SMichal Marek 
1525accd7f3SMasahiro Yamada static void (*conf_message_callback)(const char *s) =
15342368c37SMichal Marek 	conf_default_message_callback;
1545accd7f3SMasahiro Yamada void conf_set_message_callback(void (*fn)(const char *s))
15542368c37SMichal Marek {
15642368c37SMichal Marek 	conf_message_callback = fn;
15742368c37SMichal Marek }
15842368c37SMichal Marek 
15942368c37SMichal Marek static void conf_message(const char *fmt, ...)
16042368c37SMichal Marek {
16142368c37SMichal Marek 	va_list ap;
1625accd7f3SMasahiro Yamada 	char buf[4096];
1635accd7f3SMasahiro Yamada 
1645accd7f3SMasahiro Yamada 	if (!conf_message_callback)
1655accd7f3SMasahiro Yamada 		return;
16642368c37SMichal Marek 
16742368c37SMichal Marek 	va_start(ap, fmt);
1685accd7f3SMasahiro Yamada 
1695accd7f3SMasahiro Yamada 	vsnprintf(buf, sizeof(buf), fmt, ap);
1705accd7f3SMasahiro Yamada 	conf_message_callback(buf);
171b6a2ab2cSColin Ian King 	va_end(ap);
17242368c37SMichal Marek }
17342368c37SMichal Marek 
17414cdd3c4SRoman Zippel const char *conf_get_configname(void)
17514cdd3c4SRoman Zippel {
17614cdd3c4SRoman Zippel 	char *name = getenv("KCONFIG_CONFIG");
17714cdd3c4SRoman Zippel 
17814cdd3c4SRoman Zippel 	return name ? name : ".config";
17914cdd3c4SRoman Zippel }
18014cdd3c4SRoman Zippel 
18112122f62SMarkus Heidelberg const char *conf_get_autoconfig_name(void)
18212122f62SMarkus Heidelberg {
18312122f62SMarkus Heidelberg 	char *name = getenv("KCONFIG_AUTOCONFIG");
18412122f62SMarkus Heidelberg 
18512122f62SMarkus Heidelberg 	return name ? name : "include/config/auto.conf";
18612122f62SMarkus Heidelberg }
18712122f62SMarkus Heidelberg 
1881da177e4SLinus Torvalds char *conf_get_default_confname(void)
1891da177e4SLinus Torvalds {
1901da177e4SLinus Torvalds 	static char fullname[PATH_MAX+1];
1911da177e4SLinus Torvalds 	char *env, *name;
1921da177e4SLinus Torvalds 
193104daea1SMasahiro Yamada 	name = expand_string(conf_defname);
1941da177e4SLinus Torvalds 	env = getenv(SRCTREE);
1951da177e4SLinus Torvalds 	if (env) {
1961da177e4SLinus Torvalds 		sprintf(fullname, "%s/%s", env, name);
1970608182aSMasahiro Yamada 		if (is_present(fullname))
1981da177e4SLinus Torvalds 			return fullname;
1991da177e4SLinus Torvalds 	}
2001da177e4SLinus Torvalds 	return name;
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2039c900a9cSSam Ravnborg static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
2049c900a9cSSam Ravnborg {
2059c900a9cSSam Ravnborg 	char *p2;
2069c900a9cSSam Ravnborg 
2079c900a9cSSam Ravnborg 	switch (sym->type) {
2089c900a9cSSam Ravnborg 	case S_TRISTATE:
2099c900a9cSSam Ravnborg 		if (p[0] == 'm') {
2109c900a9cSSam Ravnborg 			sym->def[def].tri = mod;
2119c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2129c900a9cSSam Ravnborg 			break;
2139c900a9cSSam Ravnborg 		}
214d8fc3200SArnaud Lacombe 		/* fall through */
2159c900a9cSSam Ravnborg 	case S_BOOLEAN:
2169c900a9cSSam Ravnborg 		if (p[0] == 'y') {
2179c900a9cSSam Ravnborg 			sym->def[def].tri = yes;
2189c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2199c900a9cSSam Ravnborg 			break;
2209c900a9cSSam Ravnborg 		}
2219c900a9cSSam Ravnborg 		if (p[0] == 'n') {
2229c900a9cSSam Ravnborg 			sym->def[def].tri = no;
2239c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2249c900a9cSSam Ravnborg 			break;
2259c900a9cSSam Ravnborg 		}
22604b19b77SYann E. MORIN 		if (def != S_DEF_AUTO)
22704b19b77SYann E. MORIN 			conf_warning("symbol value '%s' invalid for %s",
22804b19b77SYann E. MORIN 				     p, sym->name);
22975f1468bSArnaud Lacombe 		return 1;
2309c900a9cSSam Ravnborg 	case S_STRING:
2319c900a9cSSam Ravnborg 		if (*p++ != '"')
2329c900a9cSSam Ravnborg 			break;
2339c900a9cSSam Ravnborg 		for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
2349c900a9cSSam Ravnborg 			if (*p2 == '"') {
2359c900a9cSSam Ravnborg 				*p2 = 0;
2369c900a9cSSam Ravnborg 				break;
2379c900a9cSSam Ravnborg 			}
2389c900a9cSSam Ravnborg 			memmove(p2, p2 + 1, strlen(p2));
2399c900a9cSSam Ravnborg 		}
2409c900a9cSSam Ravnborg 		if (!p2) {
24104b19b77SYann E. MORIN 			if (def != S_DEF_AUTO)
2429c900a9cSSam Ravnborg 				conf_warning("invalid string found");
2439c900a9cSSam Ravnborg 			return 1;
2449c900a9cSSam Ravnborg 		}
245d8fc3200SArnaud Lacombe 		/* fall through */
2469c900a9cSSam Ravnborg 	case S_INT:
2479c900a9cSSam Ravnborg 	case S_HEX:
2489c900a9cSSam Ravnborg 		if (sym_string_valid(sym, p)) {
249cd81fc82SMasahiro Yamada 			sym->def[def].val = xstrdup(p);
2509c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2519c900a9cSSam Ravnborg 		} else {
25204b19b77SYann E. MORIN 			if (def != S_DEF_AUTO)
25304b19b77SYann E. MORIN 				conf_warning("symbol value '%s' invalid for %s",
25404b19b77SYann E. MORIN 					     p, sym->name);
2559c900a9cSSam Ravnborg 			return 1;
2569c900a9cSSam Ravnborg 		}
2579c900a9cSSam Ravnborg 		break;
2589c900a9cSSam Ravnborg 	default:
2599c900a9cSSam Ravnborg 		;
2609c900a9cSSam Ravnborg 	}
2619c900a9cSSam Ravnborg 	return 0;
2629c900a9cSSam Ravnborg }
2639c900a9cSSam Ravnborg 
2641a7a8c6fSCody Schafer #define LINE_GROWTH 16
2651a7a8c6fSCody Schafer static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
2661a7a8c6fSCody Schafer {
2671a7a8c6fSCody Schafer 	char *nline;
2681a7a8c6fSCody Schafer 	size_t new_size = slen + 1;
2691a7a8c6fSCody Schafer 	if (new_size > *n) {
2701a7a8c6fSCody Schafer 		new_size += LINE_GROWTH - 1;
2711a7a8c6fSCody Schafer 		new_size *= 2;
272d717f24dSMasahiro Yamada 		nline = xrealloc(*lineptr, new_size);
2731a7a8c6fSCody Schafer 		if (!nline)
2741a7a8c6fSCody Schafer 			return -1;
2751a7a8c6fSCody Schafer 
2761a7a8c6fSCody Schafer 		*lineptr = nline;
2771a7a8c6fSCody Schafer 		*n = new_size;
2781a7a8c6fSCody Schafer 	}
2791a7a8c6fSCody Schafer 
2801a7a8c6fSCody Schafer 	(*lineptr)[slen] = c;
2811a7a8c6fSCody Schafer 
2821a7a8c6fSCody Schafer 	return 0;
2831a7a8c6fSCody Schafer }
2841a7a8c6fSCody Schafer 
2851a7a8c6fSCody Schafer static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
2861a7a8c6fSCody Schafer {
2871a7a8c6fSCody Schafer 	char *line = *lineptr;
2881a7a8c6fSCody Schafer 	size_t slen = 0;
2891a7a8c6fSCody Schafer 
2901a7a8c6fSCody Schafer 	for (;;) {
2911a7a8c6fSCody Schafer 		int c = getc(stream);
2921a7a8c6fSCody Schafer 
2931a7a8c6fSCody Schafer 		switch (c) {
2941a7a8c6fSCody Schafer 		case '\n':
2951a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
2961a7a8c6fSCody Schafer 				goto e_out;
2971a7a8c6fSCody Schafer 			slen++;
2981a7a8c6fSCody Schafer 			/* fall through */
2991a7a8c6fSCody Schafer 		case EOF:
3001a7a8c6fSCody Schafer 			if (add_byte('\0', &line, slen, n) < 0)
3011a7a8c6fSCody Schafer 				goto e_out;
3021a7a8c6fSCody Schafer 			*lineptr = line;
3031a7a8c6fSCody Schafer 			if (slen == 0)
3041a7a8c6fSCody Schafer 				return -1;
3051a7a8c6fSCody Schafer 			return slen;
3061a7a8c6fSCody Schafer 		default:
3071a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
3081a7a8c6fSCody Schafer 				goto e_out;
3091a7a8c6fSCody Schafer 			slen++;
3101a7a8c6fSCody Schafer 		}
3111a7a8c6fSCody Schafer 	}
3121a7a8c6fSCody Schafer 
3131a7a8c6fSCody Schafer e_out:
3141a7a8c6fSCody Schafer 	line[slen-1] = '\0';
3151a7a8c6fSCody Schafer 	*lineptr = line;
3161a7a8c6fSCody Schafer 	return -1;
3171a7a8c6fSCody Schafer }
3181a7a8c6fSCody Schafer 
319669bfad9SRoman Zippel int conf_read_simple(const char *name, int def)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds 	FILE *in = NULL;
3221a7a8c6fSCody Schafer 	char   *line = NULL;
3231a7a8c6fSCody Schafer 	size_t  line_asize = 0;
3241da177e4SLinus Torvalds 	char *p, *p2;
3251da177e4SLinus Torvalds 	struct symbol *sym;
326669bfad9SRoman Zippel 	int i, def_flags;
3271da177e4SLinus Torvalds 
3281da177e4SLinus Torvalds 	if (name) {
3291da177e4SLinus Torvalds 		in = zconf_fopen(name);
3301da177e4SLinus Torvalds 	} else {
331face4374SRoman Zippel 		struct property *prop;
332face4374SRoman Zippel 
33314cdd3c4SRoman Zippel 		name = conf_get_configname();
334ddc97cacSRoman Zippel 		in = zconf_fopen(name);
335ddc97cacSRoman Zippel 		if (in)
336ddc97cacSRoman Zippel 			goto load;
337bfc10001SKarsten Wiese 		sym_add_change_count(1);
3386b87b70cSAl Viro 		if (!sym_defconfig_list)
339face4374SRoman Zippel 			return 1;
340face4374SRoman Zippel 
341face4374SRoman Zippel 		for_all_defaults(sym_defconfig_list, prop) {
342face4374SRoman Zippel 			if (expr_calc_value(prop->visible.expr) == no ||
343face4374SRoman Zippel 			    prop->expr->type != E_SYMBOL)
344face4374SRoman Zippel 				continue;
345104daea1SMasahiro Yamada 			sym_calc_value(prop->expr->left.sym);
346104daea1SMasahiro Yamada 			name = sym_get_string_value(prop->expr->left.sym);
3471da177e4SLinus Torvalds 			in = zconf_fopen(name);
3481da177e4SLinus Torvalds 			if (in) {
349694c49a7SSam Ravnborg 				conf_message("using defaults found in %s",
35042368c37SMichal Marek 					 name);
351ddc97cacSRoman Zippel 				goto load;
3521da177e4SLinus Torvalds 			}
3531da177e4SLinus Torvalds 		}
3541da177e4SLinus Torvalds 	}
3551da177e4SLinus Torvalds 	if (!in)
3561da177e4SLinus Torvalds 		return 1;
3571da177e4SLinus Torvalds 
358ddc97cacSRoman Zippel load:
359c1a0f5e3SRoman Zippel 	conf_filename = name;
360c1a0f5e3SRoman Zippel 	conf_lineno = 0;
361c1a0f5e3SRoman Zippel 	conf_warnings = 0;
362c1a0f5e3SRoman Zippel 
363669bfad9SRoman Zippel 	def_flags = SYMBOL_DEF << def;
3641da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
365669bfad9SRoman Zippel 		sym->flags |= SYMBOL_CHANGED;
366669bfad9SRoman Zippel 		sym->flags &= ~(def_flags|SYMBOL_VALID);
367490f1617SYann E. MORIN 		if (sym_is_choice(sym))
368490f1617SYann E. MORIN 			sym->flags |= def_flags;
3691da177e4SLinus Torvalds 		switch (sym->type) {
3701da177e4SLinus Torvalds 		case S_INT:
3711da177e4SLinus Torvalds 		case S_HEX:
3721da177e4SLinus Torvalds 		case S_STRING:
373669bfad9SRoman Zippel 			if (sym->def[def].val)
374669bfad9SRoman Zippel 				free(sym->def[def].val);
375d8fc3200SArnaud Lacombe 			/* fall through */
3761da177e4SLinus Torvalds 		default:
377669bfad9SRoman Zippel 			sym->def[def].val = NULL;
378669bfad9SRoman Zippel 			sym->def[def].tri = no;
3791da177e4SLinus Torvalds 		}
3801da177e4SLinus Torvalds 	}
3811da177e4SLinus Torvalds 
3821a7a8c6fSCody Schafer 	while (compat_getline(&line, &line_asize, in) != -1) {
383c1a0f5e3SRoman Zippel 		conf_lineno++;
3841da177e4SLinus Torvalds 		sym = NULL;
3858baefd30SArnaud Lacombe 		if (line[0] == '#') {
386ffb5957bSArnaud Lacombe 			if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
3871da177e4SLinus Torvalds 				continue;
388ffb5957bSArnaud Lacombe 			p = strchr(line + 2 + strlen(CONFIG_), ' ');
3891da177e4SLinus Torvalds 			if (!p)
3901da177e4SLinus Torvalds 				continue;
3911da177e4SLinus Torvalds 			*p++ = 0;
3921da177e4SLinus Torvalds 			if (strncmp(p, "is not set", 10))
3931da177e4SLinus Torvalds 				continue;
394669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
395ffb5957bSArnaud Lacombe 				sym = sym_find(line + 2 + strlen(CONFIG_));
396661b0680Szippel@linux-m68k.org 				if (!sym) {
397661b0680Szippel@linux-m68k.org 					sym_add_change_count(1);
39875889e9bSMasahiro Yamada 					continue;
399661b0680Szippel@linux-m68k.org 				}
400669bfad9SRoman Zippel 			} else {
401ffb5957bSArnaud Lacombe 				sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
402669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
403669bfad9SRoman Zippel 					sym->type = S_BOOLEAN;
404669bfad9SRoman Zippel 			}
405669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
406d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
4071da177e4SLinus Torvalds 			}
4081da177e4SLinus Torvalds 			switch (sym->type) {
4091da177e4SLinus Torvalds 			case S_BOOLEAN:
4101da177e4SLinus Torvalds 			case S_TRISTATE:
411669bfad9SRoman Zippel 				sym->def[def].tri = no;
412669bfad9SRoman Zippel 				sym->flags |= def_flags;
4131da177e4SLinus Torvalds 				break;
4141da177e4SLinus Torvalds 			default:
4151da177e4SLinus Torvalds 				;
4161da177e4SLinus Torvalds 			}
417ffb5957bSArnaud Lacombe 		} else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
418ffb5957bSArnaud Lacombe 			p = strchr(line + strlen(CONFIG_), '=');
4191da177e4SLinus Torvalds 			if (!p)
4201da177e4SLinus Torvalds 				continue;
4211da177e4SLinus Torvalds 			*p++ = 0;
4221da177e4SLinus Torvalds 			p2 = strchr(p, '\n');
423d3660a8cSMatthew Wilcox 			if (p2) {
424d3660a8cSMatthew Wilcox 				*p2-- = 0;
425d3660a8cSMatthew Wilcox 				if (*p2 == '\r')
4261da177e4SLinus Torvalds 					*p2 = 0;
427d3660a8cSMatthew Wilcox 			}
4282aabbed6SMasahiro Yamada 
429ffb5957bSArnaud Lacombe 			sym = sym_find(line + strlen(CONFIG_));
430661b0680Szippel@linux-m68k.org 			if (!sym) {
4312aabbed6SMasahiro Yamada 				if (def == S_DEF_AUTO)
4322aabbed6SMasahiro Yamada 					/*
4332aabbed6SMasahiro Yamada 					 * Reading from include/config/auto.conf
4342aabbed6SMasahiro Yamada 					 * If CONFIG_FOO previously existed in
4352aabbed6SMasahiro Yamada 					 * auto.conf but it is missing now,
4362aabbed6SMasahiro Yamada 					 * include/config/foo.h must be touched.
4372aabbed6SMasahiro Yamada 					 */
4382aabbed6SMasahiro Yamada 					conf_touch_dep(line + strlen(CONFIG_));
4392aabbed6SMasahiro Yamada 				else
440661b0680Szippel@linux-m68k.org 					sym_add_change_count(1);
44175889e9bSMasahiro Yamada 				continue;
442661b0680Szippel@linux-m68k.org 			}
4432aabbed6SMasahiro Yamada 
444669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
445d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
4461da177e4SLinus Torvalds 			}
4479c900a9cSSam Ravnborg 			if (conf_set_sym_val(sym, def, def_flags, p))
448c1a0f5e3SRoman Zippel 				continue;
4498baefd30SArnaud Lacombe 		} else {
4508baefd30SArnaud Lacombe 			if (line[0] != '\r' && line[0] != '\n')
451a4663911SPaul Bolle 				conf_warning("unexpected data: %.*s",
452a4663911SPaul Bolle 					     (int)strcspn(line, "\r\n"), line);
453a4663911SPaul Bolle 
4541da177e4SLinus Torvalds 			continue;
4551da177e4SLinus Torvalds 		}
45675889e9bSMasahiro Yamada 
4571da177e4SLinus Torvalds 		if (sym && sym_is_choice_value(sym)) {
4581da177e4SLinus Torvalds 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
459669bfad9SRoman Zippel 			switch (sym->def[def].tri) {
4601da177e4SLinus Torvalds 			case no:
4611da177e4SLinus Torvalds 				break;
4621da177e4SLinus Torvalds 			case mod:
463669bfad9SRoman Zippel 				if (cs->def[def].tri == yes) {
464c1a0f5e3SRoman Zippel 					conf_warning("%s creates inconsistent choice state", sym->name);
465490f1617SYann E. MORIN 					cs->flags &= ~def_flags;
466c1a0f5e3SRoman Zippel 				}
4671da177e4SLinus Torvalds 				break;
4681da177e4SLinus Torvalds 			case yes:
469d84876f9SJan Engelhardt 				if (cs->def[def].tri != no)
470d84876f9SJan Engelhardt 					conf_warning("override: %s changes choice state", sym->name);
471669bfad9SRoman Zippel 				cs->def[def].val = sym;
4721da177e4SLinus Torvalds 				break;
4731da177e4SLinus Torvalds 			}
474d6ee3576SSam Ravnborg 			cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
4751da177e4SLinus Torvalds 		}
4761da177e4SLinus Torvalds 	}
4771a7a8c6fSCody Schafer 	free(line);
4781da177e4SLinus Torvalds 	fclose(in);
47990389160SRoman Zippel 	return 0;
48090389160SRoman Zippel }
48190389160SRoman Zippel 
48290389160SRoman Zippel int conf_read(const char *name)
48390389160SRoman Zippel {
4845d09598dSArnaud Lacombe 	struct symbol *sym;
48584dd95d4SMasahiro Yamada 	int conf_unsaved = 0;
4865d09598dSArnaud Lacombe 	int i;
48790389160SRoman Zippel 
488bfc10001SKarsten Wiese 	sym_set_change_count(0);
489ddc97cacSRoman Zippel 
4906b87b70cSAl Viro 	if (conf_read_simple(name, S_DEF_USER)) {
4916b87b70cSAl Viro 		sym_calc_value(modules_sym);
49290389160SRoman Zippel 		return 1;
4936b87b70cSAl Viro 	}
4946b87b70cSAl Viro 
4956b87b70cSAl Viro 	sym_calc_value(modules_sym);
49690389160SRoman Zippel 
4971da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
4981da177e4SLinus Torvalds 		sym_calc_value(sym);
499693359f7SDirk Gouders 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
5005d09598dSArnaud Lacombe 			continue;
501c1a0f5e3SRoman Zippel 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
502c1a0f5e3SRoman Zippel 			/* check that calculated value agrees with saved value */
503c1a0f5e3SRoman Zippel 			switch (sym->type) {
504c1a0f5e3SRoman Zippel 			case S_BOOLEAN:
505c1a0f5e3SRoman Zippel 			case S_TRISTATE:
5060c1822e6SRoman Zippel 				if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
507c1a0f5e3SRoman Zippel 					break;
508c1a0f5e3SRoman Zippel 				if (!sym_is_choice(sym))
5095d09598dSArnaud Lacombe 					continue;
510d8fc3200SArnaud Lacombe 				/* fall through */
511c1a0f5e3SRoman Zippel 			default:
5120c1822e6SRoman Zippel 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
5135d09598dSArnaud Lacombe 					continue;
514c1a0f5e3SRoman Zippel 				break;
515c1a0f5e3SRoman Zippel 			}
516c1a0f5e3SRoman Zippel 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
517c1a0f5e3SRoman Zippel 			/* no previous value and not saved */
5185d09598dSArnaud Lacombe 			continue;
519c1a0f5e3SRoman Zippel 		conf_unsaved++;
520c1a0f5e3SRoman Zippel 		/* maybe print value in verbose mode... */
5211da177e4SLinus Torvalds 	}
5221da177e4SLinus Torvalds 
523d8982ba1SRoman Zippel 	for_all_symbols(i, sym) {
524d8982ba1SRoman Zippel 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
525d8982ba1SRoman Zippel 			/* Reset values of generates values, so they'll appear
526d8982ba1SRoman Zippel 			 * as new, if they should become visible, but that
527d8982ba1SRoman Zippel 			 * doesn't quite work if the Kconfig and the saved
528d8982ba1SRoman Zippel 			 * configuration disagree.
529d8982ba1SRoman Zippel 			 */
530d8982ba1SRoman Zippel 			if (sym->visible == no && !conf_unsaved)
531d8982ba1SRoman Zippel 				sym->flags &= ~SYMBOL_DEF_USER;
532d8982ba1SRoman Zippel 			switch (sym->type) {
533d8982ba1SRoman Zippel 			case S_STRING:
534d8982ba1SRoman Zippel 			case S_INT:
535d8982ba1SRoman Zippel 			case S_HEX:
536d8982ba1SRoman Zippel 				/* Reset a string value if it's out of range */
537d8982ba1SRoman Zippel 				if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
538d8982ba1SRoman Zippel 					break;
539d8982ba1SRoman Zippel 				sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
540d8982ba1SRoman Zippel 				conf_unsaved++;
541d8982ba1SRoman Zippel 				break;
542d8982ba1SRoman Zippel 			default:
543d8982ba1SRoman Zippel 				break;
544d8982ba1SRoman Zippel 			}
545d8982ba1SRoman Zippel 		}
546d8982ba1SRoman Zippel 	}
547d8982ba1SRoman Zippel 
548bfc10001SKarsten Wiese 	sym_add_change_count(conf_warnings || conf_unsaved);
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	return 0;
5511da177e4SLinus Torvalds }
5521da177e4SLinus Torvalds 
553e54e692bSArnaud Lacombe /*
554e54e692bSArnaud Lacombe  * Kconfig configuration printer
555e54e692bSArnaud Lacombe  *
556e54e692bSArnaud Lacombe  * This printer is used when generating the resulting configuration after
557e54e692bSArnaud Lacombe  * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
558e54e692bSArnaud Lacombe  * passing a non-NULL argument to the printer.
559e54e692bSArnaud Lacombe  *
560e54e692bSArnaud Lacombe  */
561e54e692bSArnaud Lacombe static void
562e54e692bSArnaud Lacombe kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
56349192f26SSam Ravnborg {
56449192f26SSam Ravnborg 
5650dce6310SArnaud Lacombe 	switch (sym->type) {
56649192f26SSam Ravnborg 	case S_BOOLEAN:
56749192f26SSam Ravnborg 	case S_TRISTATE:
568e54e692bSArnaud Lacombe 		if (*value == 'n') {
569e54e692bSArnaud Lacombe 			bool skip_unset = (arg != NULL);
570e54e692bSArnaud Lacombe 
571e54e692bSArnaud Lacombe 			if (!skip_unset)
572e54e692bSArnaud Lacombe 				fprintf(fp, "# %s%s is not set\n",
573ffb5957bSArnaud Lacombe 				    CONFIG_, sym->name);
574e54e692bSArnaud Lacombe 			return;
57549192f26SSam Ravnborg 		}
57649192f26SSam Ravnborg 		break;
577e54e692bSArnaud Lacombe 	default:
57849192f26SSam Ravnborg 		break;
579e54e692bSArnaud Lacombe 	}
580e54e692bSArnaud Lacombe 
581e54e692bSArnaud Lacombe 	fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
582e54e692bSArnaud Lacombe }
583e54e692bSArnaud Lacombe 
584e54e692bSArnaud Lacombe static void
585e54e692bSArnaud Lacombe kconfig_print_comment(FILE *fp, const char *value, void *arg)
586e54e692bSArnaud Lacombe {
587e54e692bSArnaud Lacombe 	const char *p = value;
588e54e692bSArnaud Lacombe 	size_t l;
589e54e692bSArnaud Lacombe 
590e54e692bSArnaud Lacombe 	for (;;) {
591e54e692bSArnaud Lacombe 		l = strcspn(p, "\n");
592e54e692bSArnaud Lacombe 		fprintf(fp, "#");
593e54e692bSArnaud Lacombe 		if (l) {
594e54e692bSArnaud Lacombe 			fprintf(fp, " ");
59570cc01e7SPeter Foley 			xfwrite(p, l, 1, fp);
596e54e692bSArnaud Lacombe 			p += l;
597e54e692bSArnaud Lacombe 		}
598e54e692bSArnaud Lacombe 		fprintf(fp, "\n");
599e54e692bSArnaud Lacombe 		if (*p++ == '\0')
60049192f26SSam Ravnborg 			break;
601e54e692bSArnaud Lacombe 	}
602e54e692bSArnaud Lacombe }
603e54e692bSArnaud Lacombe 
604e54e692bSArnaud Lacombe static struct conf_printer kconfig_printer_cb =
605e54e692bSArnaud Lacombe {
606e54e692bSArnaud Lacombe 	.print_symbol = kconfig_print_symbol,
607e54e692bSArnaud Lacombe 	.print_comment = kconfig_print_comment,
608e54e692bSArnaud Lacombe };
609e54e692bSArnaud Lacombe 
610e54e692bSArnaud Lacombe /*
611e54e692bSArnaud Lacombe  * Header printer
612e54e692bSArnaud Lacombe  *
613e54e692bSArnaud Lacombe  * This printer is used when generating the `include/generated/autoconf.h' file.
614e54e692bSArnaud Lacombe  */
615e54e692bSArnaud Lacombe static void
616e54e692bSArnaud Lacombe header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
617e54e692bSArnaud Lacombe {
618e54e692bSArnaud Lacombe 
619e54e692bSArnaud Lacombe 	switch (sym->type) {
620e54e692bSArnaud Lacombe 	case S_BOOLEAN:
621eb4cf5a6SArnaud Lacombe 	case S_TRISTATE: {
622eb4cf5a6SArnaud Lacombe 		const char *suffix = "";
623eb4cf5a6SArnaud Lacombe 
624e54e692bSArnaud Lacombe 		switch (*value) {
625e54e692bSArnaud Lacombe 		case 'n':
6262a11c8eaSMichal Marek 			break;
627e54e692bSArnaud Lacombe 		case 'm':
628e54e692bSArnaud Lacombe 			suffix = "_MODULE";
629eb4cf5a6SArnaud Lacombe 			/* fall through */
630e54e692bSArnaud Lacombe 		default:
6312a11c8eaSMichal Marek 			fprintf(fp, "#define %s%s%s 1\n",
6322a11c8eaSMichal Marek 			    CONFIG_, sym->name, suffix);
633e54e692bSArnaud Lacombe 		}
634eb4cf5a6SArnaud Lacombe 		break;
635eb4cf5a6SArnaud Lacombe 	}
636eb4cf5a6SArnaud Lacombe 	case S_HEX: {
637eb4cf5a6SArnaud Lacombe 		const char *prefix = "";
638eb4cf5a6SArnaud Lacombe 
639eb4cf5a6SArnaud Lacombe 		if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
640eb4cf5a6SArnaud Lacombe 			prefix = "0x";
641eb4cf5a6SArnaud Lacombe 		fprintf(fp, "#define %s%s %s%s\n",
642eb4cf5a6SArnaud Lacombe 		    CONFIG_, sym->name, prefix, value);
643eb4cf5a6SArnaud Lacombe 		break;
644eb4cf5a6SArnaud Lacombe 	}
645eb4cf5a6SArnaud Lacombe 	case S_STRING:
646eb4cf5a6SArnaud Lacombe 	case S_INT:
647eb4cf5a6SArnaud Lacombe 		fprintf(fp, "#define %s%s %s\n",
648eb4cf5a6SArnaud Lacombe 		    CONFIG_, sym->name, value);
649e54e692bSArnaud Lacombe 		break;
650e54e692bSArnaud Lacombe 	default:
651e54e692bSArnaud Lacombe 		break;
652e54e692bSArnaud Lacombe 	}
653e54e692bSArnaud Lacombe 
654e54e692bSArnaud Lacombe }
655e54e692bSArnaud Lacombe 
656e54e692bSArnaud Lacombe static void
657e54e692bSArnaud Lacombe header_print_comment(FILE *fp, const char *value, void *arg)
658e54e692bSArnaud Lacombe {
659e54e692bSArnaud Lacombe 	const char *p = value;
660e54e692bSArnaud Lacombe 	size_t l;
661e54e692bSArnaud Lacombe 
662e54e692bSArnaud Lacombe 	fprintf(fp, "/*\n");
663e54e692bSArnaud Lacombe 	for (;;) {
664e54e692bSArnaud Lacombe 		l = strcspn(p, "\n");
665e54e692bSArnaud Lacombe 		fprintf(fp, " *");
666e54e692bSArnaud Lacombe 		if (l) {
667e54e692bSArnaud Lacombe 			fprintf(fp, " ");
66870cc01e7SPeter Foley 			xfwrite(p, l, 1, fp);
669e54e692bSArnaud Lacombe 			p += l;
670e54e692bSArnaud Lacombe 		}
671e54e692bSArnaud Lacombe 		fprintf(fp, "\n");
672e54e692bSArnaud Lacombe 		if (*p++ == '\0')
673e54e692bSArnaud Lacombe 			break;
674e54e692bSArnaud Lacombe 	}
675e54e692bSArnaud Lacombe 	fprintf(fp, " */\n");
676e54e692bSArnaud Lacombe }
677e54e692bSArnaud Lacombe 
678e54e692bSArnaud Lacombe static struct conf_printer header_printer_cb =
679e54e692bSArnaud Lacombe {
680e54e692bSArnaud Lacombe 	.print_symbol = header_print_symbol,
681e54e692bSArnaud Lacombe 	.print_comment = header_print_comment,
682e54e692bSArnaud Lacombe };
683e54e692bSArnaud Lacombe 
684e54e692bSArnaud Lacombe /*
685e54e692bSArnaud Lacombe  * Tristate printer
686e54e692bSArnaud Lacombe  *
687e54e692bSArnaud Lacombe  * This printer is used when generating the `include/config/tristate.conf' file.
688e54e692bSArnaud Lacombe  */
689e54e692bSArnaud Lacombe static void
690e54e692bSArnaud Lacombe tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
691e54e692bSArnaud Lacombe {
692e54e692bSArnaud Lacombe 
693e54e692bSArnaud Lacombe 	if (sym->type == S_TRISTATE && *value != 'n')
694e54e692bSArnaud Lacombe 		fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
695e54e692bSArnaud Lacombe }
696e54e692bSArnaud Lacombe 
697e54e692bSArnaud Lacombe static struct conf_printer tristate_printer_cb =
698e54e692bSArnaud Lacombe {
699e54e692bSArnaud Lacombe 	.print_symbol = tristate_print_symbol,
700e54e692bSArnaud Lacombe 	.print_comment = kconfig_print_comment,
701e54e692bSArnaud Lacombe };
702e54e692bSArnaud Lacombe 
703e54e692bSArnaud Lacombe static void conf_write_symbol(FILE *fp, struct symbol *sym,
704e54e692bSArnaud Lacombe 			      struct conf_printer *printer, void *printer_arg)
705e54e692bSArnaud Lacombe {
706e54e692bSArnaud Lacombe 	const char *str;
707e54e692bSArnaud Lacombe 
708e54e692bSArnaud Lacombe 	switch (sym->type) {
70949192f26SSam Ravnborg 	case S_UNKNOWN:
71049192f26SSam Ravnborg 		break;
711e54e692bSArnaud Lacombe 	case S_STRING:
712e54e692bSArnaud Lacombe 		str = sym_get_string_value(sym);
713e54e692bSArnaud Lacombe 		str = sym_escape_string_value(str);
714e54e692bSArnaud Lacombe 		printer->print_symbol(fp, sym, str, printer_arg);
715e54e692bSArnaud Lacombe 		free((void *)str);
716e54e692bSArnaud Lacombe 		break;
717e54e692bSArnaud Lacombe 	default:
718e54e692bSArnaud Lacombe 		str = sym_get_string_value(sym);
719e54e692bSArnaud Lacombe 		printer->print_symbol(fp, sym, str, printer_arg);
72049192f26SSam Ravnborg 	}
72149192f26SSam Ravnborg }
72249192f26SSam Ravnborg 
723e54e692bSArnaud Lacombe static void
724e54e692bSArnaud Lacombe conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
725e54e692bSArnaud Lacombe {
726e54e692bSArnaud Lacombe 	char buf[256];
727e54e692bSArnaud Lacombe 
728e54e692bSArnaud Lacombe 	snprintf(buf, sizeof(buf),
729e54e692bSArnaud Lacombe 	    "\n"
730e54e692bSArnaud Lacombe 	    "Automatically generated file; DO NOT EDIT.\n"
731e54e692bSArnaud Lacombe 	    "%s\n",
732e54e692bSArnaud Lacombe 	    rootmenu.prompt->text);
733e54e692bSArnaud Lacombe 
734e54e692bSArnaud Lacombe 	printer->print_comment(fp, buf, printer_arg);
735e54e692bSArnaud Lacombe }
736e54e692bSArnaud Lacombe 
7377cf3d73bSSam Ravnborg /*
7387cf3d73bSSam Ravnborg  * Write out a minimal config.
7397cf3d73bSSam Ravnborg  * All values that has default values are skipped as this is redundant.
7407cf3d73bSSam Ravnborg  */
7417cf3d73bSSam Ravnborg int conf_write_defconfig(const char *filename)
7427cf3d73bSSam Ravnborg {
7437cf3d73bSSam Ravnborg 	struct symbol *sym;
7447cf3d73bSSam Ravnborg 	struct menu *menu;
7457cf3d73bSSam Ravnborg 	FILE *out;
7467cf3d73bSSam Ravnborg 
7477cf3d73bSSam Ravnborg 	out = fopen(filename, "w");
7487cf3d73bSSam Ravnborg 	if (!out)
7497cf3d73bSSam Ravnborg 		return 1;
7507cf3d73bSSam Ravnborg 
7517cf3d73bSSam Ravnborg 	sym_clear_all_valid();
7527cf3d73bSSam Ravnborg 
7537cf3d73bSSam Ravnborg 	/* Traverse all menus to find all relevant symbols */
7547cf3d73bSSam Ravnborg 	menu = rootmenu.list;
7557cf3d73bSSam Ravnborg 
7567cf3d73bSSam Ravnborg 	while (menu != NULL)
7577cf3d73bSSam Ravnborg 	{
7587cf3d73bSSam Ravnborg 		sym = menu->sym;
7597cf3d73bSSam Ravnborg 		if (sym == NULL) {
7607cf3d73bSSam Ravnborg 			if (!menu_is_visible(menu))
7617cf3d73bSSam Ravnborg 				goto next_menu;
7627cf3d73bSSam Ravnborg 		} else if (!sym_is_choice(sym)) {
7637cf3d73bSSam Ravnborg 			sym_calc_value(sym);
7647cf3d73bSSam Ravnborg 			if (!(sym->flags & SYMBOL_WRITE))
7657cf3d73bSSam Ravnborg 				goto next_menu;
7667cf3d73bSSam Ravnborg 			sym->flags &= ~SYMBOL_WRITE;
7677cf3d73bSSam Ravnborg 			/* If we cannot change the symbol - skip */
7687cf3d73bSSam Ravnborg 			if (!sym_is_changable(sym))
7697cf3d73bSSam Ravnborg 				goto next_menu;
7707cf3d73bSSam Ravnborg 			/* If symbol equals to default value - skip */
7717cf3d73bSSam Ravnborg 			if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
7727cf3d73bSSam Ravnborg 				goto next_menu;
7737cf3d73bSSam Ravnborg 
7747cf3d73bSSam Ravnborg 			/*
7757cf3d73bSSam Ravnborg 			 * If symbol is a choice value and equals to the
7767cf3d73bSSam Ravnborg 			 * default for a choice - skip.
77784062dd3SSam Ravnborg 			 * But only if value is bool and equal to "y" and
77884062dd3SSam Ravnborg 			 * choice is not "optional".
77984062dd3SSam Ravnborg 			 * (If choice is "optional" then all values can be "n")
7807cf3d73bSSam Ravnborg 			 */
7817cf3d73bSSam Ravnborg 			if (sym_is_choice_value(sym)) {
7827cf3d73bSSam Ravnborg 				struct symbol *cs;
7837cf3d73bSSam Ravnborg 				struct symbol *ds;
7847cf3d73bSSam Ravnborg 
7857cf3d73bSSam Ravnborg 				cs = prop_get_symbol(sym_get_choice_prop(sym));
7867cf3d73bSSam Ravnborg 				ds = sym_choice_default(cs);
78784062dd3SSam Ravnborg 				if (!sym_is_optional(cs) && sym == ds) {
788801690caSSam Ravnborg 					if ((sym->type == S_BOOLEAN) &&
7897cf3d73bSSam Ravnborg 					    sym_get_tristate_value(sym) == yes)
7907cf3d73bSSam Ravnborg 						goto next_menu;
7917cf3d73bSSam Ravnborg 				}
7927cf3d73bSSam Ravnborg 			}
793e54e692bSArnaud Lacombe 			conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
7947cf3d73bSSam Ravnborg 		}
7957cf3d73bSSam Ravnborg next_menu:
7967cf3d73bSSam Ravnborg 		if (menu->list != NULL) {
7977cf3d73bSSam Ravnborg 			menu = menu->list;
7987cf3d73bSSam Ravnborg 		}
7997cf3d73bSSam Ravnborg 		else if (menu->next != NULL) {
8007cf3d73bSSam Ravnborg 			menu = menu->next;
8017cf3d73bSSam Ravnborg 		} else {
8027cf3d73bSSam Ravnborg 			while ((menu = menu->parent)) {
8037cf3d73bSSam Ravnborg 				if (menu->next != NULL) {
8047cf3d73bSSam Ravnborg 					menu = menu->next;
8057cf3d73bSSam Ravnborg 					break;
8067cf3d73bSSam Ravnborg 				}
8077cf3d73bSSam Ravnborg 			}
8087cf3d73bSSam Ravnborg 		}
8097cf3d73bSSam Ravnborg 	}
8107cf3d73bSSam Ravnborg 	fclose(out);
8117cf3d73bSSam Ravnborg 	return 0;
8127cf3d73bSSam Ravnborg }
8137cf3d73bSSam Ravnborg 
8141da177e4SLinus Torvalds int conf_write(const char *name)
8151da177e4SLinus Torvalds {
816c955ccafSRoman Zippel 	FILE *out;
8171da177e4SLinus Torvalds 	struct symbol *sym;
8181da177e4SLinus Torvalds 	struct menu *menu;
8191da177e4SLinus Torvalds 	const char *basename;
8201da177e4SLinus Torvalds 	const char *str;
8212ae89c7aSNathan Chancellor 	char dirname[PATH_MAX+1], tmpname[PATH_MAX+22], newname[PATH_MAX+8];
8221da177e4SLinus Torvalds 	char *env;
8231da177e4SLinus Torvalds 
8241da177e4SLinus Torvalds 	dirname[0] = 0;
8251da177e4SLinus Torvalds 	if (name && name[0]) {
8261da177e4SLinus Torvalds 		char *slash;
8271da177e4SLinus Torvalds 
8280608182aSMasahiro Yamada 		if (is_dir(name)) {
8291da177e4SLinus Torvalds 			strcpy(dirname, name);
8301da177e4SLinus Torvalds 			strcat(dirname, "/");
83114cdd3c4SRoman Zippel 			basename = conf_get_configname();
8321da177e4SLinus Torvalds 		} else if ((slash = strrchr(name, '/'))) {
8331da177e4SLinus Torvalds 			int size = slash - name + 1;
8341da177e4SLinus Torvalds 			memcpy(dirname, name, size);
8351da177e4SLinus Torvalds 			dirname[size] = 0;
8361da177e4SLinus Torvalds 			if (slash[1])
8371da177e4SLinus Torvalds 				basename = slash + 1;
8381da177e4SLinus Torvalds 			else
83914cdd3c4SRoman Zippel 				basename = conf_get_configname();
8401da177e4SLinus Torvalds 		} else
8411da177e4SLinus Torvalds 			basename = name;
8421da177e4SLinus Torvalds 	} else
84314cdd3c4SRoman Zippel 		basename = conf_get_configname();
8441da177e4SLinus Torvalds 
84514cdd3c4SRoman Zippel 	sprintf(newname, "%s%s", dirname, basename);
84614cdd3c4SRoman Zippel 	env = getenv("KCONFIG_OVERWRITECONFIG");
84714cdd3c4SRoman Zippel 	if (!env || !*env) {
84814cdd3c4SRoman Zippel 		sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
84914cdd3c4SRoman Zippel 		out = fopen(tmpname, "w");
85014cdd3c4SRoman Zippel 	} else {
85114cdd3c4SRoman Zippel 		*tmpname = 0;
8521da177e4SLinus Torvalds 		out = fopen(newname, "w");
85314cdd3c4SRoman Zippel 	}
8541da177e4SLinus Torvalds 	if (!out)
8551da177e4SLinus Torvalds 		return 1;
85614cdd3c4SRoman Zippel 
857e54e692bSArnaud Lacombe 	conf_write_heading(out, &kconfig_printer_cb, NULL);
8581da177e4SLinus Torvalds 
859b3214293SKarsten Wiese 	if (!conf_get_changed())
8601da177e4SLinus Torvalds 		sym_clear_all_valid();
8611da177e4SLinus Torvalds 
8621da177e4SLinus Torvalds 	menu = rootmenu.list;
8631da177e4SLinus Torvalds 	while (menu) {
8641da177e4SLinus Torvalds 		sym = menu->sym;
8651da177e4SLinus Torvalds 		if (!sym) {
8661da177e4SLinus Torvalds 			if (!menu_is_visible(menu))
8671da177e4SLinus Torvalds 				goto next;
8681da177e4SLinus Torvalds 			str = menu_get_prompt(menu);
8691da177e4SLinus Torvalds 			fprintf(out, "\n"
8701da177e4SLinus Torvalds 				     "#\n"
8711da177e4SLinus Torvalds 				     "# %s\n"
8721da177e4SLinus Torvalds 				     "#\n", str);
8731da177e4SLinus Torvalds 		} else if (!(sym->flags & SYMBOL_CHOICE)) {
8741da177e4SLinus Torvalds 			sym_calc_value(sym);
8751da177e4SLinus Torvalds 			if (!(sym->flags & SYMBOL_WRITE))
8761da177e4SLinus Torvalds 				goto next;
8771da177e4SLinus Torvalds 			sym->flags &= ~SYMBOL_WRITE;
878e54e692bSArnaud Lacombe 
879e54e692bSArnaud Lacombe 			conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
8801da177e4SLinus Torvalds 		}
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds next:
8831da177e4SLinus Torvalds 		if (menu->list) {
8841da177e4SLinus Torvalds 			menu = menu->list;
8851da177e4SLinus Torvalds 			continue;
8861da177e4SLinus Torvalds 		}
8871da177e4SLinus Torvalds 		if (menu->next)
8881da177e4SLinus Torvalds 			menu = menu->next;
8891da177e4SLinus Torvalds 		else while ((menu = menu->parent)) {
8901da177e4SLinus Torvalds 			if (menu->next) {
8911da177e4SLinus Torvalds 				menu = menu->next;
8921da177e4SLinus Torvalds 				break;
8931da177e4SLinus Torvalds 			}
8941da177e4SLinus Torvalds 		}
8951da177e4SLinus Torvalds 	}
8961da177e4SLinus Torvalds 	fclose(out);
89714cdd3c4SRoman Zippel 
89814cdd3c4SRoman Zippel 	if (*tmpname) {
8999a3d0fe8SSam Ravnborg 		strcat(dirname, basename);
90014cdd3c4SRoman Zippel 		strcat(dirname, ".old");
90114cdd3c4SRoman Zippel 		rename(newname, dirname);
90214cdd3c4SRoman Zippel 		if (rename(tmpname, newname))
9031da177e4SLinus Torvalds 			return 1;
90414cdd3c4SRoman Zippel 	}
9051da177e4SLinus Torvalds 
906694c49a7SSam Ravnborg 	conf_message("configuration written to %s", newname);
907ddc97cacSRoman Zippel 
908bfc10001SKarsten Wiese 	sym_set_change_count(0);
9091da177e4SLinus Torvalds 
9101da177e4SLinus Torvalds 	return 0;
9111da177e4SLinus Torvalds }
912c955ccafSRoman Zippel 
913a2ff4040SMasahiro Yamada /* write a dependency file as used by kbuild to track dependencies */
914a2ff4040SMasahiro Yamada static int conf_write_dep(const char *name)
915a2ff4040SMasahiro Yamada {
916a2ff4040SMasahiro Yamada 	struct file *file;
917a2ff4040SMasahiro Yamada 	FILE *out;
918a2ff4040SMasahiro Yamada 
919a2ff4040SMasahiro Yamada 	if (!name)
920a2ff4040SMasahiro Yamada 		name = ".kconfig.d";
921a2ff4040SMasahiro Yamada 	out = fopen("..config.tmp", "w");
922a2ff4040SMasahiro Yamada 	if (!out)
923a2ff4040SMasahiro Yamada 		return 1;
924a2ff4040SMasahiro Yamada 	fprintf(out, "deps_config := \\\n");
925a2ff4040SMasahiro Yamada 	for (file = file_list; file; file = file->next) {
926a2ff4040SMasahiro Yamada 		if (file->next)
927a2ff4040SMasahiro Yamada 			fprintf(out, "\t%s \\\n", file->name);
928a2ff4040SMasahiro Yamada 		else
929a2ff4040SMasahiro Yamada 			fprintf(out, "\t%s\n", file->name);
930a2ff4040SMasahiro Yamada 	}
931a2ff4040SMasahiro Yamada 	fprintf(out, "\n%s: \\\n"
932a2ff4040SMasahiro Yamada 		     "\t$(deps_config)\n\n", conf_get_autoconfig_name());
933a2ff4040SMasahiro Yamada 
934a2ff4040SMasahiro Yamada 	env_write_dep(out, conf_get_autoconfig_name());
935a2ff4040SMasahiro Yamada 
936a2ff4040SMasahiro Yamada 	fprintf(out, "\n$(deps_config): ;\n");
937a2ff4040SMasahiro Yamada 	fclose(out);
93879123b13SMasahiro Yamada 
93979123b13SMasahiro Yamada 	if (make_parent_dir(name))
94079123b13SMasahiro Yamada 		return 1;
941a2ff4040SMasahiro Yamada 	rename("..config.tmp", name);
942a2ff4040SMasahiro Yamada 	return 0;
943a2ff4040SMasahiro Yamada }
944a2ff4040SMasahiro Yamada 
9450849d212SMasahiro Yamada static int conf_touch_deps(void)
9462e3646e5SRoman Zippel {
94712122f62SMarkus Heidelberg 	const char *name;
9482e3646e5SRoman Zippel 	struct symbol *sym;
9491508fec8SMasahiro Yamada 	int res, i;
9501508fec8SMasahiro Yamada 
9511508fec8SMasahiro Yamada 	strcpy(depfile_path, "include/config/");
9521508fec8SMasahiro Yamada 	depfile_prefix_len = strlen(depfile_path);
9532e3646e5SRoman Zippel 
95412122f62SMarkus Heidelberg 	name = conf_get_autoconfig_name();
9552e3646e5SRoman Zippel 	conf_read_simple(name, S_DEF_AUTO);
9566b87b70cSAl Viro 	sym_calc_value(modules_sym);
9572e3646e5SRoman Zippel 
9582e3646e5SRoman Zippel 	for_all_symbols(i, sym) {
9592e3646e5SRoman Zippel 		sym_calc_value(sym);
960693359f7SDirk Gouders 		if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
9612e3646e5SRoman Zippel 			continue;
9622e3646e5SRoman Zippel 		if (sym->flags & SYMBOL_WRITE) {
9632e3646e5SRoman Zippel 			if (sym->flags & SYMBOL_DEF_AUTO) {
9642e3646e5SRoman Zippel 				/*
9652e3646e5SRoman Zippel 				 * symbol has old and new value,
9662e3646e5SRoman Zippel 				 * so compare them...
9672e3646e5SRoman Zippel 				 */
9682e3646e5SRoman Zippel 				switch (sym->type) {
9692e3646e5SRoman Zippel 				case S_BOOLEAN:
9702e3646e5SRoman Zippel 				case S_TRISTATE:
9712e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) ==
9722e3646e5SRoman Zippel 					    sym->def[S_DEF_AUTO].tri)
9732e3646e5SRoman Zippel 						continue;
9742e3646e5SRoman Zippel 					break;
9752e3646e5SRoman Zippel 				case S_STRING:
9762e3646e5SRoman Zippel 				case S_HEX:
9772e3646e5SRoman Zippel 				case S_INT:
9782e3646e5SRoman Zippel 					if (!strcmp(sym_get_string_value(sym),
9792e3646e5SRoman Zippel 						    sym->def[S_DEF_AUTO].val))
9802e3646e5SRoman Zippel 						continue;
9812e3646e5SRoman Zippel 					break;
9822e3646e5SRoman Zippel 				default:
9832e3646e5SRoman Zippel 					break;
9842e3646e5SRoman Zippel 				}
9852e3646e5SRoman Zippel 			} else {
9862e3646e5SRoman Zippel 				/*
9872e3646e5SRoman Zippel 				 * If there is no old value, only 'no' (unset)
9882e3646e5SRoman Zippel 				 * is allowed as new value.
9892e3646e5SRoman Zippel 				 */
9902e3646e5SRoman Zippel 				switch (sym->type) {
9912e3646e5SRoman Zippel 				case S_BOOLEAN:
9922e3646e5SRoman Zippel 				case S_TRISTATE:
9932e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) == no)
9942e3646e5SRoman Zippel 						continue;
9952e3646e5SRoman Zippel 					break;
9962e3646e5SRoman Zippel 				default:
9972e3646e5SRoman Zippel 					break;
9982e3646e5SRoman Zippel 				}
9992e3646e5SRoman Zippel 			}
10002e3646e5SRoman Zippel 		} else if (!(sym->flags & SYMBOL_DEF_AUTO))
10012e3646e5SRoman Zippel 			/* There is neither an old nor a new value. */
10022e3646e5SRoman Zippel 			continue;
10032e3646e5SRoman Zippel 		/* else
10042e3646e5SRoman Zippel 		 *	There is an old value, but no new value ('no' (unset)
10052e3646e5SRoman Zippel 		 *	isn't saved in auto.conf, so the old value is always
10062e3646e5SRoman Zippel 		 *	different from 'no').
10072e3646e5SRoman Zippel 		 */
10082e3646e5SRoman Zippel 
10091508fec8SMasahiro Yamada 		res = conf_touch_dep(sym->name);
10101508fec8SMasahiro Yamada 		if (res)
10112e3646e5SRoman Zippel 			return res;
10122e3646e5SRoman Zippel 	}
10132e3646e5SRoman Zippel 
10141508fec8SMasahiro Yamada 	return 0;
10151508fec8SMasahiro Yamada }
10161508fec8SMasahiro Yamada 
101700c864f8SMasahiro Yamada int conf_write_autoconf(int overwrite)
1018c955ccafSRoman Zippel {
1019c955ccafSRoman Zippel 	struct symbol *sym;
102012122f62SMarkus Heidelberg 	const char *name;
102100c864f8SMasahiro Yamada 	const char *autoconf_name = conf_get_autoconfig_name();
1022bc081dd6SMichal Marek 	FILE *out, *tristate, *out_h;
102349192f26SSam Ravnborg 	int i;
1024c955ccafSRoman Zippel 
102500c864f8SMasahiro Yamada 	if (!overwrite && is_present(autoconf_name))
102600c864f8SMasahiro Yamada 		return 0;
102700c864f8SMasahiro Yamada 
10282e3646e5SRoman Zippel 	sym_clear_all_valid();
10292e3646e5SRoman Zippel 
1030a2ff4040SMasahiro Yamada 	conf_write_dep("include/config/auto.conf.cmd");
1031c955ccafSRoman Zippel 
10320849d212SMasahiro Yamada 	if (conf_touch_deps())
10332e3646e5SRoman Zippel 		return 1;
10342e3646e5SRoman Zippel 
1035c955ccafSRoman Zippel 	out = fopen(".tmpconfig", "w");
1036c955ccafSRoman Zippel 	if (!out)
1037c955ccafSRoman Zippel 		return 1;
1038c955ccafSRoman Zippel 
1039bc081dd6SMichal Marek 	tristate = fopen(".tmpconfig_tristate", "w");
1040bc081dd6SMichal Marek 	if (!tristate) {
1041bc081dd6SMichal Marek 		fclose(out);
1042bc081dd6SMichal Marek 		return 1;
1043bc081dd6SMichal Marek 	}
1044bc081dd6SMichal Marek 
1045c955ccafSRoman Zippel 	out_h = fopen(".tmpconfig.h", "w");
1046c955ccafSRoman Zippel 	if (!out_h) {
1047c955ccafSRoman Zippel 		fclose(out);
1048bc081dd6SMichal Marek 		fclose(tristate);
1049c955ccafSRoman Zippel 		return 1;
1050c955ccafSRoman Zippel 	}
1051c955ccafSRoman Zippel 
1052e54e692bSArnaud Lacombe 	conf_write_heading(out, &kconfig_printer_cb, NULL);
1053e54e692bSArnaud Lacombe 
1054e54e692bSArnaud Lacombe 	conf_write_heading(tristate, &tristate_printer_cb, NULL);
1055e54e692bSArnaud Lacombe 
1056e54e692bSArnaud Lacombe 	conf_write_heading(out_h, &header_printer_cb, NULL);
1057c955ccafSRoman Zippel 
1058c955ccafSRoman Zippel 	for_all_symbols(i, sym) {
1059953742c8SArnaud Lacombe 		sym_calc_value(sym);
1060a9596135SPaul Gortmaker 		if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
1061953742c8SArnaud Lacombe 			continue;
1062953742c8SArnaud Lacombe 
1063a9596135SPaul Gortmaker 		/* write symbol to auto.conf, tristate and header files */
1064e54e692bSArnaud Lacombe 		conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
106549192f26SSam Ravnborg 
1066e54e692bSArnaud Lacombe 		conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
1067e54e692bSArnaud Lacombe 
1068e54e692bSArnaud Lacombe 		conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
1069c955ccafSRoman Zippel 	}
1070c955ccafSRoman Zippel 	fclose(out);
1071bc081dd6SMichal Marek 	fclose(tristate);
1072c955ccafSRoman Zippel 	fclose(out_h);
1073c955ccafSRoman Zippel 
1074c955ccafSRoman Zippel 	name = getenv("KCONFIG_AUTOHEADER");
1075c955ccafSRoman Zippel 	if (!name)
1076264a2683SSam Ravnborg 		name = "include/generated/autoconf.h";
107779123b13SMasahiro Yamada 	if (make_parent_dir(name))
107879123b13SMasahiro Yamada 		return 1;
1079c955ccafSRoman Zippel 	if (rename(".tmpconfig.h", name))
1080c955ccafSRoman Zippel 		return 1;
108179123b13SMasahiro Yamada 
1082bc081dd6SMichal Marek 	name = getenv("KCONFIG_TRISTATE");
1083bc081dd6SMichal Marek 	if (!name)
1084bc081dd6SMichal Marek 		name = "include/config/tristate.conf";
108579123b13SMasahiro Yamada 	if (make_parent_dir(name))
108679123b13SMasahiro Yamada 		return 1;
1087bc081dd6SMichal Marek 	if (rename(".tmpconfig_tristate", name))
1088bc081dd6SMichal Marek 		return 1;
108979123b13SMasahiro Yamada 
109000c864f8SMasahiro Yamada 	if (make_parent_dir(autoconf_name))
109179123b13SMasahiro Yamada 		return 1;
1092c955ccafSRoman Zippel 	/*
1093c955ccafSRoman Zippel 	 * This must be the last step, kbuild has a dependency on auto.conf
1094c955ccafSRoman Zippel 	 * and this marks the successful completion of the previous steps.
1095c955ccafSRoman Zippel 	 */
109600c864f8SMasahiro Yamada 	if (rename(".tmpconfig", autoconf_name))
1097c955ccafSRoman Zippel 		return 1;
1098c955ccafSRoman Zippel 
1099c955ccafSRoman Zippel 	return 0;
1100c955ccafSRoman Zippel }
1101b3214293SKarsten Wiese 
1102bfc10001SKarsten Wiese static int sym_change_count;
11033b354c55SKarsten Wiese static void (*conf_changed_callback)(void);
1104bfc10001SKarsten Wiese 
1105bfc10001SKarsten Wiese void sym_set_change_count(int count)
1106bfc10001SKarsten Wiese {
11073b354c55SKarsten Wiese 	int _sym_change_count = sym_change_count;
1108bfc10001SKarsten Wiese 	sym_change_count = count;
11093b354c55SKarsten Wiese 	if (conf_changed_callback &&
11103b354c55SKarsten Wiese 	    (bool)_sym_change_count != (bool)count)
11113b354c55SKarsten Wiese 		conf_changed_callback();
1112bfc10001SKarsten Wiese }
1113bfc10001SKarsten Wiese 
1114bfc10001SKarsten Wiese void sym_add_change_count(int count)
1115bfc10001SKarsten Wiese {
11163b354c55SKarsten Wiese 	sym_set_change_count(count + sym_change_count);
1117bfc10001SKarsten Wiese }
1118bfc10001SKarsten Wiese 
1119b3214293SKarsten Wiese bool conf_get_changed(void)
1120b3214293SKarsten Wiese {
1121b3214293SKarsten Wiese 	return sym_change_count;
1122b3214293SKarsten Wiese }
11233b354c55SKarsten Wiese 
11243b354c55SKarsten Wiese void conf_set_changed_callback(void (*fn)(void))
11253b354c55SKarsten Wiese {
11263b354c55SKarsten Wiese 	conf_changed_callback = fn;
11273b354c55SKarsten Wiese }
1128dc7862e5SRoman Zippel 
11293b9a19e0SYann E. MORIN static bool randomize_choice_values(struct symbol *csym)
1130a64b44eaSSam Ravnborg {
1131a64b44eaSSam Ravnborg 	struct property *prop;
1132a64b44eaSSam Ravnborg 	struct symbol *sym;
1133a64b44eaSSam Ravnborg 	struct expr *e;
1134a64b44eaSSam Ravnborg 	int cnt, def;
1135a64b44eaSSam Ravnborg 
1136a64b44eaSSam Ravnborg 	/*
1137579fb8e7SArnaud Lacombe 	 * If choice is mod then we may have more items selected
1138a64b44eaSSam Ravnborg 	 * and if no then no-one.
1139a64b44eaSSam Ravnborg 	 * In both cases stop.
1140a64b44eaSSam Ravnborg 	 */
1141a64b44eaSSam Ravnborg 	if (csym->curr.tri != yes)
11423b9a19e0SYann E. MORIN 		return false;
1143a64b44eaSSam Ravnborg 
1144a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
1145a64b44eaSSam Ravnborg 
1146a64b44eaSSam Ravnborg 	/* count entries in choice block */
1147a64b44eaSSam Ravnborg 	cnt = 0;
1148a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym)
1149a64b44eaSSam Ravnborg 		cnt++;
1150a64b44eaSSam Ravnborg 
1151a64b44eaSSam Ravnborg 	/*
1152a64b44eaSSam Ravnborg 	 * find a random value and set it to yes,
1153a64b44eaSSam Ravnborg 	 * set the rest to no so we have only one set
1154a64b44eaSSam Ravnborg 	 */
1155a64b44eaSSam Ravnborg 	def = (rand() % cnt);
1156a64b44eaSSam Ravnborg 
1157a64b44eaSSam Ravnborg 	cnt = 0;
1158a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
1159a64b44eaSSam Ravnborg 		if (def == cnt++) {
1160a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = yes;
1161a64b44eaSSam Ravnborg 			csym->def[S_DEF_USER].val = sym;
1162a64b44eaSSam Ravnborg 		}
1163a64b44eaSSam Ravnborg 		else {
1164a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
1165a64b44eaSSam Ravnborg 		}
1166e6abf12aSYann E. MORIN 		sym->flags |= SYMBOL_DEF_USER;
1167e6abf12aSYann E. MORIN 		/* clear VALID to get value calculated */
1168e6abf12aSYann E. MORIN 		sym->flags &= ~SYMBOL_VALID;
1169a64b44eaSSam Ravnborg 	}
1170a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
1171a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
1172a64b44eaSSam Ravnborg 	csym->flags &= ~(SYMBOL_VALID);
11733b9a19e0SYann E. MORIN 
11743b9a19e0SYann E. MORIN 	return true;
1175a64b44eaSSam Ravnborg }
1176a64b44eaSSam Ravnborg 
1177fbe98bb9SArve Hjønnevåg void set_all_choice_values(struct symbol *csym)
1178a64b44eaSSam Ravnborg {
1179a64b44eaSSam Ravnborg 	struct property *prop;
1180a64b44eaSSam Ravnborg 	struct symbol *sym;
1181a64b44eaSSam Ravnborg 	struct expr *e;
1182a64b44eaSSam Ravnborg 
1183a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
1184a64b44eaSSam Ravnborg 
1185a64b44eaSSam Ravnborg 	/*
1186a64b44eaSSam Ravnborg 	 * Set all non-assinged choice values to no
1187a64b44eaSSam Ravnborg 	 */
1188a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
1189a64b44eaSSam Ravnborg 		if (!sym_has_value(sym))
1190a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
1191a64b44eaSSam Ravnborg 	}
1192a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
1193a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
1194fbe98bb9SArve Hjønnevåg 	csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1195a64b44eaSSam Ravnborg }
1196dc7862e5SRoman Zippel 
11973b9a19e0SYann E. MORIN bool conf_set_all_new_symbols(enum conf_def_mode mode)
1198dc7862e5SRoman Zippel {
1199dc7862e5SRoman Zippel 	struct symbol *sym, *csym;
1200b92d804aSMasahiro Yamada 	int i, cnt, pby, pty, ptm;	/* pby: probability of bool     = y
1201e43956e6SYann E. MORIN 					 * pty: probability of tristate = y
1202e43956e6SYann E. MORIN 					 * ptm: probability of tristate = m
1203e43956e6SYann E. MORIN 					 */
1204e43956e6SYann E. MORIN 
1205e43956e6SYann E. MORIN 	pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1206e43956e6SYann E. MORIN 				   * below, otherwise gcc whines about
1207e43956e6SYann E. MORIN 				   * -Wmaybe-uninitialized */
1208e43956e6SYann E. MORIN 	if (mode == def_random) {
1209e43956e6SYann E. MORIN 		int n, p[3];
1210e43956e6SYann E. MORIN 		char *env = getenv("KCONFIG_PROBABILITY");
1211e43956e6SYann E. MORIN 		n = 0;
1212e43956e6SYann E. MORIN 		while( env && *env ) {
1213e43956e6SYann E. MORIN 			char *endp;
1214e43956e6SYann E. MORIN 			int tmp = strtol( env, &endp, 10 );
1215e43956e6SYann E. MORIN 			if( tmp >= 0 && tmp <= 100 ) {
1216e43956e6SYann E. MORIN 				p[n++] = tmp;
1217e43956e6SYann E. MORIN 			} else {
1218e43956e6SYann E. MORIN 				errno = ERANGE;
1219e43956e6SYann E. MORIN 				perror( "KCONFIG_PROBABILITY" );
1220e43956e6SYann E. MORIN 				exit( 1 );
1221e43956e6SYann E. MORIN 			}
1222e43956e6SYann E. MORIN 			env = (*endp == ':') ? endp+1 : endp;
1223e43956e6SYann E. MORIN 			if( n >=3 ) {
1224e43956e6SYann E. MORIN 				break;
1225e43956e6SYann E. MORIN 			}
1226e43956e6SYann E. MORIN 		}
1227e43956e6SYann E. MORIN 		switch( n ) {
1228e43956e6SYann E. MORIN 		case 1:
1229e43956e6SYann E. MORIN 			pby = p[0]; ptm = pby/2; pty = pby-ptm;
1230e43956e6SYann E. MORIN 			break;
1231e43956e6SYann E. MORIN 		case 2:
1232e43956e6SYann E. MORIN 			pty = p[0]; ptm = p[1]; pby = pty + ptm;
1233e43956e6SYann E. MORIN 			break;
1234e43956e6SYann E. MORIN 		case 3:
1235e43956e6SYann E. MORIN 			pby = p[0]; pty = p[1]; ptm = p[2];
1236e43956e6SYann E. MORIN 			break;
1237e43956e6SYann E. MORIN 		}
1238e43956e6SYann E. MORIN 
1239e43956e6SYann E. MORIN 		if( pty+ptm > 100 ) {
1240e43956e6SYann E. MORIN 			errno = ERANGE;
1241e43956e6SYann E. MORIN 			perror( "KCONFIG_PROBABILITY" );
1242e43956e6SYann E. MORIN 			exit( 1 );
1243e43956e6SYann E. MORIN 		}
1244e43956e6SYann E. MORIN 	}
12453b9a19e0SYann E. MORIN 	bool has_changed = false;
1246dc7862e5SRoman Zippel 
1247dc7862e5SRoman Zippel 	for_all_symbols(i, sym) {
1248cfa98f2eSYann E. MORIN 		if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1249dc7862e5SRoman Zippel 			continue;
1250dc7862e5SRoman Zippel 		switch (sym_get_type(sym)) {
1251dc7862e5SRoman Zippel 		case S_BOOLEAN:
1252dc7862e5SRoman Zippel 		case S_TRISTATE:
12533b9a19e0SYann E. MORIN 			has_changed = true;
1254dc7862e5SRoman Zippel 			switch (mode) {
1255dc7862e5SRoman Zippel 			case def_yes:
1256dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = yes;
1257dc7862e5SRoman Zippel 				break;
1258dc7862e5SRoman Zippel 			case def_mod:
1259dc7862e5SRoman Zippel 				sym->def[S_DEF_USER].tri = mod;
1260dc7862e5SRoman Zippel 				break;
1261dc7862e5SRoman Zippel 			case def_no:
12625d2acfc7SJosh Triplett 				if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
12635d2acfc7SJosh Triplett 					sym->def[S_DEF_USER].tri = yes;
12645d2acfc7SJosh Triplett 				else
1265dc7862e5SRoman Zippel 					sym->def[S_DEF_USER].tri = no;
1266dc7862e5SRoman Zippel 				break;
1267dc7862e5SRoman Zippel 			case def_random:
1268e43956e6SYann E. MORIN 				sym->def[S_DEF_USER].tri = no;
1269e43956e6SYann E. MORIN 				cnt = rand() % 100;
1270e43956e6SYann E. MORIN 				if (sym->type == S_TRISTATE) {
1271e43956e6SYann E. MORIN 					if (cnt < pty)
1272e43956e6SYann E. MORIN 						sym->def[S_DEF_USER].tri = yes;
1273e43956e6SYann E. MORIN 					else if (cnt < (pty+ptm))
1274e43956e6SYann E. MORIN 						sym->def[S_DEF_USER].tri = mod;
1275e43956e6SYann E. MORIN 				} else if (cnt < pby)
1276e43956e6SYann E. MORIN 					sym->def[S_DEF_USER].tri = yes;
1277dc7862e5SRoman Zippel 				break;
1278dc7862e5SRoman Zippel 			default:
1279dc7862e5SRoman Zippel 				continue;
1280dc7862e5SRoman Zippel 			}
1281184832c9SSam Ravnborg 			if (!(sym_is_choice(sym) && mode == def_random))
1282dc7862e5SRoman Zippel 				sym->flags |= SYMBOL_DEF_USER;
1283dc7862e5SRoman Zippel 			break;
1284dc7862e5SRoman Zippel 		default:
1285dc7862e5SRoman Zippel 			break;
1286dc7862e5SRoman Zippel 		}
1287dc7862e5SRoman Zippel 
1288dc7862e5SRoman Zippel 	}
1289dc7862e5SRoman Zippel 
1290ce97e13eSAl Viro 	sym_clear_all_valid();
1291dc7862e5SRoman Zippel 
1292184832c9SSam Ravnborg 	/*
1293184832c9SSam Ravnborg 	 * We have different type of choice blocks.
1294579fb8e7SArnaud Lacombe 	 * If curr.tri equals to mod then we can select several
1295184832c9SSam Ravnborg 	 * choice symbols in one block.
1296184832c9SSam Ravnborg 	 * In this case we do nothing.
1297579fb8e7SArnaud Lacombe 	 * If curr.tri equals yes then only one symbol can be
1298184832c9SSam Ravnborg 	 * selected in a choice block and we set it to yes,
1299184832c9SSam Ravnborg 	 * and the rest to no.
1300184832c9SSam Ravnborg 	 */
1301fbe98bb9SArve Hjønnevåg 	if (mode != def_random) {
1302fbe98bb9SArve Hjønnevåg 		for_all_symbols(i, csym) {
1303fbe98bb9SArve Hjønnevåg 			if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1304fbe98bb9SArve Hjønnevåg 			    sym_is_choice_value(csym))
1305fbe98bb9SArve Hjønnevåg 				csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1306fbe98bb9SArve Hjønnevåg 		}
1307fbe98bb9SArve Hjønnevåg 	}
1308fbe98bb9SArve Hjønnevåg 
1309dc7862e5SRoman Zippel 	for_all_symbols(i, csym) {
1310dc7862e5SRoman Zippel 		if (sym_has_value(csym) || !sym_is_choice(csym))
1311dc7862e5SRoman Zippel 			continue;
1312dc7862e5SRoman Zippel 
1313dc7862e5SRoman Zippel 		sym_calc_value(csym);
1314a64b44eaSSam Ravnborg 		if (mode == def_random)
13153b9a19e0SYann E. MORIN 			has_changed = randomize_choice_values(csym);
13163b9a19e0SYann E. MORIN 		else {
13173b9a19e0SYann E. MORIN 			set_all_choice_values(csym);
13183b9a19e0SYann E. MORIN 			has_changed = true;
1319dc7862e5SRoman Zippel 		}
1320dc7862e5SRoman Zippel 	}
13213b9a19e0SYann E. MORIN 
13223b9a19e0SYann E. MORIN 	return has_changed;
13233b9a19e0SYann E. MORIN }
1324