xref: /openbmc/linux/scripts/kconfig/confdata.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
10c874100SMasahiro Yamada // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
667424f61SMasahiro Yamada #include <sys/mman.h>
71da177e4SLinus Torvalds #include <sys/stat.h>
878cb0907SBoris Kolpackov #include <sys/types.h>
91da177e4SLinus Torvalds #include <ctype.h>
1094bedecaSArnaud Lacombe #include <errno.h>
112e3646e5SRoman Zippel #include <fcntl.h>
12558e78e3SMasahiro Yamada #include <limits.h>
1310a4b277SArnaud Lacombe #include <stdarg.h>
146ce45a91SMasahiro Yamada #include <stdbool.h>
151da177e4SLinus Torvalds #include <stdio.h>
161da177e4SLinus Torvalds #include <stdlib.h>
171da177e4SLinus Torvalds #include <string.h>
181da177e4SLinus Torvalds #include <time.h>
191da177e4SLinus Torvalds #include <unistd.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include "lkc.h"
221da177e4SLinus Torvalds 
230608182aSMasahiro Yamada /* return true if 'path' exists, false otherwise */
is_present(const char * path)240608182aSMasahiro Yamada static bool is_present(const char *path)
250608182aSMasahiro Yamada {
260608182aSMasahiro Yamada 	struct stat st;
270608182aSMasahiro Yamada 
280608182aSMasahiro Yamada 	return !stat(path, &st);
290608182aSMasahiro Yamada }
300608182aSMasahiro Yamada 
310608182aSMasahiro Yamada /* return true if 'path' exists and it is a directory, false otherwise */
is_dir(const char * path)320608182aSMasahiro Yamada static bool is_dir(const char *path)
330608182aSMasahiro Yamada {
340608182aSMasahiro Yamada 	struct stat st;
350608182aSMasahiro Yamada 
360608182aSMasahiro Yamada 	if (stat(path, &st))
37a69b191fSYang Li 		return false;
380608182aSMasahiro Yamada 
390608182aSMasahiro Yamada 	return S_ISDIR(st.st_mode);
400608182aSMasahiro Yamada }
410608182aSMasahiro Yamada 
4267424f61SMasahiro Yamada /* return true if the given two files are the same, false otherwise */
is_same(const char * file1,const char * file2)4367424f61SMasahiro Yamada static bool is_same(const char *file1, const char *file2)
4467424f61SMasahiro Yamada {
4567424f61SMasahiro Yamada 	int fd1, fd2;
4667424f61SMasahiro Yamada 	struct stat st1, st2;
4767424f61SMasahiro Yamada 	void *map1, *map2;
4867424f61SMasahiro Yamada 	bool ret = false;
4967424f61SMasahiro Yamada 
5067424f61SMasahiro Yamada 	fd1 = open(file1, O_RDONLY);
5167424f61SMasahiro Yamada 	if (fd1 < 0)
5267424f61SMasahiro Yamada 		return ret;
5367424f61SMasahiro Yamada 
5467424f61SMasahiro Yamada 	fd2 = open(file2, O_RDONLY);
5567424f61SMasahiro Yamada 	if (fd2 < 0)
5667424f61SMasahiro Yamada 		goto close1;
5767424f61SMasahiro Yamada 
5867424f61SMasahiro Yamada 	ret = fstat(fd1, &st1);
5967424f61SMasahiro Yamada 	if (ret)
6067424f61SMasahiro Yamada 		goto close2;
6167424f61SMasahiro Yamada 	ret = fstat(fd2, &st2);
6267424f61SMasahiro Yamada 	if (ret)
6367424f61SMasahiro Yamada 		goto close2;
6467424f61SMasahiro Yamada 
6567424f61SMasahiro Yamada 	if (st1.st_size != st2.st_size)
6667424f61SMasahiro Yamada 		goto close2;
6767424f61SMasahiro Yamada 
6867424f61SMasahiro Yamada 	map1 = mmap(NULL, st1.st_size, PROT_READ, MAP_PRIVATE, fd1, 0);
6967424f61SMasahiro Yamada 	if (map1 == MAP_FAILED)
7067424f61SMasahiro Yamada 		goto close2;
7167424f61SMasahiro Yamada 
7267424f61SMasahiro Yamada 	map2 = mmap(NULL, st2.st_size, PROT_READ, MAP_PRIVATE, fd2, 0);
7367424f61SMasahiro Yamada 	if (map2 == MAP_FAILED)
7467424f61SMasahiro Yamada 		goto close2;
7567424f61SMasahiro Yamada 
7667424f61SMasahiro Yamada 	if (bcmp(map1, map2, st1.st_size))
7767424f61SMasahiro Yamada 		goto close2;
7867424f61SMasahiro Yamada 
7967424f61SMasahiro Yamada 	ret = true;
8067424f61SMasahiro Yamada close2:
8167424f61SMasahiro Yamada 	close(fd2);
8267424f61SMasahiro Yamada close1:
8367424f61SMasahiro Yamada 	close(fd1);
8467424f61SMasahiro Yamada 
8567424f61SMasahiro Yamada 	return ret;
8667424f61SMasahiro Yamada }
8767424f61SMasahiro Yamada 
880608182aSMasahiro Yamada /*
890608182aSMasahiro Yamada  * Create the parent directory of the given path.
900608182aSMasahiro Yamada  *
910608182aSMasahiro Yamada  * For example, if 'include/config/auto.conf' is given, create 'include/config'.
920608182aSMasahiro Yamada  */
make_parent_dir(const char * path)930608182aSMasahiro Yamada static int make_parent_dir(const char *path)
940608182aSMasahiro Yamada {
950608182aSMasahiro Yamada 	char tmp[PATH_MAX + 1];
960608182aSMasahiro Yamada 	char *p;
970608182aSMasahiro Yamada 
980608182aSMasahiro Yamada 	strncpy(tmp, path, sizeof(tmp));
990608182aSMasahiro Yamada 	tmp[sizeof(tmp) - 1] = 0;
1000608182aSMasahiro Yamada 
1010608182aSMasahiro Yamada 	/* Remove the base name. Just return if nothing is left */
1020608182aSMasahiro Yamada 	p = strrchr(tmp, '/');
1030608182aSMasahiro Yamada 	if (!p)
1040608182aSMasahiro Yamada 		return 0;
1050608182aSMasahiro Yamada 	*(p + 1) = 0;
1060608182aSMasahiro Yamada 
1070608182aSMasahiro Yamada 	/* Just in case it is an absolute path */
1080608182aSMasahiro Yamada 	p = tmp;
1090608182aSMasahiro Yamada 	while (*p == '/')
1100608182aSMasahiro Yamada 		p++;
1110608182aSMasahiro Yamada 
1120608182aSMasahiro Yamada 	while ((p = strchr(p, '/'))) {
1130608182aSMasahiro Yamada 		*p = 0;
1140608182aSMasahiro Yamada 
1150608182aSMasahiro Yamada 		/* skip if the directory exists */
1160608182aSMasahiro Yamada 		if (!is_dir(tmp) && mkdir(tmp, 0755))
1170608182aSMasahiro Yamada 			return -1;
1180608182aSMasahiro Yamada 
1190608182aSMasahiro Yamada 		*p = '/';
1200608182aSMasahiro Yamada 		while (*p == '/')
1210608182aSMasahiro Yamada 			p++;
1220608182aSMasahiro Yamada 	}
1230608182aSMasahiro Yamada 
1240608182aSMasahiro Yamada 	return 0;
1250608182aSMasahiro Yamada }
1260608182aSMasahiro Yamada 
1271508fec8SMasahiro Yamada static char depfile_path[PATH_MAX];
1281508fec8SMasahiro Yamada static size_t depfile_prefix_len;
1291508fec8SMasahiro Yamada 
1301508fec8SMasahiro Yamada /* touch depfile for symbol 'name' */
conf_touch_dep(const char * name)1311508fec8SMasahiro Yamada static int conf_touch_dep(const char *name)
1321508fec8SMasahiro Yamada {
133fee762d6SMasahiro Yamada 	int fd;
1341508fec8SMasahiro Yamada 
1350e0345b7SAlexey Dobriyan 	/* check overflow: prefix + name + '\0' must fit in buffer. */
1360e0345b7SAlexey Dobriyan 	if (depfile_prefix_len + strlen(name) + 1 > sizeof(depfile_path))
1371508fec8SMasahiro Yamada 		return -1;
1381508fec8SMasahiro Yamada 
139fee762d6SMasahiro Yamada 	strcpy(depfile_path + depfile_prefix_len, name);
1401508fec8SMasahiro Yamada 
1411508fec8SMasahiro Yamada 	fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1421508fec8SMasahiro Yamada 	if (fd == -1)
1431508fec8SMasahiro Yamada 		return -1;
1441508fec8SMasahiro Yamada 	close(fd);
1451508fec8SMasahiro Yamada 
1461508fec8SMasahiro Yamada 	return 0;
1471508fec8SMasahiro Yamada }
1481508fec8SMasahiro Yamada 
149c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
150c1a0f5e3SRoman Zippel 	__attribute__ ((format (printf, 1, 2)));
151c1a0f5e3SRoman Zippel 
15242368c37SMichal Marek static void conf_message(const char *fmt, ...)
15342368c37SMichal Marek 	__attribute__ ((format (printf, 1, 2)));
15442368c37SMichal Marek 
155c1a0f5e3SRoman Zippel static const char *conf_filename;
15684dd95d4SMasahiro Yamada static int conf_lineno, conf_warnings;
157c1a0f5e3SRoman Zippel 
conf_warning(const char * fmt,...)158c1a0f5e3SRoman Zippel static void conf_warning(const char *fmt, ...)
159c1a0f5e3SRoman Zippel {
160c1a0f5e3SRoman Zippel 	va_list ap;
161c1a0f5e3SRoman Zippel 	va_start(ap, fmt);
162c1a0f5e3SRoman Zippel 	fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
163c1a0f5e3SRoman Zippel 	vfprintf(stderr, fmt, ap);
164c1a0f5e3SRoman Zippel 	fprintf(stderr, "\n");
165c1a0f5e3SRoman Zippel 	va_end(ap);
166c1a0f5e3SRoman Zippel 	conf_warnings++;
167c1a0f5e3SRoman Zippel }
168c1a0f5e3SRoman Zippel 
conf_default_message_callback(const char * s)1695accd7f3SMasahiro Yamada static void conf_default_message_callback(const char *s)
17042368c37SMichal Marek {
17142368c37SMichal Marek 	printf("#\n# ");
1725accd7f3SMasahiro Yamada 	printf("%s", s);
17342368c37SMichal Marek 	printf("\n#\n");
17442368c37SMichal Marek }
17542368c37SMichal Marek 
1765accd7f3SMasahiro Yamada static void (*conf_message_callback)(const char *s) =
17742368c37SMichal Marek 	conf_default_message_callback;
conf_set_message_callback(void (* fn)(const char * s))1785accd7f3SMasahiro Yamada void conf_set_message_callback(void (*fn)(const char *s))
17942368c37SMichal Marek {
18042368c37SMichal Marek 	conf_message_callback = fn;
18142368c37SMichal Marek }
18242368c37SMichal Marek 
conf_message(const char * fmt,...)18342368c37SMichal Marek static void conf_message(const char *fmt, ...)
18442368c37SMichal Marek {
18542368c37SMichal Marek 	va_list ap;
1865accd7f3SMasahiro Yamada 	char buf[4096];
1875accd7f3SMasahiro Yamada 
1885accd7f3SMasahiro Yamada 	if (!conf_message_callback)
1895accd7f3SMasahiro Yamada 		return;
19042368c37SMichal Marek 
19142368c37SMichal Marek 	va_start(ap, fmt);
1925accd7f3SMasahiro Yamada 
1935accd7f3SMasahiro Yamada 	vsnprintf(buf, sizeof(buf), fmt, ap);
1945accd7f3SMasahiro Yamada 	conf_message_callback(buf);
195b6a2ab2cSColin Ian King 	va_end(ap);
19642368c37SMichal Marek }
19742368c37SMichal Marek 
conf_get_configname(void)19814cdd3c4SRoman Zippel const char *conf_get_configname(void)
19914cdd3c4SRoman Zippel {
20014cdd3c4SRoman Zippel 	char *name = getenv("KCONFIG_CONFIG");
20114cdd3c4SRoman Zippel 
20214cdd3c4SRoman Zippel 	return name ? name : ".config";
20314cdd3c4SRoman Zippel }
20414cdd3c4SRoman Zippel 
conf_get_autoconfig_name(void)2059b9f5948SMasahiro Yamada static const char *conf_get_autoconfig_name(void)
20612122f62SMarkus Heidelberg {
20712122f62SMarkus Heidelberg 	char *name = getenv("KCONFIG_AUTOCONFIG");
20812122f62SMarkus Heidelberg 
20912122f62SMarkus Heidelberg 	return name ? name : "include/config/auto.conf";
21012122f62SMarkus Heidelberg }
21112122f62SMarkus Heidelberg 
conf_get_autoheader_name(void)2128499f2ddSMasahiro Yamada static const char *conf_get_autoheader_name(void)
2138499f2ddSMasahiro Yamada {
2148499f2ddSMasahiro Yamada 	char *name = getenv("KCONFIG_AUTOHEADER");
2158499f2ddSMasahiro Yamada 
2168499f2ddSMasahiro Yamada 	return name ? name : "include/generated/autoconf.h";
2178499f2ddSMasahiro Yamada }
2188499f2ddSMasahiro Yamada 
conf_get_rustccfg_name(void)2192f7ab126SMiguel Ojeda static const char *conf_get_rustccfg_name(void)
2202f7ab126SMiguel Ojeda {
2212f7ab126SMiguel Ojeda 	char *name = getenv("KCONFIG_RUSTCCFG");
2222f7ab126SMiguel Ojeda 
2232f7ab126SMiguel Ojeda 	return name ? name : "include/generated/rustc_cfg";
2242f7ab126SMiguel Ojeda }
2252f7ab126SMiguel Ojeda 
conf_set_sym_val(struct symbol * sym,int def,int def_flags,char * p)2269c900a9cSSam Ravnborg static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
2279c900a9cSSam Ravnborg {
2289c900a9cSSam Ravnborg 	char *p2;
2299c900a9cSSam Ravnborg 
2309c900a9cSSam Ravnborg 	switch (sym->type) {
2319c900a9cSSam Ravnborg 	case S_TRISTATE:
2329c900a9cSSam Ravnborg 		if (p[0] == 'm') {
2339c900a9cSSam Ravnborg 			sym->def[def].tri = mod;
2349c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2359c900a9cSSam Ravnborg 			break;
2369c900a9cSSam Ravnborg 		}
237d8fc3200SArnaud Lacombe 		/* fall through */
2389c900a9cSSam Ravnborg 	case S_BOOLEAN:
2399c900a9cSSam Ravnborg 		if (p[0] == 'y') {
2409c900a9cSSam Ravnborg 			sym->def[def].tri = yes;
2419c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2429c900a9cSSam Ravnborg 			break;
2439c900a9cSSam Ravnborg 		}
2449c900a9cSSam Ravnborg 		if (p[0] == 'n') {
2459c900a9cSSam Ravnborg 			sym->def[def].tri = no;
2469c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2479c900a9cSSam Ravnborg 			break;
2489c900a9cSSam Ravnborg 		}
24904b19b77SYann E. MORIN 		if (def != S_DEF_AUTO)
25004b19b77SYann E. MORIN 			conf_warning("symbol value '%s' invalid for %s",
25104b19b77SYann E. MORIN 				     p, sym->name);
25275f1468bSArnaud Lacombe 		return 1;
2539c900a9cSSam Ravnborg 	case S_STRING:
254129ab0d2SMasahiro Yamada 		/* No escaping for S_DEF_AUTO (include/config/auto.conf) */
255129ab0d2SMasahiro Yamada 		if (def != S_DEF_AUTO) {
2569c900a9cSSam Ravnborg 			if (*p++ != '"')
2579c900a9cSSam Ravnborg 				break;
2589c900a9cSSam Ravnborg 			for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
2599c900a9cSSam Ravnborg 				if (*p2 == '"') {
2609c900a9cSSam Ravnborg 					*p2 = 0;
2619c900a9cSSam Ravnborg 					break;
2629c900a9cSSam Ravnborg 				}
2639c900a9cSSam Ravnborg 				memmove(p2, p2 + 1, strlen(p2));
2649c900a9cSSam Ravnborg 			}
2659c900a9cSSam Ravnborg 			if (!p2) {
2669c900a9cSSam Ravnborg 				conf_warning("invalid string found");
2679c900a9cSSam Ravnborg 				return 1;
2689c900a9cSSam Ravnborg 			}
269129ab0d2SMasahiro Yamada 		}
270d8fc3200SArnaud Lacombe 		/* fall through */
2719c900a9cSSam Ravnborg 	case S_INT:
2729c900a9cSSam Ravnborg 	case S_HEX:
2739c900a9cSSam Ravnborg 		if (sym_string_valid(sym, p)) {
274cd81fc82SMasahiro Yamada 			sym->def[def].val = xstrdup(p);
2759c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2769c900a9cSSam Ravnborg 		} else {
27704b19b77SYann E. MORIN 			if (def != S_DEF_AUTO)
27804b19b77SYann E. MORIN 				conf_warning("symbol value '%s' invalid for %s",
27904b19b77SYann E. MORIN 					     p, sym->name);
2809c900a9cSSam Ravnborg 			return 1;
2819c900a9cSSam Ravnborg 		}
2829c900a9cSSam Ravnborg 		break;
2839c900a9cSSam Ravnborg 	default:
2849c900a9cSSam Ravnborg 		;
2859c900a9cSSam Ravnborg 	}
2869c900a9cSSam Ravnborg 	return 0;
2879c900a9cSSam Ravnborg }
2889c900a9cSSam Ravnborg 
2891a7a8c6fSCody Schafer #define LINE_GROWTH 16
add_byte(int c,char ** lineptr,size_t slen,size_t * n)2901a7a8c6fSCody Schafer static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
2911a7a8c6fSCody Schafer {
2921a7a8c6fSCody Schafer 	char *nline;
2931a7a8c6fSCody Schafer 	size_t new_size = slen + 1;
2941a7a8c6fSCody Schafer 	if (new_size > *n) {
2951a7a8c6fSCody Schafer 		new_size += LINE_GROWTH - 1;
2961a7a8c6fSCody Schafer 		new_size *= 2;
297d717f24dSMasahiro Yamada 		nline = xrealloc(*lineptr, new_size);
2981a7a8c6fSCody Schafer 		if (!nline)
2991a7a8c6fSCody Schafer 			return -1;
3001a7a8c6fSCody Schafer 
3011a7a8c6fSCody Schafer 		*lineptr = nline;
3021a7a8c6fSCody Schafer 		*n = new_size;
3031a7a8c6fSCody Schafer 	}
3041a7a8c6fSCody Schafer 
3051a7a8c6fSCody Schafer 	(*lineptr)[slen] = c;
3061a7a8c6fSCody Schafer 
3071a7a8c6fSCody Schafer 	return 0;
3081a7a8c6fSCody Schafer }
3091a7a8c6fSCody Schafer 
compat_getline(char ** lineptr,size_t * n,FILE * stream)3101a7a8c6fSCody Schafer static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
3111a7a8c6fSCody Schafer {
3121a7a8c6fSCody Schafer 	char *line = *lineptr;
3131a7a8c6fSCody Schafer 	size_t slen = 0;
3141a7a8c6fSCody Schafer 
3151a7a8c6fSCody Schafer 	for (;;) {
3161a7a8c6fSCody Schafer 		int c = getc(stream);
3171a7a8c6fSCody Schafer 
3181a7a8c6fSCody Schafer 		switch (c) {
3191a7a8c6fSCody Schafer 		case '\n':
3201a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
3211a7a8c6fSCody Schafer 				goto e_out;
3221a7a8c6fSCody Schafer 			slen++;
3231a7a8c6fSCody Schafer 			/* fall through */
3241a7a8c6fSCody Schafer 		case EOF:
3251a7a8c6fSCody Schafer 			if (add_byte('\0', &line, slen, n) < 0)
3261a7a8c6fSCody Schafer 				goto e_out;
3271a7a8c6fSCody Schafer 			*lineptr = line;
3281a7a8c6fSCody Schafer 			if (slen == 0)
3291a7a8c6fSCody Schafer 				return -1;
3301a7a8c6fSCody Schafer 			return slen;
3311a7a8c6fSCody Schafer 		default:
3321a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
3331a7a8c6fSCody Schafer 				goto e_out;
3341a7a8c6fSCody Schafer 			slen++;
3351a7a8c6fSCody Schafer 		}
3361a7a8c6fSCody Schafer 	}
3371a7a8c6fSCody Schafer 
3381a7a8c6fSCody Schafer e_out:
3391a7a8c6fSCody Schafer 	line[slen-1] = '\0';
3401a7a8c6fSCody Schafer 	*lineptr = line;
3411a7a8c6fSCody Schafer 	return -1;
3421a7a8c6fSCody Schafer }
3431a7a8c6fSCody Schafer 
conf_read_simple(const char * name,int def)344669bfad9SRoman Zippel int conf_read_simple(const char *name, int def)
3451da177e4SLinus Torvalds {
3461da177e4SLinus Torvalds 	FILE *in = NULL;
3471a7a8c6fSCody Schafer 	char   *line = NULL;
3481a7a8c6fSCody Schafer 	size_t  line_asize = 0;
3491da177e4SLinus Torvalds 	char *p, *p2;
3501da177e4SLinus Torvalds 	struct symbol *sym;
351669bfad9SRoman Zippel 	int i, def_flags;
352*7cd34300SSergey Senozhatsky 	const char *warn_unknown;
353*7cd34300SSergey Senozhatsky 	const char *werror;
3541da177e4SLinus Torvalds 
355*7cd34300SSergey Senozhatsky 	warn_unknown = getenv("KCONFIG_WARN_UNKNOWN_SYMBOLS");
356*7cd34300SSergey Senozhatsky 	werror = getenv("KCONFIG_WERROR");
3571da177e4SLinus Torvalds 	if (name) {
3581da177e4SLinus Torvalds 		in = zconf_fopen(name);
3591da177e4SLinus Torvalds 	} else {
360b75b0a81SMasahiro Yamada 		char *env;
361face4374SRoman Zippel 
36214cdd3c4SRoman Zippel 		name = conf_get_configname();
363ddc97cacSRoman Zippel 		in = zconf_fopen(name);
364ddc97cacSRoman Zippel 		if (in)
365ddc97cacSRoman Zippel 			goto load;
3665ee54659SMasahiro Yamada 		conf_set_changed(true);
367b75b0a81SMasahiro Yamada 
368b75b0a81SMasahiro Yamada 		env = getenv("KCONFIG_DEFCONFIG_LIST");
369b75b0a81SMasahiro Yamada 		if (!env)
370face4374SRoman Zippel 			return 1;
371face4374SRoman Zippel 
372b75b0a81SMasahiro Yamada 		while (1) {
373b75b0a81SMasahiro Yamada 			bool is_last;
374b75b0a81SMasahiro Yamada 
375b75b0a81SMasahiro Yamada 			while (isspace(*env))
376b75b0a81SMasahiro Yamada 				env++;
377b75b0a81SMasahiro Yamada 
378b75b0a81SMasahiro Yamada 			if (!*env)
379b75b0a81SMasahiro Yamada 				break;
380b75b0a81SMasahiro Yamada 
381b75b0a81SMasahiro Yamada 			p = env;
382b75b0a81SMasahiro Yamada 			while (*p && !isspace(*p))
383b75b0a81SMasahiro Yamada 				p++;
384b75b0a81SMasahiro Yamada 
385b75b0a81SMasahiro Yamada 			is_last = (*p == '\0');
386b75b0a81SMasahiro Yamada 
387b75b0a81SMasahiro Yamada 			*p = '\0';
388b75b0a81SMasahiro Yamada 
389b75b0a81SMasahiro Yamada 			in = zconf_fopen(env);
3901da177e4SLinus Torvalds 			if (in) {
391694c49a7SSam Ravnborg 				conf_message("using defaults found in %s",
392b75b0a81SMasahiro Yamada 					     env);
393ddc97cacSRoman Zippel 				goto load;
3941da177e4SLinus Torvalds 			}
395b75b0a81SMasahiro Yamada 
396b75b0a81SMasahiro Yamada 			if (is_last)
397b75b0a81SMasahiro Yamada 				break;
398b75b0a81SMasahiro Yamada 
399b75b0a81SMasahiro Yamada 			env = p + 1;
4001da177e4SLinus Torvalds 		}
4011da177e4SLinus Torvalds 	}
4021da177e4SLinus Torvalds 	if (!in)
4031da177e4SLinus Torvalds 		return 1;
4041da177e4SLinus Torvalds 
405ddc97cacSRoman Zippel load:
406c1a0f5e3SRoman Zippel 	conf_filename = name;
407c1a0f5e3SRoman Zippel 	conf_lineno = 0;
408c1a0f5e3SRoman Zippel 	conf_warnings = 0;
409c1a0f5e3SRoman Zippel 
410669bfad9SRoman Zippel 	def_flags = SYMBOL_DEF << def;
4111da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
412669bfad9SRoman Zippel 		sym->flags |= SYMBOL_CHANGED;
413669bfad9SRoman Zippel 		sym->flags &= ~(def_flags|SYMBOL_VALID);
414490f1617SYann E. MORIN 		if (sym_is_choice(sym))
415490f1617SYann E. MORIN 			sym->flags |= def_flags;
4161da177e4SLinus Torvalds 		switch (sym->type) {
4171da177e4SLinus Torvalds 		case S_INT:
4181da177e4SLinus Torvalds 		case S_HEX:
4191da177e4SLinus Torvalds 		case S_STRING:
420669bfad9SRoman Zippel 			if (sym->def[def].val)
421669bfad9SRoman Zippel 				free(sym->def[def].val);
422d8fc3200SArnaud Lacombe 			/* fall through */
4231da177e4SLinus Torvalds 		default:
424669bfad9SRoman Zippel 			sym->def[def].val = NULL;
425669bfad9SRoman Zippel 			sym->def[def].tri = no;
4261da177e4SLinus Torvalds 		}
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds 
4291a7a8c6fSCody Schafer 	while (compat_getline(&line, &line_asize, in) != -1) {
430c1a0f5e3SRoman Zippel 		conf_lineno++;
4311da177e4SLinus Torvalds 		sym = NULL;
4328baefd30SArnaud Lacombe 		if (line[0] == '#') {
433ffb5957bSArnaud Lacombe 			if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
4341da177e4SLinus Torvalds 				continue;
435ffb5957bSArnaud Lacombe 			p = strchr(line + 2 + strlen(CONFIG_), ' ');
4361da177e4SLinus Torvalds 			if (!p)
4371da177e4SLinus Torvalds 				continue;
4381da177e4SLinus Torvalds 			*p++ = 0;
4391da177e4SLinus Torvalds 			if (strncmp(p, "is not set", 10))
4401da177e4SLinus Torvalds 				continue;
441669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
442ffb5957bSArnaud Lacombe 				sym = sym_find(line + 2 + strlen(CONFIG_));
443661b0680Szippel@linux-m68k.org 				if (!sym) {
444*7cd34300SSergey Senozhatsky 					if (warn_unknown)
445*7cd34300SSergey Senozhatsky 						conf_warning("unknown symbol: %s",
446*7cd34300SSergey Senozhatsky 							     line + 2 + strlen(CONFIG_));
447*7cd34300SSergey Senozhatsky 
4485ee54659SMasahiro Yamada 					conf_set_changed(true);
44975889e9bSMasahiro Yamada 					continue;
450661b0680Szippel@linux-m68k.org 				}
451669bfad9SRoman Zippel 			} else {
452ffb5957bSArnaud Lacombe 				sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
453669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
454669bfad9SRoman Zippel 					sym->type = S_BOOLEAN;
455669bfad9SRoman Zippel 			}
456669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
457d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
4581da177e4SLinus Torvalds 			}
4591da177e4SLinus Torvalds 			switch (sym->type) {
4601da177e4SLinus Torvalds 			case S_BOOLEAN:
4611da177e4SLinus Torvalds 			case S_TRISTATE:
462669bfad9SRoman Zippel 				sym->def[def].tri = no;
463669bfad9SRoman Zippel 				sym->flags |= def_flags;
4641da177e4SLinus Torvalds 				break;
4651da177e4SLinus Torvalds 			default:
4661da177e4SLinus Torvalds 				;
4671da177e4SLinus Torvalds 			}
468ffb5957bSArnaud Lacombe 		} else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
469ffb5957bSArnaud Lacombe 			p = strchr(line + strlen(CONFIG_), '=');
4701da177e4SLinus Torvalds 			if (!p)
4711da177e4SLinus Torvalds 				continue;
4721da177e4SLinus Torvalds 			*p++ = 0;
4731da177e4SLinus Torvalds 			p2 = strchr(p, '\n');
474d3660a8cSMatthew Wilcox 			if (p2) {
475d3660a8cSMatthew Wilcox 				*p2-- = 0;
476d3660a8cSMatthew Wilcox 				if (*p2 == '\r')
4771da177e4SLinus Torvalds 					*p2 = 0;
478d3660a8cSMatthew Wilcox 			}
4792aabbed6SMasahiro Yamada 
480ffb5957bSArnaud Lacombe 			sym = sym_find(line + strlen(CONFIG_));
481661b0680Szippel@linux-m68k.org 			if (!sym) {
482*7cd34300SSergey Senozhatsky 				if (def == S_DEF_AUTO) {
4832aabbed6SMasahiro Yamada 					/*
4842aabbed6SMasahiro Yamada 					 * Reading from include/config/auto.conf
4852aabbed6SMasahiro Yamada 					 * If CONFIG_FOO previously existed in
4862aabbed6SMasahiro Yamada 					 * auto.conf but it is missing now,
4870e0345b7SAlexey Dobriyan 					 * include/config/FOO must be touched.
4882aabbed6SMasahiro Yamada 					 */
4892aabbed6SMasahiro Yamada 					conf_touch_dep(line + strlen(CONFIG_));
490*7cd34300SSergey Senozhatsky 				} else {
491*7cd34300SSergey Senozhatsky 					if (warn_unknown)
492*7cd34300SSergey Senozhatsky 						conf_warning("unknown symbol: %s",
493*7cd34300SSergey Senozhatsky 							     line + strlen(CONFIG_));
494*7cd34300SSergey Senozhatsky 
4955ee54659SMasahiro Yamada 					conf_set_changed(true);
496*7cd34300SSergey Senozhatsky 				}
49775889e9bSMasahiro Yamada 				continue;
498661b0680Szippel@linux-m68k.org 			}
4992aabbed6SMasahiro Yamada 
500669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
501d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
5021da177e4SLinus Torvalds 			}
5039c900a9cSSam Ravnborg 			if (conf_set_sym_val(sym, def, def_flags, p))
504c1a0f5e3SRoman Zippel 				continue;
5058baefd30SArnaud Lacombe 		} else {
5068baefd30SArnaud Lacombe 			if (line[0] != '\r' && line[0] != '\n')
507a4663911SPaul Bolle 				conf_warning("unexpected data: %.*s",
508a4663911SPaul Bolle 					     (int)strcspn(line, "\r\n"), line);
509a4663911SPaul Bolle 
5101da177e4SLinus Torvalds 			continue;
5111da177e4SLinus Torvalds 		}
51275889e9bSMasahiro Yamada 
5131da177e4SLinus Torvalds 		if (sym && sym_is_choice_value(sym)) {
5141da177e4SLinus Torvalds 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
515669bfad9SRoman Zippel 			switch (sym->def[def].tri) {
5161da177e4SLinus Torvalds 			case no:
5171da177e4SLinus Torvalds 				break;
5181da177e4SLinus Torvalds 			case mod:
519669bfad9SRoman Zippel 				if (cs->def[def].tri == yes) {
520c1a0f5e3SRoman Zippel 					conf_warning("%s creates inconsistent choice state", sym->name);
521490f1617SYann E. MORIN 					cs->flags &= ~def_flags;
522c1a0f5e3SRoman Zippel 				}
5231da177e4SLinus Torvalds 				break;
5241da177e4SLinus Torvalds 			case yes:
525d84876f9SJan Engelhardt 				if (cs->def[def].tri != no)
526d84876f9SJan Engelhardt 					conf_warning("override: %s changes choice state", sym->name);
527669bfad9SRoman Zippel 				cs->def[def].val = sym;
5281da177e4SLinus Torvalds 				break;
5291da177e4SLinus Torvalds 			}
530d6ee3576SSam Ravnborg 			cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
5311da177e4SLinus Torvalds 		}
5321da177e4SLinus Torvalds 	}
5331a7a8c6fSCody Schafer 	free(line);
5341da177e4SLinus Torvalds 	fclose(in);
535*7cd34300SSergey Senozhatsky 
536*7cd34300SSergey Senozhatsky 	if (conf_warnings && werror)
537*7cd34300SSergey Senozhatsky 		exit(1);
538*7cd34300SSergey Senozhatsky 
53990389160SRoman Zippel 	return 0;
54090389160SRoman Zippel }
54190389160SRoman Zippel 
conf_read(const char * name)54290389160SRoman Zippel int conf_read(const char *name)
54390389160SRoman Zippel {
5445d09598dSArnaud Lacombe 	struct symbol *sym;
54584dd95d4SMasahiro Yamada 	int conf_unsaved = 0;
5465d09598dSArnaud Lacombe 	int i;
54790389160SRoman Zippel 
5485ee54659SMasahiro Yamada 	conf_set_changed(false);
549ddc97cacSRoman Zippel 
5506b87b70cSAl Viro 	if (conf_read_simple(name, S_DEF_USER)) {
5516b87b70cSAl Viro 		sym_calc_value(modules_sym);
55290389160SRoman Zippel 		return 1;
5536b87b70cSAl Viro 	}
5546b87b70cSAl Viro 
5556b87b70cSAl Viro 	sym_calc_value(modules_sym);
55690389160SRoman Zippel 
5571da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
5581da177e4SLinus Torvalds 		sym_calc_value(sym);
559693359f7SDirk Gouders 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
5605d09598dSArnaud Lacombe 			continue;
561c1a0f5e3SRoman Zippel 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
562c1a0f5e3SRoman Zippel 			/* check that calculated value agrees with saved value */
563c1a0f5e3SRoman Zippel 			switch (sym->type) {
564c1a0f5e3SRoman Zippel 			case S_BOOLEAN:
565c1a0f5e3SRoman Zippel 			case S_TRISTATE:
566e3cd5136SMasahiro Yamada 				if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
5675d09598dSArnaud Lacombe 					continue;
568e3cd5136SMasahiro Yamada 				break;
569c1a0f5e3SRoman Zippel 			default:
5700c1822e6SRoman Zippel 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
5715d09598dSArnaud Lacombe 					continue;
572c1a0f5e3SRoman Zippel 				break;
573c1a0f5e3SRoman Zippel 			}
574c1a0f5e3SRoman Zippel 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
575c1a0f5e3SRoman Zippel 			/* no previous value and not saved */
5765d09598dSArnaud Lacombe 			continue;
577c1a0f5e3SRoman Zippel 		conf_unsaved++;
578c1a0f5e3SRoman Zippel 		/* maybe print value in verbose mode... */
5791da177e4SLinus Torvalds 	}
5801da177e4SLinus Torvalds 
581d8982ba1SRoman Zippel 	for_all_symbols(i, sym) {
582d8982ba1SRoman Zippel 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
583d8982ba1SRoman Zippel 			/* Reset values of generates values, so they'll appear
584d8982ba1SRoman Zippel 			 * as new, if they should become visible, but that
585d8982ba1SRoman Zippel 			 * doesn't quite work if the Kconfig and the saved
586d8982ba1SRoman Zippel 			 * configuration disagree.
587d8982ba1SRoman Zippel 			 */
588d8982ba1SRoman Zippel 			if (sym->visible == no && !conf_unsaved)
589d8982ba1SRoman Zippel 				sym->flags &= ~SYMBOL_DEF_USER;
590d8982ba1SRoman Zippel 			switch (sym->type) {
591d8982ba1SRoman Zippel 			case S_STRING:
592d8982ba1SRoman Zippel 			case S_INT:
593d8982ba1SRoman Zippel 			case S_HEX:
594d8982ba1SRoman Zippel 				/* Reset a string value if it's out of range */
595d8982ba1SRoman Zippel 				if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
596d8982ba1SRoman Zippel 					break;
597d8982ba1SRoman Zippel 				sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
598d8982ba1SRoman Zippel 				conf_unsaved++;
599d8982ba1SRoman Zippel 				break;
600d8982ba1SRoman Zippel 			default:
601d8982ba1SRoman Zippel 				break;
602d8982ba1SRoman Zippel 			}
603d8982ba1SRoman Zippel 		}
604d8982ba1SRoman Zippel 	}
605d8982ba1SRoman Zippel 
6065ee54659SMasahiro Yamada 	if (conf_warnings || conf_unsaved)
6075ee54659SMasahiro Yamada 		conf_set_changed(true);
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	return 0;
6101da177e4SLinus Torvalds }
6111da177e4SLinus Torvalds 
612ca51b26bSMasahiro Yamada struct comment_style {
613ca51b26bSMasahiro Yamada 	const char *decoration;
614ca51b26bSMasahiro Yamada 	const char *prefix;
615ca51b26bSMasahiro Yamada 	const char *postfix;
616ca51b26bSMasahiro Yamada };
617ca51b26bSMasahiro Yamada 
618ca51b26bSMasahiro Yamada static const struct comment_style comment_style_pound = {
619ca51b26bSMasahiro Yamada 	.decoration = "#",
620ca51b26bSMasahiro Yamada 	.prefix = "#",
621ca51b26bSMasahiro Yamada 	.postfix = "#",
622ca51b26bSMasahiro Yamada };
623ca51b26bSMasahiro Yamada 
624ca51b26bSMasahiro Yamada static const struct comment_style comment_style_c = {
625ca51b26bSMasahiro Yamada 	.decoration = " *",
626ca51b26bSMasahiro Yamada 	.prefix = "/*",
627ca51b26bSMasahiro Yamada 	.postfix = " */",
628ca51b26bSMasahiro Yamada };
629ca51b26bSMasahiro Yamada 
conf_write_heading(FILE * fp,const struct comment_style * cs)630ca51b26bSMasahiro Yamada static void conf_write_heading(FILE *fp, const struct comment_style *cs)
631ca51b26bSMasahiro Yamada {
6322f7ab126SMiguel Ojeda 	if (!cs)
6332f7ab126SMiguel Ojeda 		return;
6342f7ab126SMiguel Ojeda 
635ca51b26bSMasahiro Yamada 	fprintf(fp, "%s\n", cs->prefix);
636ca51b26bSMasahiro Yamada 
637ca51b26bSMasahiro Yamada 	fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n",
638ca51b26bSMasahiro Yamada 		cs->decoration);
639ca51b26bSMasahiro Yamada 
640ca51b26bSMasahiro Yamada 	fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text);
641ca51b26bSMasahiro Yamada 
642ca51b26bSMasahiro Yamada 	fprintf(fp, "%s\n", cs->postfix);
643ca51b26bSMasahiro Yamada }
644ca51b26bSMasahiro Yamada 
64580f7bc77SMasahiro Yamada /* The returned pointer must be freed on the caller side */
escape_string_value(const char * in)64680f7bc77SMasahiro Yamada static char *escape_string_value(const char *in)
64780f7bc77SMasahiro Yamada {
64880f7bc77SMasahiro Yamada 	const char *p;
64980f7bc77SMasahiro Yamada 	char *out;
65080f7bc77SMasahiro Yamada 	size_t len;
65180f7bc77SMasahiro Yamada 
65280f7bc77SMasahiro Yamada 	len = strlen(in) + strlen("\"\"") + 1;
65380f7bc77SMasahiro Yamada 
65480f7bc77SMasahiro Yamada 	p = in;
65580f7bc77SMasahiro Yamada 	while (1) {
65680f7bc77SMasahiro Yamada 		p += strcspn(p, "\"\\");
65780f7bc77SMasahiro Yamada 
65880f7bc77SMasahiro Yamada 		if (p[0] == '\0')
65980f7bc77SMasahiro Yamada 			break;
66080f7bc77SMasahiro Yamada 
66180f7bc77SMasahiro Yamada 		len++;
66280f7bc77SMasahiro Yamada 		p++;
66380f7bc77SMasahiro Yamada 	}
66480f7bc77SMasahiro Yamada 
66580f7bc77SMasahiro Yamada 	out = xmalloc(len);
66680f7bc77SMasahiro Yamada 	out[0] = '\0';
66780f7bc77SMasahiro Yamada 
66880f7bc77SMasahiro Yamada 	strcat(out, "\"");
66980f7bc77SMasahiro Yamada 
67080f7bc77SMasahiro Yamada 	p = in;
67180f7bc77SMasahiro Yamada 	while (1) {
67280f7bc77SMasahiro Yamada 		len = strcspn(p, "\"\\");
67380f7bc77SMasahiro Yamada 		strncat(out, p, len);
67480f7bc77SMasahiro Yamada 		p += len;
67580f7bc77SMasahiro Yamada 
67680f7bc77SMasahiro Yamada 		if (p[0] == '\0')
67780f7bc77SMasahiro Yamada 			break;
67880f7bc77SMasahiro Yamada 
67980f7bc77SMasahiro Yamada 		strcat(out, "\\");
68080f7bc77SMasahiro Yamada 		strncat(out, p++, 1);
68180f7bc77SMasahiro Yamada 	}
68280f7bc77SMasahiro Yamada 
68380f7bc77SMasahiro Yamada 	strcat(out, "\"");
68480f7bc77SMasahiro Yamada 
68580f7bc77SMasahiro Yamada 	return out;
68680f7bc77SMasahiro Yamada }
68780f7bc77SMasahiro Yamada 
6886ce45a91SMasahiro Yamada enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
68949192f26SSam Ravnborg 
__print_symbol(FILE * fp,struct symbol * sym,enum output_n output_n,bool escape_string)6906ce45a91SMasahiro Yamada static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
6916ce45a91SMasahiro Yamada 			   bool escape_string)
692e54e692bSArnaud Lacombe {
693229d0cfaSMasahiro Yamada 	const char *val;
694229d0cfaSMasahiro Yamada 	char *escaped = NULL;
695e54e692bSArnaud Lacombe 
696229d0cfaSMasahiro Yamada 	if (sym->type == S_UNKNOWN)
697229d0cfaSMasahiro Yamada 		return;
698229d0cfaSMasahiro Yamada 
699229d0cfaSMasahiro Yamada 	val = sym_get_string_value(sym);
700229d0cfaSMasahiro Yamada 
7016ce45a91SMasahiro Yamada 	if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
7026ce45a91SMasahiro Yamada 	    output_n != OUTPUT_N && *val == 'n') {
7036ce45a91SMasahiro Yamada 		if (output_n == OUTPUT_N_AS_UNSET)
7046ce45a91SMasahiro Yamada 			fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
7056ce45a91SMasahiro Yamada 		return;
7066ce45a91SMasahiro Yamada 	}
7076ce45a91SMasahiro Yamada 
7086ce45a91SMasahiro Yamada 	if (sym->type == S_STRING && escape_string) {
70980f7bc77SMasahiro Yamada 		escaped = escape_string_value(val);
710229d0cfaSMasahiro Yamada 		val = escaped;
71149192f26SSam Ravnborg 	}
712229d0cfaSMasahiro Yamada 
7136ce45a91SMasahiro Yamada 	fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
7146ce45a91SMasahiro Yamada 
7156ce45a91SMasahiro Yamada 	free(escaped);
7166ce45a91SMasahiro Yamada }
7176ce45a91SMasahiro Yamada 
print_symbol_for_dotconfig(FILE * fp,struct symbol * sym)7186ce45a91SMasahiro Yamada static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
7196ce45a91SMasahiro Yamada {
7206ce45a91SMasahiro Yamada 	__print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
7216ce45a91SMasahiro Yamada }
7226ce45a91SMasahiro Yamada 
print_symbol_for_autoconf(FILE * fp,struct symbol * sym)7236ce45a91SMasahiro Yamada static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
7246ce45a91SMasahiro Yamada {
725129ab0d2SMasahiro Yamada 	__print_symbol(fp, sym, OUTPUT_N_NONE, false);
7266ce45a91SMasahiro Yamada }
7276ce45a91SMasahiro Yamada 
print_symbol_for_listconfig(struct symbol * sym)72851d792cbSMasahiro Yamada void print_symbol_for_listconfig(struct symbol *sym)
72951d792cbSMasahiro Yamada {
73051d792cbSMasahiro Yamada 	__print_symbol(stdout, sym, OUTPUT_N, true);
73151d792cbSMasahiro Yamada }
73251d792cbSMasahiro Yamada 
print_symbol_for_c(FILE * fp,struct symbol * sym)7336ce45a91SMasahiro Yamada static void print_symbol_for_c(FILE *fp, struct symbol *sym)
7346ce45a91SMasahiro Yamada {
7356ce45a91SMasahiro Yamada 	const char *val;
7366ce45a91SMasahiro Yamada 	const char *sym_suffix = "";
7376ce45a91SMasahiro Yamada 	const char *val_prefix = "";
7386ce45a91SMasahiro Yamada 	char *escaped = NULL;
7396ce45a91SMasahiro Yamada 
7406ce45a91SMasahiro Yamada 	if (sym->type == S_UNKNOWN)
7416ce45a91SMasahiro Yamada 		return;
7426ce45a91SMasahiro Yamada 
7436ce45a91SMasahiro Yamada 	val = sym_get_string_value(sym);
7446ce45a91SMasahiro Yamada 
7456ce45a91SMasahiro Yamada 	switch (sym->type) {
7466ce45a91SMasahiro Yamada 	case S_BOOLEAN:
7476ce45a91SMasahiro Yamada 	case S_TRISTATE:
7486ce45a91SMasahiro Yamada 		switch (*val) {
7496ce45a91SMasahiro Yamada 		case 'n':
7506ce45a91SMasahiro Yamada 			return;
7516ce45a91SMasahiro Yamada 		case 'm':
7526ce45a91SMasahiro Yamada 			sym_suffix = "_MODULE";
7536ce45a91SMasahiro Yamada 			/* fall through */
7546ce45a91SMasahiro Yamada 		default:
7556ce45a91SMasahiro Yamada 			val = "1";
7566ce45a91SMasahiro Yamada 		}
7576ce45a91SMasahiro Yamada 		break;
7586ce45a91SMasahiro Yamada 	case S_HEX:
7596ce45a91SMasahiro Yamada 		if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
7606ce45a91SMasahiro Yamada 			val_prefix = "0x";
7616ce45a91SMasahiro Yamada 		break;
7626ce45a91SMasahiro Yamada 	case S_STRING:
76380f7bc77SMasahiro Yamada 		escaped = escape_string_value(val);
7646ce45a91SMasahiro Yamada 		val = escaped;
7656ce45a91SMasahiro Yamada 	default:
7666ce45a91SMasahiro Yamada 		break;
7676ce45a91SMasahiro Yamada 	}
7686ce45a91SMasahiro Yamada 
7696ce45a91SMasahiro Yamada 	fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
7706ce45a91SMasahiro Yamada 		val_prefix, val);
771229d0cfaSMasahiro Yamada 
772229d0cfaSMasahiro Yamada 	free(escaped);
77349192f26SSam Ravnborg }
77449192f26SSam Ravnborg 
print_symbol_for_rustccfg(FILE * fp,struct symbol * sym)7752f7ab126SMiguel Ojeda static void print_symbol_for_rustccfg(FILE *fp, struct symbol *sym)
7762f7ab126SMiguel Ojeda {
7772f7ab126SMiguel Ojeda 	const char *val;
7782f7ab126SMiguel Ojeda 	const char *val_prefix = "";
7792f7ab126SMiguel Ojeda 	char *val_prefixed = NULL;
7802f7ab126SMiguel Ojeda 	size_t val_prefixed_len;
7812f7ab126SMiguel Ojeda 	char *escaped = NULL;
7822f7ab126SMiguel Ojeda 
7832f7ab126SMiguel Ojeda 	if (sym->type == S_UNKNOWN)
7842f7ab126SMiguel Ojeda 		return;
7852f7ab126SMiguel Ojeda 
7862f7ab126SMiguel Ojeda 	val = sym_get_string_value(sym);
7872f7ab126SMiguel Ojeda 
7882f7ab126SMiguel Ojeda 	switch (sym->type) {
7892f7ab126SMiguel Ojeda 	case S_BOOLEAN:
7902f7ab126SMiguel Ojeda 	case S_TRISTATE:
7912f7ab126SMiguel Ojeda 		/*
7922f7ab126SMiguel Ojeda 		 * We do not care about disabled ones, i.e. no need for
7932f7ab126SMiguel Ojeda 		 * what otherwise are "comments" in other printers.
7942f7ab126SMiguel Ojeda 		 */
7952f7ab126SMiguel Ojeda 		if (*val == 'n')
7962f7ab126SMiguel Ojeda 			return;
7972f7ab126SMiguel Ojeda 
7982f7ab126SMiguel Ojeda 		/*
7992f7ab126SMiguel Ojeda 		 * To have similar functionality to the C macro `IS_ENABLED()`
8002f7ab126SMiguel Ojeda 		 * we provide an empty `--cfg CONFIG_X` here in both `y`
8012f7ab126SMiguel Ojeda 		 * and `m` cases.
8022f7ab126SMiguel Ojeda 		 *
8032f7ab126SMiguel Ojeda 		 * Then, the common `fprintf()` below will also give us
8042f7ab126SMiguel Ojeda 		 * a `--cfg CONFIG_X="y"` or `--cfg CONFIG_X="m"`, which can
8052f7ab126SMiguel Ojeda 		 * be used as the equivalent of `IS_BUILTIN()`/`IS_MODULE()`.
8062f7ab126SMiguel Ojeda 		 */
8072f7ab126SMiguel Ojeda 		fprintf(fp, "--cfg=%s%s\n", CONFIG_, sym->name);
8082f7ab126SMiguel Ojeda 		break;
8092f7ab126SMiguel Ojeda 	case S_HEX:
8102f7ab126SMiguel Ojeda 		if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
8112f7ab126SMiguel Ojeda 			val_prefix = "0x";
8122f7ab126SMiguel Ojeda 		break;
8132f7ab126SMiguel Ojeda 	default:
8142f7ab126SMiguel Ojeda 		break;
8152f7ab126SMiguel Ojeda 	}
8162f7ab126SMiguel Ojeda 
8172f7ab126SMiguel Ojeda 	if (strlen(val_prefix) > 0) {
8182f7ab126SMiguel Ojeda 		val_prefixed_len = strlen(val) + strlen(val_prefix) + 1;
8192f7ab126SMiguel Ojeda 		val_prefixed = xmalloc(val_prefixed_len);
8202f7ab126SMiguel Ojeda 		snprintf(val_prefixed, val_prefixed_len, "%s%s", val_prefix, val);
8212f7ab126SMiguel Ojeda 		val = val_prefixed;
8222f7ab126SMiguel Ojeda 	}
8232f7ab126SMiguel Ojeda 
8242f7ab126SMiguel Ojeda 	/* All values get escaped: the `--cfg` option only takes strings */
8252f7ab126SMiguel Ojeda 	escaped = escape_string_value(val);
8262f7ab126SMiguel Ojeda 	val = escaped;
8272f7ab126SMiguel Ojeda 
8282f7ab126SMiguel Ojeda 	fprintf(fp, "--cfg=%s%s=%s\n", CONFIG_, sym->name, val);
8292f7ab126SMiguel Ojeda 
8302f7ab126SMiguel Ojeda 	free(escaped);
8312f7ab126SMiguel Ojeda 	free(val_prefixed);
8322f7ab126SMiguel Ojeda }
8332f7ab126SMiguel Ojeda 
8347cf3d73bSSam Ravnborg /*
8357cf3d73bSSam Ravnborg  * Write out a minimal config.
8367cf3d73bSSam Ravnborg  * All values that has default values are skipped as this is redundant.
8377cf3d73bSSam Ravnborg  */
conf_write_defconfig(const char * filename)8387cf3d73bSSam Ravnborg int conf_write_defconfig(const char *filename)
8397cf3d73bSSam Ravnborg {
8407cf3d73bSSam Ravnborg 	struct symbol *sym;
8417cf3d73bSSam Ravnborg 	struct menu *menu;
8427cf3d73bSSam Ravnborg 	FILE *out;
8437cf3d73bSSam Ravnborg 
8447cf3d73bSSam Ravnborg 	out = fopen(filename, "w");
8457cf3d73bSSam Ravnborg 	if (!out)
8467cf3d73bSSam Ravnborg 		return 1;
8477cf3d73bSSam Ravnborg 
8487cf3d73bSSam Ravnborg 	sym_clear_all_valid();
8497cf3d73bSSam Ravnborg 
8507cf3d73bSSam Ravnborg 	/* Traverse all menus to find all relevant symbols */
8517cf3d73bSSam Ravnborg 	menu = rootmenu.list;
8527cf3d73bSSam Ravnborg 
8537cf3d73bSSam Ravnborg 	while (menu != NULL)
8547cf3d73bSSam Ravnborg 	{
8557cf3d73bSSam Ravnborg 		sym = menu->sym;
8567cf3d73bSSam Ravnborg 		if (sym == NULL) {
8577cf3d73bSSam Ravnborg 			if (!menu_is_visible(menu))
8587cf3d73bSSam Ravnborg 				goto next_menu;
8597cf3d73bSSam Ravnborg 		} else if (!sym_is_choice(sym)) {
8607cf3d73bSSam Ravnborg 			sym_calc_value(sym);
8617cf3d73bSSam Ravnborg 			if (!(sym->flags & SYMBOL_WRITE))
8627cf3d73bSSam Ravnborg 				goto next_menu;
8637cf3d73bSSam Ravnborg 			sym->flags &= ~SYMBOL_WRITE;
8647cf3d73bSSam Ravnborg 			/* If we cannot change the symbol - skip */
865baa23ec8SMarco Ammon 			if (!sym_is_changeable(sym))
8667cf3d73bSSam Ravnborg 				goto next_menu;
8677cf3d73bSSam Ravnborg 			/* If symbol equals to default value - skip */
8687cf3d73bSSam Ravnborg 			if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
8697cf3d73bSSam Ravnborg 				goto next_menu;
8707cf3d73bSSam Ravnborg 
8717cf3d73bSSam Ravnborg 			/*
8727cf3d73bSSam Ravnborg 			 * If symbol is a choice value and equals to the
8737cf3d73bSSam Ravnborg 			 * default for a choice - skip.
87484062dd3SSam Ravnborg 			 * But only if value is bool and equal to "y" and
87584062dd3SSam Ravnborg 			 * choice is not "optional".
87684062dd3SSam Ravnborg 			 * (If choice is "optional" then all values can be "n")
8777cf3d73bSSam Ravnborg 			 */
8787cf3d73bSSam Ravnborg 			if (sym_is_choice_value(sym)) {
8797cf3d73bSSam Ravnborg 				struct symbol *cs;
8807cf3d73bSSam Ravnborg 				struct symbol *ds;
8817cf3d73bSSam Ravnborg 
8827cf3d73bSSam Ravnborg 				cs = prop_get_symbol(sym_get_choice_prop(sym));
8837cf3d73bSSam Ravnborg 				ds = sym_choice_default(cs);
88484062dd3SSam Ravnborg 				if (!sym_is_optional(cs) && sym == ds) {
885801690caSSam Ravnborg 					if ((sym->type == S_BOOLEAN) &&
8867cf3d73bSSam Ravnborg 					    sym_get_tristate_value(sym) == yes)
8877cf3d73bSSam Ravnborg 						goto next_menu;
8887cf3d73bSSam Ravnborg 				}
8897cf3d73bSSam Ravnborg 			}
8906ce45a91SMasahiro Yamada 			print_symbol_for_dotconfig(out, sym);
8917cf3d73bSSam Ravnborg 		}
8927cf3d73bSSam Ravnborg next_menu:
8937cf3d73bSSam Ravnborg 		if (menu->list != NULL) {
8947cf3d73bSSam Ravnborg 			menu = menu->list;
8957cf3d73bSSam Ravnborg 		}
8967cf3d73bSSam Ravnborg 		else if (menu->next != NULL) {
8977cf3d73bSSam Ravnborg 			menu = menu->next;
8987cf3d73bSSam Ravnborg 		} else {
8997cf3d73bSSam Ravnborg 			while ((menu = menu->parent)) {
9007cf3d73bSSam Ravnborg 				if (menu->next != NULL) {
9017cf3d73bSSam Ravnborg 					menu = menu->next;
9027cf3d73bSSam Ravnborg 					break;
9037cf3d73bSSam Ravnborg 				}
9047cf3d73bSSam Ravnborg 			}
9057cf3d73bSSam Ravnborg 		}
9067cf3d73bSSam Ravnborg 	}
9077cf3d73bSSam Ravnborg 	fclose(out);
9087cf3d73bSSam Ravnborg 	return 0;
9097cf3d73bSSam Ravnborg }
9107cf3d73bSSam Ravnborg 
conf_write(const char * name)9111da177e4SLinus Torvalds int conf_write(const char *name)
9121da177e4SLinus Torvalds {
913c955ccafSRoman Zippel 	FILE *out;
9141da177e4SLinus Torvalds 	struct symbol *sym;
9151da177e4SLinus Torvalds 	struct menu *menu;
9161da177e4SLinus Torvalds 	const char *str;
917ceb7f329SMasahiro Yamada 	char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
9181da177e4SLinus Torvalds 	char *env;
9190c5b6c28SM. Vefa Bicakci 	int i;
920aff11cd9SAlexander Popov 	bool need_newline = false;
9211da177e4SLinus Torvalds 
922ceb7f329SMasahiro Yamada 	if (!name)
923ceb7f329SMasahiro Yamada 		name = conf_get_configname();
924ceb7f329SMasahiro Yamada 
925ceb7f329SMasahiro Yamada 	if (!*name) {
926ceb7f329SMasahiro Yamada 		fprintf(stderr, "config name is empty\n");
927ceb7f329SMasahiro Yamada 		return -1;
928ceb7f329SMasahiro Yamada 	}
9291da177e4SLinus Torvalds 
9300608182aSMasahiro Yamada 	if (is_dir(name)) {
931ceb7f329SMasahiro Yamada 		fprintf(stderr, "%s: Is a directory\n", name);
932ceb7f329SMasahiro Yamada 		return -1;
933ceb7f329SMasahiro Yamada 	}
9341da177e4SLinus Torvalds 
935580c5b3eSMasahiro Yamada 	if (make_parent_dir(name))
936580c5b3eSMasahiro Yamada 		return -1;
937580c5b3eSMasahiro Yamada 
93814cdd3c4SRoman Zippel 	env = getenv("KCONFIG_OVERWRITECONFIG");
939ceb7f329SMasahiro Yamada 	if (env && *env) {
94014cdd3c4SRoman Zippel 		*tmpname = 0;
941ceb7f329SMasahiro Yamada 		out = fopen(name, "w");
942ceb7f329SMasahiro Yamada 	} else {
943ceb7f329SMasahiro Yamada 		snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
944ceb7f329SMasahiro Yamada 			 name, (int)getpid());
945ceb7f329SMasahiro Yamada 		out = fopen(tmpname, "w");
94614cdd3c4SRoman Zippel 	}
9471da177e4SLinus Torvalds 	if (!out)
9481da177e4SLinus Torvalds 		return 1;
94914cdd3c4SRoman Zippel 
950ca51b26bSMasahiro Yamada 	conf_write_heading(out, &comment_style_pound);
9511da177e4SLinus Torvalds 
952b3214293SKarsten Wiese 	if (!conf_get_changed())
9531da177e4SLinus Torvalds 		sym_clear_all_valid();
9541da177e4SLinus Torvalds 
9551da177e4SLinus Torvalds 	menu = rootmenu.list;
9561da177e4SLinus Torvalds 	while (menu) {
9571da177e4SLinus Torvalds 		sym = menu->sym;
9581da177e4SLinus Torvalds 		if (!sym) {
9591da177e4SLinus Torvalds 			if (!menu_is_visible(menu))
9601da177e4SLinus Torvalds 				goto next;
9611da177e4SLinus Torvalds 			str = menu_get_prompt(menu);
9621da177e4SLinus Torvalds 			fprintf(out, "\n"
9631da177e4SLinus Torvalds 				     "#\n"
9641da177e4SLinus Torvalds 				     "# %s\n"
9651da177e4SLinus Torvalds 				     "#\n", str);
966aff11cd9SAlexander Popov 			need_newline = false;
9678e2442a5SMasahiro Yamada 		} else if (!(sym->flags & SYMBOL_CHOICE) &&
9688e2442a5SMasahiro Yamada 			   !(sym->flags & SYMBOL_WRITTEN)) {
9691da177e4SLinus Torvalds 			sym_calc_value(sym);
9701da177e4SLinus Torvalds 			if (!(sym->flags & SYMBOL_WRITE))
9711da177e4SLinus Torvalds 				goto next;
972aff11cd9SAlexander Popov 			if (need_newline) {
973aff11cd9SAlexander Popov 				fprintf(out, "\n");
974aff11cd9SAlexander Popov 				need_newline = false;
975aff11cd9SAlexander Popov 			}
9768e2442a5SMasahiro Yamada 			sym->flags |= SYMBOL_WRITTEN;
9776ce45a91SMasahiro Yamada 			print_symbol_for_dotconfig(out, sym);
9781da177e4SLinus Torvalds 		}
9791da177e4SLinus Torvalds 
9801da177e4SLinus Torvalds next:
9811da177e4SLinus Torvalds 		if (menu->list) {
9821da177e4SLinus Torvalds 			menu = menu->list;
9831da177e4SLinus Torvalds 			continue;
9841da177e4SLinus Torvalds 		}
985a7d4f58eSMasahiro Yamada 
986a7d4f58eSMasahiro Yamada end_check:
987a7d4f58eSMasahiro Yamada 		if (!menu->sym && menu_is_visible(menu) && menu != &rootmenu &&
988a7d4f58eSMasahiro Yamada 		    menu->prompt->type == P_MENU) {
989a7d4f58eSMasahiro Yamada 			fprintf(out, "# end of %s\n", menu_get_prompt(menu));
990aff11cd9SAlexander Popov 			need_newline = true;
991aff11cd9SAlexander Popov 		}
992a7d4f58eSMasahiro Yamada 
9931da177e4SLinus Torvalds 		if (menu->next) {
9941da177e4SLinus Torvalds 			menu = menu->next;
995a7d4f58eSMasahiro Yamada 		} else {
996a7d4f58eSMasahiro Yamada 			menu = menu->parent;
997a7d4f58eSMasahiro Yamada 			if (menu)
998a7d4f58eSMasahiro Yamada 				goto end_check;
9991da177e4SLinus Torvalds 		}
10001da177e4SLinus Torvalds 	}
10011da177e4SLinus Torvalds 	fclose(out);
100214cdd3c4SRoman Zippel 
10030c5b6c28SM. Vefa Bicakci 	for_all_symbols(i, sym)
10040c5b6c28SM. Vefa Bicakci 		sym->flags &= ~SYMBOL_WRITTEN;
10050c5b6c28SM. Vefa Bicakci 
100614cdd3c4SRoman Zippel 	if (*tmpname) {
100767424f61SMasahiro Yamada 		if (is_same(name, tmpname)) {
100867424f61SMasahiro Yamada 			conf_message("No change to %s", name);
100967424f61SMasahiro Yamada 			unlink(tmpname);
10105ee54659SMasahiro Yamada 			conf_set_changed(false);
101167424f61SMasahiro Yamada 			return 0;
101267424f61SMasahiro Yamada 		}
101367424f61SMasahiro Yamada 
1014ceb7f329SMasahiro Yamada 		snprintf(oldname, sizeof(oldname), "%s.old", name);
1015ceb7f329SMasahiro Yamada 		rename(name, oldname);
1016ceb7f329SMasahiro Yamada 		if (rename(tmpname, name))
10171da177e4SLinus Torvalds 			return 1;
101814cdd3c4SRoman Zippel 	}
10191da177e4SLinus Torvalds 
1020ceb7f329SMasahiro Yamada 	conf_message("configuration written to %s", name);
1021ddc97cacSRoman Zippel 
10225ee54659SMasahiro Yamada 	conf_set_changed(false);
10231da177e4SLinus Torvalds 
10241da177e4SLinus Torvalds 	return 0;
10251da177e4SLinus Torvalds }
1026c955ccafSRoman Zippel 
1027a2ff4040SMasahiro Yamada /* write a dependency file as used by kbuild to track dependencies */
conf_write_autoconf_cmd(const char * autoconf_name)102800d674cbSMasahiro Yamada static int conf_write_autoconf_cmd(const char *autoconf_name)
1029a2ff4040SMasahiro Yamada {
103000d674cbSMasahiro Yamada 	char name[PATH_MAX], tmp[PATH_MAX];
1031a2ff4040SMasahiro Yamada 	struct file *file;
1032a2ff4040SMasahiro Yamada 	FILE *out;
103300d674cbSMasahiro Yamada 	int ret;
1034a2ff4040SMasahiro Yamada 
103500d674cbSMasahiro Yamada 	ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
103600d674cbSMasahiro Yamada 	if (ret >= sizeof(name)) /* check truncation */
103700d674cbSMasahiro Yamada 		return -1;
103879123b13SMasahiro Yamada 
103979123b13SMasahiro Yamada 	if (make_parent_dir(name))
104000d674cbSMasahiro Yamada 		return -1;
104100d674cbSMasahiro Yamada 
104200d674cbSMasahiro Yamada 	ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
104300d674cbSMasahiro Yamada 	if (ret >= sizeof(tmp)) /* check truncation */
104400d674cbSMasahiro Yamada 		return -1;
104500d674cbSMasahiro Yamada 
104600d674cbSMasahiro Yamada 	out = fopen(tmp, "w");
104700d674cbSMasahiro Yamada 	if (!out) {
104800d674cbSMasahiro Yamada 		perror("fopen");
104900d674cbSMasahiro Yamada 		return -1;
105000d674cbSMasahiro Yamada 	}
105100d674cbSMasahiro Yamada 
105200d674cbSMasahiro Yamada 	fprintf(out, "deps_config := \\\n");
105300d674cbSMasahiro Yamada 	for (file = file_list; file; file = file->next)
105400d674cbSMasahiro Yamada 		fprintf(out, "\t%s \\\n", file->name);
105500d674cbSMasahiro Yamada 
105600d674cbSMasahiro Yamada 	fprintf(out, "\n%s: $(deps_config)\n\n", autoconf_name);
105700d674cbSMasahiro Yamada 
105800d674cbSMasahiro Yamada 	env_write_dep(out, autoconf_name);
105900d674cbSMasahiro Yamada 
106000d674cbSMasahiro Yamada 	fprintf(out, "\n$(deps_config): ;\n");
106100d674cbSMasahiro Yamada 
1062868653f4SMasahiro Yamada 	fflush(out);
1063d23a0c37SMasahiro Yamada 	ret = ferror(out); /* error check for all fprintf() calls */
106400d674cbSMasahiro Yamada 	fclose(out);
1065d23a0c37SMasahiro Yamada 	if (ret)
1066d23a0c37SMasahiro Yamada 		return -1;
106700d674cbSMasahiro Yamada 
106800d674cbSMasahiro Yamada 	if (rename(tmp, name)) {
106900d674cbSMasahiro Yamada 		perror("rename");
107000d674cbSMasahiro Yamada 		return -1;
107100d674cbSMasahiro Yamada 	}
107200d674cbSMasahiro Yamada 
1073a2ff4040SMasahiro Yamada 	return 0;
1074a2ff4040SMasahiro Yamada }
1075a2ff4040SMasahiro Yamada 
conf_touch_deps(void)10760849d212SMasahiro Yamada static int conf_touch_deps(void)
10772e3646e5SRoman Zippel {
10781b9e740aSJing Leng 	const char *name, *tmp;
10792e3646e5SRoman Zippel 	struct symbol *sym;
10801508fec8SMasahiro Yamada 	int res, i;
10811508fec8SMasahiro Yamada 
108212122f62SMarkus Heidelberg 	name = conf_get_autoconfig_name();
10831b9e740aSJing Leng 	tmp = strrchr(name, '/');
10841b9e740aSJing Leng 	depfile_prefix_len = tmp ? tmp - name + 1 : 0;
10851b9e740aSJing Leng 	if (depfile_prefix_len + 1 > sizeof(depfile_path))
10861b9e740aSJing Leng 		return -1;
10871b9e740aSJing Leng 
10881b9e740aSJing Leng 	strncpy(depfile_path, name, depfile_prefix_len);
10891b9e740aSJing Leng 	depfile_path[depfile_prefix_len] = 0;
10901b9e740aSJing Leng 
10912e3646e5SRoman Zippel 	conf_read_simple(name, S_DEF_AUTO);
10926b87b70cSAl Viro 	sym_calc_value(modules_sym);
10932e3646e5SRoman Zippel 
10942e3646e5SRoman Zippel 	for_all_symbols(i, sym) {
10952e3646e5SRoman Zippel 		sym_calc_value(sym);
1096693359f7SDirk Gouders 		if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
10972e3646e5SRoman Zippel 			continue;
10982e3646e5SRoman Zippel 		if (sym->flags & SYMBOL_WRITE) {
10992e3646e5SRoman Zippel 			if (sym->flags & SYMBOL_DEF_AUTO) {
11002e3646e5SRoman Zippel 				/*
11012e3646e5SRoman Zippel 				 * symbol has old and new value,
11022e3646e5SRoman Zippel 				 * so compare them...
11032e3646e5SRoman Zippel 				 */
11042e3646e5SRoman Zippel 				switch (sym->type) {
11052e3646e5SRoman Zippel 				case S_BOOLEAN:
11062e3646e5SRoman Zippel 				case S_TRISTATE:
11072e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) ==
11082e3646e5SRoman Zippel 					    sym->def[S_DEF_AUTO].tri)
11092e3646e5SRoman Zippel 						continue;
11102e3646e5SRoman Zippel 					break;
11112e3646e5SRoman Zippel 				case S_STRING:
11122e3646e5SRoman Zippel 				case S_HEX:
11132e3646e5SRoman Zippel 				case S_INT:
11142e3646e5SRoman Zippel 					if (!strcmp(sym_get_string_value(sym),
11152e3646e5SRoman Zippel 						    sym->def[S_DEF_AUTO].val))
11162e3646e5SRoman Zippel 						continue;
11172e3646e5SRoman Zippel 					break;
11182e3646e5SRoman Zippel 				default:
11192e3646e5SRoman Zippel 					break;
11202e3646e5SRoman Zippel 				}
11212e3646e5SRoman Zippel 			} else {
11222e3646e5SRoman Zippel 				/*
11232e3646e5SRoman Zippel 				 * If there is no old value, only 'no' (unset)
11242e3646e5SRoman Zippel 				 * is allowed as new value.
11252e3646e5SRoman Zippel 				 */
11262e3646e5SRoman Zippel 				switch (sym->type) {
11272e3646e5SRoman Zippel 				case S_BOOLEAN:
11282e3646e5SRoman Zippel 				case S_TRISTATE:
11292e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) == no)
11302e3646e5SRoman Zippel 						continue;
11312e3646e5SRoman Zippel 					break;
11322e3646e5SRoman Zippel 				default:
11332e3646e5SRoman Zippel 					break;
11342e3646e5SRoman Zippel 				}
11352e3646e5SRoman Zippel 			}
11362e3646e5SRoman Zippel 		} else if (!(sym->flags & SYMBOL_DEF_AUTO))
11372e3646e5SRoman Zippel 			/* There is neither an old nor a new value. */
11382e3646e5SRoman Zippel 			continue;
11392e3646e5SRoman Zippel 		/* else
11402e3646e5SRoman Zippel 		 *	There is an old value, but no new value ('no' (unset)
11412e3646e5SRoman Zippel 		 *	isn't saved in auto.conf, so the old value is always
11422e3646e5SRoman Zippel 		 *	different from 'no').
11432e3646e5SRoman Zippel 		 */
11442e3646e5SRoman Zippel 
11451508fec8SMasahiro Yamada 		res = conf_touch_dep(sym->name);
11461508fec8SMasahiro Yamada 		if (res)
11472e3646e5SRoman Zippel 			return res;
11482e3646e5SRoman Zippel 	}
11492e3646e5SRoman Zippel 
11501508fec8SMasahiro Yamada 	return 0;
11511508fec8SMasahiro Yamada }
11521508fec8SMasahiro Yamada 
__conf_write_autoconf(const char * filename,void (* print_symbol)(FILE *,struct symbol *),const struct comment_style * comment_style)115357ddd07cSMasahiro Yamada static int __conf_write_autoconf(const char *filename,
115457ddd07cSMasahiro Yamada 				 void (*print_symbol)(FILE *, struct symbol *),
115557ddd07cSMasahiro Yamada 				 const struct comment_style *comment_style)
115657ddd07cSMasahiro Yamada {
115757ddd07cSMasahiro Yamada 	char tmp[PATH_MAX];
115857ddd07cSMasahiro Yamada 	FILE *file;
115957ddd07cSMasahiro Yamada 	struct symbol *sym;
116057ddd07cSMasahiro Yamada 	int ret, i;
116157ddd07cSMasahiro Yamada 
116257ddd07cSMasahiro Yamada 	if (make_parent_dir(filename))
116357ddd07cSMasahiro Yamada 		return -1;
116457ddd07cSMasahiro Yamada 
116557ddd07cSMasahiro Yamada 	ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
116657ddd07cSMasahiro Yamada 	if (ret >= sizeof(tmp)) /* check truncation */
116757ddd07cSMasahiro Yamada 		return -1;
116857ddd07cSMasahiro Yamada 
116957ddd07cSMasahiro Yamada 	file = fopen(tmp, "w");
117057ddd07cSMasahiro Yamada 	if (!file) {
117157ddd07cSMasahiro Yamada 		perror("fopen");
117257ddd07cSMasahiro Yamada 		return -1;
117357ddd07cSMasahiro Yamada 	}
117457ddd07cSMasahiro Yamada 
117557ddd07cSMasahiro Yamada 	conf_write_heading(file, comment_style);
117657ddd07cSMasahiro Yamada 
117757ddd07cSMasahiro Yamada 	for_all_symbols(i, sym)
117857ddd07cSMasahiro Yamada 		if ((sym->flags & SYMBOL_WRITE) && sym->name)
117957ddd07cSMasahiro Yamada 			print_symbol(file, sym);
118057ddd07cSMasahiro Yamada 
1181868653f4SMasahiro Yamada 	fflush(file);
118257ddd07cSMasahiro Yamada 	/* check possible errors in conf_write_heading() and print_symbol() */
1183d23a0c37SMasahiro Yamada 	ret = ferror(file);
118457ddd07cSMasahiro Yamada 	fclose(file);
1185d23a0c37SMasahiro Yamada 	if (ret)
1186d23a0c37SMasahiro Yamada 		return -1;
118757ddd07cSMasahiro Yamada 
118857ddd07cSMasahiro Yamada 	if (rename(tmp, filename)) {
118957ddd07cSMasahiro Yamada 		perror("rename");
119057ddd07cSMasahiro Yamada 		return -1;
119157ddd07cSMasahiro Yamada 	}
119257ddd07cSMasahiro Yamada 
119357ddd07cSMasahiro Yamada 	return 0;
119457ddd07cSMasahiro Yamada }
119557ddd07cSMasahiro Yamada 
conf_write_autoconf(int overwrite)119600c864f8SMasahiro Yamada int conf_write_autoconf(int overwrite)
1197c955ccafSRoman Zippel {
1198c955ccafSRoman Zippel 	struct symbol *sym;
119900c864f8SMasahiro Yamada 	const char *autoconf_name = conf_get_autoconfig_name();
120057ddd07cSMasahiro Yamada 	int ret, i;
1201c955ccafSRoman Zippel 
120200c864f8SMasahiro Yamada 	if (!overwrite && is_present(autoconf_name))
120300c864f8SMasahiro Yamada 		return 0;
120400c864f8SMasahiro Yamada 
120500d674cbSMasahiro Yamada 	ret = conf_write_autoconf_cmd(autoconf_name);
120600d674cbSMasahiro Yamada 	if (ret)
120700d674cbSMasahiro Yamada 		return -1;
1208c955ccafSRoman Zippel 
12090849d212SMasahiro Yamada 	if (conf_touch_deps())
12102e3646e5SRoman Zippel 		return 1;
12112e3646e5SRoman Zippel 
121257ddd07cSMasahiro Yamada 	for_all_symbols(i, sym)
1213953742c8SArnaud Lacombe 		sym_calc_value(sym);
1214953742c8SArnaud Lacombe 
121557ddd07cSMasahiro Yamada 	ret = __conf_write_autoconf(conf_get_autoheader_name(),
121657ddd07cSMasahiro Yamada 				    print_symbol_for_c,
121757ddd07cSMasahiro Yamada 				    &comment_style_c);
121857ddd07cSMasahiro Yamada 	if (ret)
121957ddd07cSMasahiro Yamada 		return ret;
1220c955ccafSRoman Zippel 
12212f7ab126SMiguel Ojeda 	ret = __conf_write_autoconf(conf_get_rustccfg_name(),
12222f7ab126SMiguel Ojeda 				    print_symbol_for_rustccfg,
12232f7ab126SMiguel Ojeda 				    NULL);
12242f7ab126SMiguel Ojeda 	if (ret)
12252f7ab126SMiguel Ojeda 		return ret;
12262f7ab126SMiguel Ojeda 
1227c955ccafSRoman Zippel 	/*
122857ddd07cSMasahiro Yamada 	 * Create include/config/auto.conf. This must be the last step because
122957ddd07cSMasahiro Yamada 	 * Kbuild has a dependency on auto.conf and this marks the successful
123057ddd07cSMasahiro Yamada 	 * completion of the previous steps.
1231c955ccafSRoman Zippel 	 */
123257ddd07cSMasahiro Yamada 	ret = __conf_write_autoconf(conf_get_autoconfig_name(),
123357ddd07cSMasahiro Yamada 				    print_symbol_for_autoconf,
123457ddd07cSMasahiro Yamada 				    &comment_style_pound);
123557ddd07cSMasahiro Yamada 	if (ret)
123657ddd07cSMasahiro Yamada 		return ret;
1237c955ccafSRoman Zippel 
1238c955ccafSRoman Zippel 	return 0;
1239c955ccafSRoman Zippel }
1240b3214293SKarsten Wiese 
12415ee54659SMasahiro Yamada static bool conf_changed;
12423b354c55SKarsten Wiese static void (*conf_changed_callback)(void);
1243bfc10001SKarsten Wiese 
conf_set_changed(bool val)12445ee54659SMasahiro Yamada void conf_set_changed(bool val)
1245bfc10001SKarsten Wiese {
1246ee06a3efSJurica Vukadin 	bool changed = conf_changed != val;
1247bfc10001SKarsten Wiese 
12485ee54659SMasahiro Yamada 	conf_changed = val;
1249ee06a3efSJurica Vukadin 
1250ee06a3efSJurica Vukadin 	if (conf_changed_callback && changed)
1251ee06a3efSJurica Vukadin 		conf_changed_callback();
1252bfc10001SKarsten Wiese }
1253bfc10001SKarsten Wiese 
conf_get_changed(void)1254b3214293SKarsten Wiese bool conf_get_changed(void)
1255b3214293SKarsten Wiese {
12565ee54659SMasahiro Yamada 	return conf_changed;
1257b3214293SKarsten Wiese }
12583b354c55SKarsten Wiese 
conf_set_changed_callback(void (* fn)(void))12593b354c55SKarsten Wiese void conf_set_changed_callback(void (*fn)(void))
12603b354c55SKarsten Wiese {
12613b354c55SKarsten Wiese 	conf_changed_callback = fn;
12623b354c55SKarsten Wiese }
1263dc7862e5SRoman Zippel 
set_all_choice_values(struct symbol * csym)1264fbe98bb9SArve Hjønnevåg void set_all_choice_values(struct symbol *csym)
1265a64b44eaSSam Ravnborg {
1266a64b44eaSSam Ravnborg 	struct property *prop;
1267a64b44eaSSam Ravnborg 	struct symbol *sym;
1268a64b44eaSSam Ravnborg 	struct expr *e;
1269a64b44eaSSam Ravnborg 
1270a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
1271a64b44eaSSam Ravnborg 
1272a64b44eaSSam Ravnborg 	/*
1273a64b44eaSSam Ravnborg 	 * Set all non-assinged choice values to no
1274a64b44eaSSam Ravnborg 	 */
1275a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
1276a64b44eaSSam Ravnborg 		if (!sym_has_value(sym))
1277a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
1278a64b44eaSSam Ravnborg 	}
1279a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
1280a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
1281fbe98bb9SArve Hjønnevåg 	csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1282a64b44eaSSam Ravnborg }
1283