xref: /linux/scripts/kconfig/confdata.c (revision 129ab0d2d9f38b9d43df35235fc66c6740d6928b)
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 */
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 */
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 */
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  */
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' */
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 
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 
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;
1785accd7f3SMasahiro Yamada void conf_set_message_callback(void (*fn)(const char *s))
17942368c37SMichal Marek {
18042368c37SMichal Marek 	conf_message_callback = fn;
18142368c37SMichal Marek }
18242368c37SMichal Marek 
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 
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 
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 
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 
2199c900a9cSSam Ravnborg static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
2209c900a9cSSam Ravnborg {
2219c900a9cSSam Ravnborg 	char *p2;
2229c900a9cSSam Ravnborg 
2239c900a9cSSam Ravnborg 	switch (sym->type) {
2249c900a9cSSam Ravnborg 	case S_TRISTATE:
2259c900a9cSSam Ravnborg 		if (p[0] == 'm') {
2269c900a9cSSam Ravnborg 			sym->def[def].tri = mod;
2279c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2289c900a9cSSam Ravnborg 			break;
2299c900a9cSSam Ravnborg 		}
230d8fc3200SArnaud Lacombe 		/* fall through */
2319c900a9cSSam Ravnborg 	case S_BOOLEAN:
2329c900a9cSSam Ravnborg 		if (p[0] == 'y') {
2339c900a9cSSam Ravnborg 			sym->def[def].tri = yes;
2349c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2359c900a9cSSam Ravnborg 			break;
2369c900a9cSSam Ravnborg 		}
2379c900a9cSSam Ravnborg 		if (p[0] == 'n') {
2389c900a9cSSam Ravnborg 			sym->def[def].tri = no;
2399c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2409c900a9cSSam Ravnborg 			break;
2419c900a9cSSam Ravnborg 		}
24204b19b77SYann E. MORIN 		if (def != S_DEF_AUTO)
24304b19b77SYann E. MORIN 			conf_warning("symbol value '%s' invalid for %s",
24404b19b77SYann E. MORIN 				     p, sym->name);
24575f1468bSArnaud Lacombe 		return 1;
2469c900a9cSSam Ravnborg 	case S_STRING:
247*129ab0d2SMasahiro Yamada 		/* No escaping for S_DEF_AUTO (include/config/auto.conf) */
248*129ab0d2SMasahiro Yamada 		if (def != S_DEF_AUTO) {
2499c900a9cSSam Ravnborg 			if (*p++ != '"')
2509c900a9cSSam Ravnborg 				break;
2519c900a9cSSam Ravnborg 			for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
2529c900a9cSSam Ravnborg 				if (*p2 == '"') {
2539c900a9cSSam Ravnborg 					*p2 = 0;
2549c900a9cSSam Ravnborg 					break;
2559c900a9cSSam Ravnborg 				}
2569c900a9cSSam Ravnborg 				memmove(p2, p2 + 1, strlen(p2));
2579c900a9cSSam Ravnborg 			}
2589c900a9cSSam Ravnborg 			if (!p2) {
2599c900a9cSSam Ravnborg 				conf_warning("invalid string found");
2609c900a9cSSam Ravnborg 				return 1;
2619c900a9cSSam Ravnborg 			}
262*129ab0d2SMasahiro Yamada 		}
263d8fc3200SArnaud Lacombe 		/* fall through */
2649c900a9cSSam Ravnborg 	case S_INT:
2659c900a9cSSam Ravnborg 	case S_HEX:
2669c900a9cSSam Ravnborg 		if (sym_string_valid(sym, p)) {
267cd81fc82SMasahiro Yamada 			sym->def[def].val = xstrdup(p);
2689c900a9cSSam Ravnborg 			sym->flags |= def_flags;
2699c900a9cSSam Ravnborg 		} else {
27004b19b77SYann E. MORIN 			if (def != S_DEF_AUTO)
27104b19b77SYann E. MORIN 				conf_warning("symbol value '%s' invalid for %s",
27204b19b77SYann E. MORIN 					     p, sym->name);
2739c900a9cSSam Ravnborg 			return 1;
2749c900a9cSSam Ravnborg 		}
2759c900a9cSSam Ravnborg 		break;
2769c900a9cSSam Ravnborg 	default:
2779c900a9cSSam Ravnborg 		;
2789c900a9cSSam Ravnborg 	}
2799c900a9cSSam Ravnborg 	return 0;
2809c900a9cSSam Ravnborg }
2819c900a9cSSam Ravnborg 
2821a7a8c6fSCody Schafer #define LINE_GROWTH 16
2831a7a8c6fSCody Schafer static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
2841a7a8c6fSCody Schafer {
2851a7a8c6fSCody Schafer 	char *nline;
2861a7a8c6fSCody Schafer 	size_t new_size = slen + 1;
2871a7a8c6fSCody Schafer 	if (new_size > *n) {
2881a7a8c6fSCody Schafer 		new_size += LINE_GROWTH - 1;
2891a7a8c6fSCody Schafer 		new_size *= 2;
290d717f24dSMasahiro Yamada 		nline = xrealloc(*lineptr, new_size);
2911a7a8c6fSCody Schafer 		if (!nline)
2921a7a8c6fSCody Schafer 			return -1;
2931a7a8c6fSCody Schafer 
2941a7a8c6fSCody Schafer 		*lineptr = nline;
2951a7a8c6fSCody Schafer 		*n = new_size;
2961a7a8c6fSCody Schafer 	}
2971a7a8c6fSCody Schafer 
2981a7a8c6fSCody Schafer 	(*lineptr)[slen] = c;
2991a7a8c6fSCody Schafer 
3001a7a8c6fSCody Schafer 	return 0;
3011a7a8c6fSCody Schafer }
3021a7a8c6fSCody Schafer 
3031a7a8c6fSCody Schafer static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
3041a7a8c6fSCody Schafer {
3051a7a8c6fSCody Schafer 	char *line = *lineptr;
3061a7a8c6fSCody Schafer 	size_t slen = 0;
3071a7a8c6fSCody Schafer 
3081a7a8c6fSCody Schafer 	for (;;) {
3091a7a8c6fSCody Schafer 		int c = getc(stream);
3101a7a8c6fSCody Schafer 
3111a7a8c6fSCody Schafer 		switch (c) {
3121a7a8c6fSCody Schafer 		case '\n':
3131a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
3141a7a8c6fSCody Schafer 				goto e_out;
3151a7a8c6fSCody Schafer 			slen++;
3161a7a8c6fSCody Schafer 			/* fall through */
3171a7a8c6fSCody Schafer 		case EOF:
3181a7a8c6fSCody Schafer 			if (add_byte('\0', &line, slen, n) < 0)
3191a7a8c6fSCody Schafer 				goto e_out;
3201a7a8c6fSCody Schafer 			*lineptr = line;
3211a7a8c6fSCody Schafer 			if (slen == 0)
3221a7a8c6fSCody Schafer 				return -1;
3231a7a8c6fSCody Schafer 			return slen;
3241a7a8c6fSCody Schafer 		default:
3251a7a8c6fSCody Schafer 			if (add_byte(c, &line, slen, n) < 0)
3261a7a8c6fSCody Schafer 				goto e_out;
3271a7a8c6fSCody Schafer 			slen++;
3281a7a8c6fSCody Schafer 		}
3291a7a8c6fSCody Schafer 	}
3301a7a8c6fSCody Schafer 
3311a7a8c6fSCody Schafer e_out:
3321a7a8c6fSCody Schafer 	line[slen-1] = '\0';
3331a7a8c6fSCody Schafer 	*lineptr = line;
3341a7a8c6fSCody Schafer 	return -1;
3351a7a8c6fSCody Schafer }
3361a7a8c6fSCody Schafer 
337669bfad9SRoman Zippel int conf_read_simple(const char *name, int def)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	FILE *in = NULL;
3401a7a8c6fSCody Schafer 	char   *line = NULL;
3411a7a8c6fSCody Schafer 	size_t  line_asize = 0;
3421da177e4SLinus Torvalds 	char *p, *p2;
3431da177e4SLinus Torvalds 	struct symbol *sym;
344669bfad9SRoman Zippel 	int i, def_flags;
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	if (name) {
3471da177e4SLinus Torvalds 		in = zconf_fopen(name);
3481da177e4SLinus Torvalds 	} else {
349b75b0a81SMasahiro Yamada 		char *env;
350face4374SRoman Zippel 
35114cdd3c4SRoman Zippel 		name = conf_get_configname();
352ddc97cacSRoman Zippel 		in = zconf_fopen(name);
353ddc97cacSRoman Zippel 		if (in)
354ddc97cacSRoman Zippel 			goto load;
3555ee54659SMasahiro Yamada 		conf_set_changed(true);
356b75b0a81SMasahiro Yamada 
357b75b0a81SMasahiro Yamada 		env = getenv("KCONFIG_DEFCONFIG_LIST");
358b75b0a81SMasahiro Yamada 		if (!env)
359face4374SRoman Zippel 			return 1;
360face4374SRoman Zippel 
361b75b0a81SMasahiro Yamada 		while (1) {
362b75b0a81SMasahiro Yamada 			bool is_last;
363b75b0a81SMasahiro Yamada 
364b75b0a81SMasahiro Yamada 			while (isspace(*env))
365b75b0a81SMasahiro Yamada 				env++;
366b75b0a81SMasahiro Yamada 
367b75b0a81SMasahiro Yamada 			if (!*env)
368b75b0a81SMasahiro Yamada 				break;
369b75b0a81SMasahiro Yamada 
370b75b0a81SMasahiro Yamada 			p = env;
371b75b0a81SMasahiro Yamada 			while (*p && !isspace(*p))
372b75b0a81SMasahiro Yamada 				p++;
373b75b0a81SMasahiro Yamada 
374b75b0a81SMasahiro Yamada 			is_last = (*p == '\0');
375b75b0a81SMasahiro Yamada 
376b75b0a81SMasahiro Yamada 			*p = '\0';
377b75b0a81SMasahiro Yamada 
378b75b0a81SMasahiro Yamada 			in = zconf_fopen(env);
3791da177e4SLinus Torvalds 			if (in) {
380694c49a7SSam Ravnborg 				conf_message("using defaults found in %s",
381b75b0a81SMasahiro Yamada 					     env);
382ddc97cacSRoman Zippel 				goto load;
3831da177e4SLinus Torvalds 			}
384b75b0a81SMasahiro Yamada 
385b75b0a81SMasahiro Yamada 			if (is_last)
386b75b0a81SMasahiro Yamada 				break;
387b75b0a81SMasahiro Yamada 
388b75b0a81SMasahiro Yamada 			env = p + 1;
3891da177e4SLinus Torvalds 		}
3901da177e4SLinus Torvalds 	}
3911da177e4SLinus Torvalds 	if (!in)
3921da177e4SLinus Torvalds 		return 1;
3931da177e4SLinus Torvalds 
394ddc97cacSRoman Zippel load:
395c1a0f5e3SRoman Zippel 	conf_filename = name;
396c1a0f5e3SRoman Zippel 	conf_lineno = 0;
397c1a0f5e3SRoman Zippel 	conf_warnings = 0;
398c1a0f5e3SRoman Zippel 
399669bfad9SRoman Zippel 	def_flags = SYMBOL_DEF << def;
4001da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
401669bfad9SRoman Zippel 		sym->flags |= SYMBOL_CHANGED;
402669bfad9SRoman Zippel 		sym->flags &= ~(def_flags|SYMBOL_VALID);
403490f1617SYann E. MORIN 		if (sym_is_choice(sym))
404490f1617SYann E. MORIN 			sym->flags |= def_flags;
4051da177e4SLinus Torvalds 		switch (sym->type) {
4061da177e4SLinus Torvalds 		case S_INT:
4071da177e4SLinus Torvalds 		case S_HEX:
4081da177e4SLinus Torvalds 		case S_STRING:
409669bfad9SRoman Zippel 			if (sym->def[def].val)
410669bfad9SRoman Zippel 				free(sym->def[def].val);
411d8fc3200SArnaud Lacombe 			/* fall through */
4121da177e4SLinus Torvalds 		default:
413669bfad9SRoman Zippel 			sym->def[def].val = NULL;
414669bfad9SRoman Zippel 			sym->def[def].tri = no;
4151da177e4SLinus Torvalds 		}
4161da177e4SLinus Torvalds 	}
4171da177e4SLinus Torvalds 
4181a7a8c6fSCody Schafer 	while (compat_getline(&line, &line_asize, in) != -1) {
419c1a0f5e3SRoman Zippel 		conf_lineno++;
4201da177e4SLinus Torvalds 		sym = NULL;
4218baefd30SArnaud Lacombe 		if (line[0] == '#') {
422ffb5957bSArnaud Lacombe 			if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
4231da177e4SLinus Torvalds 				continue;
424ffb5957bSArnaud Lacombe 			p = strchr(line + 2 + strlen(CONFIG_), ' ');
4251da177e4SLinus Torvalds 			if (!p)
4261da177e4SLinus Torvalds 				continue;
4271da177e4SLinus Torvalds 			*p++ = 0;
4281da177e4SLinus Torvalds 			if (strncmp(p, "is not set", 10))
4291da177e4SLinus Torvalds 				continue;
430669bfad9SRoman Zippel 			if (def == S_DEF_USER) {
431ffb5957bSArnaud Lacombe 				sym = sym_find(line + 2 + strlen(CONFIG_));
432661b0680Szippel@linux-m68k.org 				if (!sym) {
4335ee54659SMasahiro Yamada 					conf_set_changed(true);
43475889e9bSMasahiro Yamada 					continue;
435661b0680Szippel@linux-m68k.org 				}
436669bfad9SRoman Zippel 			} else {
437ffb5957bSArnaud Lacombe 				sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
438669bfad9SRoman Zippel 				if (sym->type == S_UNKNOWN)
439669bfad9SRoman Zippel 					sym->type = S_BOOLEAN;
440669bfad9SRoman Zippel 			}
441669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
442d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
4431da177e4SLinus Torvalds 			}
4441da177e4SLinus Torvalds 			switch (sym->type) {
4451da177e4SLinus Torvalds 			case S_BOOLEAN:
4461da177e4SLinus Torvalds 			case S_TRISTATE:
447669bfad9SRoman Zippel 				sym->def[def].tri = no;
448669bfad9SRoman Zippel 				sym->flags |= def_flags;
4491da177e4SLinus Torvalds 				break;
4501da177e4SLinus Torvalds 			default:
4511da177e4SLinus Torvalds 				;
4521da177e4SLinus Torvalds 			}
453ffb5957bSArnaud Lacombe 		} else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
454ffb5957bSArnaud Lacombe 			p = strchr(line + strlen(CONFIG_), '=');
4551da177e4SLinus Torvalds 			if (!p)
4561da177e4SLinus Torvalds 				continue;
4571da177e4SLinus Torvalds 			*p++ = 0;
4581da177e4SLinus Torvalds 			p2 = strchr(p, '\n');
459d3660a8cSMatthew Wilcox 			if (p2) {
460d3660a8cSMatthew Wilcox 				*p2-- = 0;
461d3660a8cSMatthew Wilcox 				if (*p2 == '\r')
4621da177e4SLinus Torvalds 					*p2 = 0;
463d3660a8cSMatthew Wilcox 			}
4642aabbed6SMasahiro Yamada 
465ffb5957bSArnaud Lacombe 			sym = sym_find(line + strlen(CONFIG_));
466661b0680Szippel@linux-m68k.org 			if (!sym) {
4672aabbed6SMasahiro Yamada 				if (def == S_DEF_AUTO)
4682aabbed6SMasahiro Yamada 					/*
4692aabbed6SMasahiro Yamada 					 * Reading from include/config/auto.conf
4702aabbed6SMasahiro Yamada 					 * If CONFIG_FOO previously existed in
4712aabbed6SMasahiro Yamada 					 * auto.conf but it is missing now,
4720e0345b7SAlexey Dobriyan 					 * include/config/FOO must be touched.
4732aabbed6SMasahiro Yamada 					 */
4742aabbed6SMasahiro Yamada 					conf_touch_dep(line + strlen(CONFIG_));
4752aabbed6SMasahiro Yamada 				else
4765ee54659SMasahiro Yamada 					conf_set_changed(true);
47775889e9bSMasahiro Yamada 				continue;
478661b0680Szippel@linux-m68k.org 			}
4792aabbed6SMasahiro Yamada 
480669bfad9SRoman Zippel 			if (sym->flags & def_flags) {
481d84876f9SJan Engelhardt 				conf_warning("override: reassigning to symbol %s", sym->name);
4821da177e4SLinus Torvalds 			}
4839c900a9cSSam Ravnborg 			if (conf_set_sym_val(sym, def, def_flags, p))
484c1a0f5e3SRoman Zippel 				continue;
4858baefd30SArnaud Lacombe 		} else {
4868baefd30SArnaud Lacombe 			if (line[0] != '\r' && line[0] != '\n')
487a4663911SPaul Bolle 				conf_warning("unexpected data: %.*s",
488a4663911SPaul Bolle 					     (int)strcspn(line, "\r\n"), line);
489a4663911SPaul Bolle 
4901da177e4SLinus Torvalds 			continue;
4911da177e4SLinus Torvalds 		}
49275889e9bSMasahiro Yamada 
4931da177e4SLinus Torvalds 		if (sym && sym_is_choice_value(sym)) {
4941da177e4SLinus Torvalds 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
495669bfad9SRoman Zippel 			switch (sym->def[def].tri) {
4961da177e4SLinus Torvalds 			case no:
4971da177e4SLinus Torvalds 				break;
4981da177e4SLinus Torvalds 			case mod:
499669bfad9SRoman Zippel 				if (cs->def[def].tri == yes) {
500c1a0f5e3SRoman Zippel 					conf_warning("%s creates inconsistent choice state", sym->name);
501490f1617SYann E. MORIN 					cs->flags &= ~def_flags;
502c1a0f5e3SRoman Zippel 				}
5031da177e4SLinus Torvalds 				break;
5041da177e4SLinus Torvalds 			case yes:
505d84876f9SJan Engelhardt 				if (cs->def[def].tri != no)
506d84876f9SJan Engelhardt 					conf_warning("override: %s changes choice state", sym->name);
507669bfad9SRoman Zippel 				cs->def[def].val = sym;
5081da177e4SLinus Torvalds 				break;
5091da177e4SLinus Torvalds 			}
510d6ee3576SSam Ravnborg 			cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
5111da177e4SLinus Torvalds 		}
5121da177e4SLinus Torvalds 	}
5131a7a8c6fSCody Schafer 	free(line);
5141da177e4SLinus Torvalds 	fclose(in);
51590389160SRoman Zippel 	return 0;
51690389160SRoman Zippel }
51790389160SRoman Zippel 
51890389160SRoman Zippel int conf_read(const char *name)
51990389160SRoman Zippel {
5205d09598dSArnaud Lacombe 	struct symbol *sym;
52184dd95d4SMasahiro Yamada 	int conf_unsaved = 0;
5225d09598dSArnaud Lacombe 	int i;
52390389160SRoman Zippel 
5245ee54659SMasahiro Yamada 	conf_set_changed(false);
525ddc97cacSRoman Zippel 
5266b87b70cSAl Viro 	if (conf_read_simple(name, S_DEF_USER)) {
5276b87b70cSAl Viro 		sym_calc_value(modules_sym);
52890389160SRoman Zippel 		return 1;
5296b87b70cSAl Viro 	}
5306b87b70cSAl Viro 
5316b87b70cSAl Viro 	sym_calc_value(modules_sym);
53290389160SRoman Zippel 
5331da177e4SLinus Torvalds 	for_all_symbols(i, sym) {
5341da177e4SLinus Torvalds 		sym_calc_value(sym);
535693359f7SDirk Gouders 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
5365d09598dSArnaud Lacombe 			continue;
537c1a0f5e3SRoman Zippel 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
538c1a0f5e3SRoman Zippel 			/* check that calculated value agrees with saved value */
539c1a0f5e3SRoman Zippel 			switch (sym->type) {
540c1a0f5e3SRoman Zippel 			case S_BOOLEAN:
541c1a0f5e3SRoman Zippel 			case S_TRISTATE:
542e3cd5136SMasahiro Yamada 				if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
5435d09598dSArnaud Lacombe 					continue;
544e3cd5136SMasahiro Yamada 				break;
545c1a0f5e3SRoman Zippel 			default:
5460c1822e6SRoman Zippel 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
5475d09598dSArnaud Lacombe 					continue;
548c1a0f5e3SRoman Zippel 				break;
549c1a0f5e3SRoman Zippel 			}
550c1a0f5e3SRoman Zippel 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
551c1a0f5e3SRoman Zippel 			/* no previous value and not saved */
5525d09598dSArnaud Lacombe 			continue;
553c1a0f5e3SRoman Zippel 		conf_unsaved++;
554c1a0f5e3SRoman Zippel 		/* maybe print value in verbose mode... */
5551da177e4SLinus Torvalds 	}
5561da177e4SLinus Torvalds 
557d8982ba1SRoman Zippel 	for_all_symbols(i, sym) {
558d8982ba1SRoman Zippel 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
559d8982ba1SRoman Zippel 			/* Reset values of generates values, so they'll appear
560d8982ba1SRoman Zippel 			 * as new, if they should become visible, but that
561d8982ba1SRoman Zippel 			 * doesn't quite work if the Kconfig and the saved
562d8982ba1SRoman Zippel 			 * configuration disagree.
563d8982ba1SRoman Zippel 			 */
564d8982ba1SRoman Zippel 			if (sym->visible == no && !conf_unsaved)
565d8982ba1SRoman Zippel 				sym->flags &= ~SYMBOL_DEF_USER;
566d8982ba1SRoman Zippel 			switch (sym->type) {
567d8982ba1SRoman Zippel 			case S_STRING:
568d8982ba1SRoman Zippel 			case S_INT:
569d8982ba1SRoman Zippel 			case S_HEX:
570d8982ba1SRoman Zippel 				/* Reset a string value if it's out of range */
571d8982ba1SRoman Zippel 				if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
572d8982ba1SRoman Zippel 					break;
573d8982ba1SRoman Zippel 				sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
574d8982ba1SRoman Zippel 				conf_unsaved++;
575d8982ba1SRoman Zippel 				break;
576d8982ba1SRoman Zippel 			default:
577d8982ba1SRoman Zippel 				break;
578d8982ba1SRoman Zippel 			}
579d8982ba1SRoman Zippel 		}
580d8982ba1SRoman Zippel 	}
581d8982ba1SRoman Zippel 
5825ee54659SMasahiro Yamada 	if (conf_warnings || conf_unsaved)
5835ee54659SMasahiro Yamada 		conf_set_changed(true);
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	return 0;
5861da177e4SLinus Torvalds }
5871da177e4SLinus Torvalds 
588ca51b26bSMasahiro Yamada struct comment_style {
589ca51b26bSMasahiro Yamada 	const char *decoration;
590ca51b26bSMasahiro Yamada 	const char *prefix;
591ca51b26bSMasahiro Yamada 	const char *postfix;
592ca51b26bSMasahiro Yamada };
593ca51b26bSMasahiro Yamada 
594ca51b26bSMasahiro Yamada static const struct comment_style comment_style_pound = {
595ca51b26bSMasahiro Yamada 	.decoration = "#",
596ca51b26bSMasahiro Yamada 	.prefix = "#",
597ca51b26bSMasahiro Yamada 	.postfix = "#",
598ca51b26bSMasahiro Yamada };
599ca51b26bSMasahiro Yamada 
600ca51b26bSMasahiro Yamada static const struct comment_style comment_style_c = {
601ca51b26bSMasahiro Yamada 	.decoration = " *",
602ca51b26bSMasahiro Yamada 	.prefix = "/*",
603ca51b26bSMasahiro Yamada 	.postfix = " */",
604ca51b26bSMasahiro Yamada };
605ca51b26bSMasahiro Yamada 
606ca51b26bSMasahiro Yamada static void conf_write_heading(FILE *fp, const struct comment_style *cs)
607ca51b26bSMasahiro Yamada {
608ca51b26bSMasahiro Yamada 	fprintf(fp, "%s\n", cs->prefix);
609ca51b26bSMasahiro Yamada 
610ca51b26bSMasahiro Yamada 	fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n",
611ca51b26bSMasahiro Yamada 		cs->decoration);
612ca51b26bSMasahiro Yamada 
613ca51b26bSMasahiro Yamada 	fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text);
614ca51b26bSMasahiro Yamada 
615ca51b26bSMasahiro Yamada 	fprintf(fp, "%s\n", cs->postfix);
616ca51b26bSMasahiro Yamada }
617ca51b26bSMasahiro Yamada 
61880f7bc77SMasahiro Yamada /* The returned pointer must be freed on the caller side */
61980f7bc77SMasahiro Yamada static char *escape_string_value(const char *in)
62080f7bc77SMasahiro Yamada {
62180f7bc77SMasahiro Yamada 	const char *p;
62280f7bc77SMasahiro Yamada 	char *out;
62380f7bc77SMasahiro Yamada 	size_t len;
62480f7bc77SMasahiro Yamada 
62580f7bc77SMasahiro Yamada 	len = strlen(in) + strlen("\"\"") + 1;
62680f7bc77SMasahiro Yamada 
62780f7bc77SMasahiro Yamada 	p = in;
62880f7bc77SMasahiro Yamada 	while (1) {
62980f7bc77SMasahiro Yamada 		p += strcspn(p, "\"\\");
63080f7bc77SMasahiro Yamada 
63180f7bc77SMasahiro Yamada 		if (p[0] == '\0')
63280f7bc77SMasahiro Yamada 			break;
63380f7bc77SMasahiro Yamada 
63480f7bc77SMasahiro Yamada 		len++;
63580f7bc77SMasahiro Yamada 		p++;
63680f7bc77SMasahiro Yamada 	}
63780f7bc77SMasahiro Yamada 
63880f7bc77SMasahiro Yamada 	out = xmalloc(len);
63980f7bc77SMasahiro Yamada 	out[0] = '\0';
64080f7bc77SMasahiro Yamada 
64180f7bc77SMasahiro Yamada 	strcat(out, "\"");
64280f7bc77SMasahiro Yamada 
64380f7bc77SMasahiro Yamada 	p = in;
64480f7bc77SMasahiro Yamada 	while (1) {
64580f7bc77SMasahiro Yamada 		len = strcspn(p, "\"\\");
64680f7bc77SMasahiro Yamada 		strncat(out, p, len);
64780f7bc77SMasahiro Yamada 		p += len;
64880f7bc77SMasahiro Yamada 
64980f7bc77SMasahiro Yamada 		if (p[0] == '\0')
65080f7bc77SMasahiro Yamada 			break;
65180f7bc77SMasahiro Yamada 
65280f7bc77SMasahiro Yamada 		strcat(out, "\\");
65380f7bc77SMasahiro Yamada 		strncat(out, p++, 1);
65480f7bc77SMasahiro Yamada 	}
65580f7bc77SMasahiro Yamada 
65680f7bc77SMasahiro Yamada 	strcat(out, "\"");
65780f7bc77SMasahiro Yamada 
65880f7bc77SMasahiro Yamada 	return out;
65980f7bc77SMasahiro Yamada }
66080f7bc77SMasahiro Yamada 
661e54e692bSArnaud Lacombe /*
662e54e692bSArnaud Lacombe  * Kconfig configuration printer
663e54e692bSArnaud Lacombe  *
664e54e692bSArnaud Lacombe  * This printer is used when generating the resulting configuration after
665e54e692bSArnaud Lacombe  * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
666e54e692bSArnaud Lacombe  * passing a non-NULL argument to the printer.
667e54e692bSArnaud Lacombe  */
6686ce45a91SMasahiro Yamada enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
66949192f26SSam Ravnborg 
6706ce45a91SMasahiro Yamada static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
6716ce45a91SMasahiro Yamada 			   bool escape_string)
672e54e692bSArnaud Lacombe {
673229d0cfaSMasahiro Yamada 	const char *val;
674229d0cfaSMasahiro Yamada 	char *escaped = NULL;
675e54e692bSArnaud Lacombe 
676229d0cfaSMasahiro Yamada 	if (sym->type == S_UNKNOWN)
677229d0cfaSMasahiro Yamada 		return;
678229d0cfaSMasahiro Yamada 
679229d0cfaSMasahiro Yamada 	val = sym_get_string_value(sym);
680229d0cfaSMasahiro Yamada 
6816ce45a91SMasahiro Yamada 	if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
6826ce45a91SMasahiro Yamada 	    output_n != OUTPUT_N && *val == 'n') {
6836ce45a91SMasahiro Yamada 		if (output_n == OUTPUT_N_AS_UNSET)
6846ce45a91SMasahiro Yamada 			fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
6856ce45a91SMasahiro Yamada 		return;
6866ce45a91SMasahiro Yamada 	}
6876ce45a91SMasahiro Yamada 
6886ce45a91SMasahiro Yamada 	if (sym->type == S_STRING && escape_string) {
68980f7bc77SMasahiro Yamada 		escaped = escape_string_value(val);
690229d0cfaSMasahiro Yamada 		val = escaped;
69149192f26SSam Ravnborg 	}
692229d0cfaSMasahiro Yamada 
6936ce45a91SMasahiro Yamada 	fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
6946ce45a91SMasahiro Yamada 
6956ce45a91SMasahiro Yamada 	free(escaped);
6966ce45a91SMasahiro Yamada }
6976ce45a91SMasahiro Yamada 
6986ce45a91SMasahiro Yamada static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
6996ce45a91SMasahiro Yamada {
7006ce45a91SMasahiro Yamada 	__print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
7016ce45a91SMasahiro Yamada }
7026ce45a91SMasahiro Yamada 
7036ce45a91SMasahiro Yamada static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
7046ce45a91SMasahiro Yamada {
705*129ab0d2SMasahiro Yamada 	__print_symbol(fp, sym, OUTPUT_N_NONE, false);
7066ce45a91SMasahiro Yamada }
7076ce45a91SMasahiro Yamada 
70851d792cbSMasahiro Yamada void print_symbol_for_listconfig(struct symbol *sym)
70951d792cbSMasahiro Yamada {
71051d792cbSMasahiro Yamada 	__print_symbol(stdout, sym, OUTPUT_N, true);
71151d792cbSMasahiro Yamada }
71251d792cbSMasahiro Yamada 
7136ce45a91SMasahiro Yamada static void print_symbol_for_c(FILE *fp, struct symbol *sym)
7146ce45a91SMasahiro Yamada {
7156ce45a91SMasahiro Yamada 	const char *val;
7166ce45a91SMasahiro Yamada 	const char *sym_suffix = "";
7176ce45a91SMasahiro Yamada 	const char *val_prefix = "";
7186ce45a91SMasahiro Yamada 	char *escaped = NULL;
7196ce45a91SMasahiro Yamada 
7206ce45a91SMasahiro Yamada 	if (sym->type == S_UNKNOWN)
7216ce45a91SMasahiro Yamada 		return;
7226ce45a91SMasahiro Yamada 
7236ce45a91SMasahiro Yamada 	val = sym_get_string_value(sym);
7246ce45a91SMasahiro Yamada 
7256ce45a91SMasahiro Yamada 	switch (sym->type) {
7266ce45a91SMasahiro Yamada 	case S_BOOLEAN:
7276ce45a91SMasahiro Yamada 	case S_TRISTATE:
7286ce45a91SMasahiro Yamada 		switch (*val) {
7296ce45a91SMasahiro Yamada 		case 'n':
7306ce45a91SMasahiro Yamada 			return;
7316ce45a91SMasahiro Yamada 		case 'm':
7326ce45a91SMasahiro Yamada 			sym_suffix = "_MODULE";
7336ce45a91SMasahiro Yamada 			/* fall through */
7346ce45a91SMasahiro Yamada 		default:
7356ce45a91SMasahiro Yamada 			val = "1";
7366ce45a91SMasahiro Yamada 		}
7376ce45a91SMasahiro Yamada 		break;
7386ce45a91SMasahiro Yamada 	case S_HEX:
7396ce45a91SMasahiro Yamada 		if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
7406ce45a91SMasahiro Yamada 			val_prefix = "0x";
7416ce45a91SMasahiro Yamada 		break;
7426ce45a91SMasahiro Yamada 	case S_STRING:
74380f7bc77SMasahiro Yamada 		escaped = escape_string_value(val);
7446ce45a91SMasahiro Yamada 		val = escaped;
7456ce45a91SMasahiro Yamada 	default:
7466ce45a91SMasahiro Yamada 		break;
7476ce45a91SMasahiro Yamada 	}
7486ce45a91SMasahiro Yamada 
7496ce45a91SMasahiro Yamada 	fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
7506ce45a91SMasahiro Yamada 		val_prefix, val);
751229d0cfaSMasahiro Yamada 
752229d0cfaSMasahiro Yamada 	free(escaped);
75349192f26SSam Ravnborg }
75449192f26SSam Ravnborg 
7557cf3d73bSSam Ravnborg /*
7567cf3d73bSSam Ravnborg  * Write out a minimal config.
7577cf3d73bSSam Ravnborg  * All values that has default values are skipped as this is redundant.
7587cf3d73bSSam Ravnborg  */
7597cf3d73bSSam Ravnborg int conf_write_defconfig(const char *filename)
7607cf3d73bSSam Ravnborg {
7617cf3d73bSSam Ravnborg 	struct symbol *sym;
7627cf3d73bSSam Ravnborg 	struct menu *menu;
7637cf3d73bSSam Ravnborg 	FILE *out;
7647cf3d73bSSam Ravnborg 
7657cf3d73bSSam Ravnborg 	out = fopen(filename, "w");
7667cf3d73bSSam Ravnborg 	if (!out)
7677cf3d73bSSam Ravnborg 		return 1;
7687cf3d73bSSam Ravnborg 
7697cf3d73bSSam Ravnborg 	sym_clear_all_valid();
7707cf3d73bSSam Ravnborg 
7717cf3d73bSSam Ravnborg 	/* Traverse all menus to find all relevant symbols */
7727cf3d73bSSam Ravnborg 	menu = rootmenu.list;
7737cf3d73bSSam Ravnborg 
7747cf3d73bSSam Ravnborg 	while (menu != NULL)
7757cf3d73bSSam Ravnborg 	{
7767cf3d73bSSam Ravnborg 		sym = menu->sym;
7777cf3d73bSSam Ravnborg 		if (sym == NULL) {
7787cf3d73bSSam Ravnborg 			if (!menu_is_visible(menu))
7797cf3d73bSSam Ravnborg 				goto next_menu;
7807cf3d73bSSam Ravnborg 		} else if (!sym_is_choice(sym)) {
7817cf3d73bSSam Ravnborg 			sym_calc_value(sym);
7827cf3d73bSSam Ravnborg 			if (!(sym->flags & SYMBOL_WRITE))
7837cf3d73bSSam Ravnborg 				goto next_menu;
7847cf3d73bSSam Ravnborg 			sym->flags &= ~SYMBOL_WRITE;
7857cf3d73bSSam Ravnborg 			/* If we cannot change the symbol - skip */
786baa23ec8SMarco Ammon 			if (!sym_is_changeable(sym))
7877cf3d73bSSam Ravnborg 				goto next_menu;
7887cf3d73bSSam Ravnborg 			/* If symbol equals to default value - skip */
7897cf3d73bSSam Ravnborg 			if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
7907cf3d73bSSam Ravnborg 				goto next_menu;
7917cf3d73bSSam Ravnborg 
7927cf3d73bSSam Ravnborg 			/*
7937cf3d73bSSam Ravnborg 			 * If symbol is a choice value and equals to the
7947cf3d73bSSam Ravnborg 			 * default for a choice - skip.
79584062dd3SSam Ravnborg 			 * But only if value is bool and equal to "y" and
79684062dd3SSam Ravnborg 			 * choice is not "optional".
79784062dd3SSam Ravnborg 			 * (If choice is "optional" then all values can be "n")
7987cf3d73bSSam Ravnborg 			 */
7997cf3d73bSSam Ravnborg 			if (sym_is_choice_value(sym)) {
8007cf3d73bSSam Ravnborg 				struct symbol *cs;
8017cf3d73bSSam Ravnborg 				struct symbol *ds;
8027cf3d73bSSam Ravnborg 
8037cf3d73bSSam Ravnborg 				cs = prop_get_symbol(sym_get_choice_prop(sym));
8047cf3d73bSSam Ravnborg 				ds = sym_choice_default(cs);
80584062dd3SSam Ravnborg 				if (!sym_is_optional(cs) && sym == ds) {
806801690caSSam Ravnborg 					if ((sym->type == S_BOOLEAN) &&
8077cf3d73bSSam Ravnborg 					    sym_get_tristate_value(sym) == yes)
8087cf3d73bSSam Ravnborg 						goto next_menu;
8097cf3d73bSSam Ravnborg 				}
8107cf3d73bSSam Ravnborg 			}
8116ce45a91SMasahiro Yamada 			print_symbol_for_dotconfig(out, sym);
8127cf3d73bSSam Ravnborg 		}
8137cf3d73bSSam Ravnborg next_menu:
8147cf3d73bSSam Ravnborg 		if (menu->list != NULL) {
8157cf3d73bSSam Ravnborg 			menu = menu->list;
8167cf3d73bSSam Ravnborg 		}
8177cf3d73bSSam Ravnborg 		else if (menu->next != NULL) {
8187cf3d73bSSam Ravnborg 			menu = menu->next;
8197cf3d73bSSam Ravnborg 		} else {
8207cf3d73bSSam Ravnborg 			while ((menu = menu->parent)) {
8217cf3d73bSSam Ravnborg 				if (menu->next != NULL) {
8227cf3d73bSSam Ravnborg 					menu = menu->next;
8237cf3d73bSSam Ravnborg 					break;
8247cf3d73bSSam Ravnborg 				}
8257cf3d73bSSam Ravnborg 			}
8267cf3d73bSSam Ravnborg 		}
8277cf3d73bSSam Ravnborg 	}
8287cf3d73bSSam Ravnborg 	fclose(out);
8297cf3d73bSSam Ravnborg 	return 0;
8307cf3d73bSSam Ravnborg }
8317cf3d73bSSam Ravnborg 
8321da177e4SLinus Torvalds int conf_write(const char *name)
8331da177e4SLinus Torvalds {
834c955ccafSRoman Zippel 	FILE *out;
8351da177e4SLinus Torvalds 	struct symbol *sym;
8361da177e4SLinus Torvalds 	struct menu *menu;
8371da177e4SLinus Torvalds 	const char *str;
838ceb7f329SMasahiro Yamada 	char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
8391da177e4SLinus Torvalds 	char *env;
8400c5b6c28SM. Vefa Bicakci 	int i;
841aff11cd9SAlexander Popov 	bool need_newline = false;
8421da177e4SLinus Torvalds 
843ceb7f329SMasahiro Yamada 	if (!name)
844ceb7f329SMasahiro Yamada 		name = conf_get_configname();
845ceb7f329SMasahiro Yamada 
846ceb7f329SMasahiro Yamada 	if (!*name) {
847ceb7f329SMasahiro Yamada 		fprintf(stderr, "config name is empty\n");
848ceb7f329SMasahiro Yamada 		return -1;
849ceb7f329SMasahiro Yamada 	}
8501da177e4SLinus Torvalds 
8510608182aSMasahiro Yamada 	if (is_dir(name)) {
852ceb7f329SMasahiro Yamada 		fprintf(stderr, "%s: Is a directory\n", name);
853ceb7f329SMasahiro Yamada 		return -1;
854ceb7f329SMasahiro Yamada 	}
8551da177e4SLinus Torvalds 
856580c5b3eSMasahiro Yamada 	if (make_parent_dir(name))
857580c5b3eSMasahiro Yamada 		return -1;
858580c5b3eSMasahiro Yamada 
85914cdd3c4SRoman Zippel 	env = getenv("KCONFIG_OVERWRITECONFIG");
860ceb7f329SMasahiro Yamada 	if (env && *env) {
86114cdd3c4SRoman Zippel 		*tmpname = 0;
862ceb7f329SMasahiro Yamada 		out = fopen(name, "w");
863ceb7f329SMasahiro Yamada 	} else {
864ceb7f329SMasahiro Yamada 		snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
865ceb7f329SMasahiro Yamada 			 name, (int)getpid());
866ceb7f329SMasahiro Yamada 		out = fopen(tmpname, "w");
86714cdd3c4SRoman Zippel 	}
8681da177e4SLinus Torvalds 	if (!out)
8691da177e4SLinus Torvalds 		return 1;
87014cdd3c4SRoman Zippel 
871ca51b26bSMasahiro Yamada 	conf_write_heading(out, &comment_style_pound);
8721da177e4SLinus Torvalds 
873b3214293SKarsten Wiese 	if (!conf_get_changed())
8741da177e4SLinus Torvalds 		sym_clear_all_valid();
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	menu = rootmenu.list;
8771da177e4SLinus Torvalds 	while (menu) {
8781da177e4SLinus Torvalds 		sym = menu->sym;
8791da177e4SLinus Torvalds 		if (!sym) {
8801da177e4SLinus Torvalds 			if (!menu_is_visible(menu))
8811da177e4SLinus Torvalds 				goto next;
8821da177e4SLinus Torvalds 			str = menu_get_prompt(menu);
8831da177e4SLinus Torvalds 			fprintf(out, "\n"
8841da177e4SLinus Torvalds 				     "#\n"
8851da177e4SLinus Torvalds 				     "# %s\n"
8861da177e4SLinus Torvalds 				     "#\n", str);
887aff11cd9SAlexander Popov 			need_newline = false;
8888e2442a5SMasahiro Yamada 		} else if (!(sym->flags & SYMBOL_CHOICE) &&
8898e2442a5SMasahiro Yamada 			   !(sym->flags & SYMBOL_WRITTEN)) {
8901da177e4SLinus Torvalds 			sym_calc_value(sym);
8911da177e4SLinus Torvalds 			if (!(sym->flags & SYMBOL_WRITE))
8921da177e4SLinus Torvalds 				goto next;
893aff11cd9SAlexander Popov 			if (need_newline) {
894aff11cd9SAlexander Popov 				fprintf(out, "\n");
895aff11cd9SAlexander Popov 				need_newline = false;
896aff11cd9SAlexander Popov 			}
8978e2442a5SMasahiro Yamada 			sym->flags |= SYMBOL_WRITTEN;
8986ce45a91SMasahiro Yamada 			print_symbol_for_dotconfig(out, sym);
8991da177e4SLinus Torvalds 		}
9001da177e4SLinus Torvalds 
9011da177e4SLinus Torvalds next:
9021da177e4SLinus Torvalds 		if (menu->list) {
9031da177e4SLinus Torvalds 			menu = menu->list;
9041da177e4SLinus Torvalds 			continue;
9051da177e4SLinus Torvalds 		}
9061da177e4SLinus Torvalds 		if (menu->next)
9071da177e4SLinus Torvalds 			menu = menu->next;
9081da177e4SLinus Torvalds 		else while ((menu = menu->parent)) {
909aff11cd9SAlexander Popov 			if (!menu->sym && menu_is_visible(menu) &&
910aff11cd9SAlexander Popov 			    menu != &rootmenu) {
911aff11cd9SAlexander Popov 				str = menu_get_prompt(menu);
912aff11cd9SAlexander Popov 				fprintf(out, "# end of %s\n", str);
913aff11cd9SAlexander Popov 				need_newline = true;
914aff11cd9SAlexander Popov 			}
9151da177e4SLinus Torvalds 			if (menu->next) {
9161da177e4SLinus Torvalds 				menu = menu->next;
9171da177e4SLinus Torvalds 				break;
9181da177e4SLinus Torvalds 			}
9191da177e4SLinus Torvalds 		}
9201da177e4SLinus Torvalds 	}
9211da177e4SLinus Torvalds 	fclose(out);
92214cdd3c4SRoman Zippel 
9230c5b6c28SM. Vefa Bicakci 	for_all_symbols(i, sym)
9240c5b6c28SM. Vefa Bicakci 		sym->flags &= ~SYMBOL_WRITTEN;
9250c5b6c28SM. Vefa Bicakci 
92614cdd3c4SRoman Zippel 	if (*tmpname) {
92767424f61SMasahiro Yamada 		if (is_same(name, tmpname)) {
92867424f61SMasahiro Yamada 			conf_message("No change to %s", name);
92967424f61SMasahiro Yamada 			unlink(tmpname);
9305ee54659SMasahiro Yamada 			conf_set_changed(false);
93167424f61SMasahiro Yamada 			return 0;
93267424f61SMasahiro Yamada 		}
93367424f61SMasahiro Yamada 
934ceb7f329SMasahiro Yamada 		snprintf(oldname, sizeof(oldname), "%s.old", name);
935ceb7f329SMasahiro Yamada 		rename(name, oldname);
936ceb7f329SMasahiro Yamada 		if (rename(tmpname, name))
9371da177e4SLinus Torvalds 			return 1;
93814cdd3c4SRoman Zippel 	}
9391da177e4SLinus Torvalds 
940ceb7f329SMasahiro Yamada 	conf_message("configuration written to %s", name);
941ddc97cacSRoman Zippel 
9425ee54659SMasahiro Yamada 	conf_set_changed(false);
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	return 0;
9451da177e4SLinus Torvalds }
946c955ccafSRoman Zippel 
947a2ff4040SMasahiro Yamada /* write a dependency file as used by kbuild to track dependencies */
94800d674cbSMasahiro Yamada static int conf_write_autoconf_cmd(const char *autoconf_name)
949a2ff4040SMasahiro Yamada {
95000d674cbSMasahiro Yamada 	char name[PATH_MAX], tmp[PATH_MAX];
951a2ff4040SMasahiro Yamada 	struct file *file;
952a2ff4040SMasahiro Yamada 	FILE *out;
95300d674cbSMasahiro Yamada 	int ret;
954a2ff4040SMasahiro Yamada 
95500d674cbSMasahiro Yamada 	ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
95600d674cbSMasahiro Yamada 	if (ret >= sizeof(name)) /* check truncation */
95700d674cbSMasahiro Yamada 		return -1;
95879123b13SMasahiro Yamada 
95979123b13SMasahiro Yamada 	if (make_parent_dir(name))
96000d674cbSMasahiro Yamada 		return -1;
96100d674cbSMasahiro Yamada 
96200d674cbSMasahiro Yamada 	ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
96300d674cbSMasahiro Yamada 	if (ret >= sizeof(tmp)) /* check truncation */
96400d674cbSMasahiro Yamada 		return -1;
96500d674cbSMasahiro Yamada 
96600d674cbSMasahiro Yamada 	out = fopen(tmp, "w");
96700d674cbSMasahiro Yamada 	if (!out) {
96800d674cbSMasahiro Yamada 		perror("fopen");
96900d674cbSMasahiro Yamada 		return -1;
97000d674cbSMasahiro Yamada 	}
97100d674cbSMasahiro Yamada 
97200d674cbSMasahiro Yamada 	fprintf(out, "deps_config := \\\n");
97300d674cbSMasahiro Yamada 	for (file = file_list; file; file = file->next)
97400d674cbSMasahiro Yamada 		fprintf(out, "\t%s \\\n", file->name);
97500d674cbSMasahiro Yamada 
97600d674cbSMasahiro Yamada 	fprintf(out, "\n%s: $(deps_config)\n\n", autoconf_name);
97700d674cbSMasahiro Yamada 
97800d674cbSMasahiro Yamada 	env_write_dep(out, autoconf_name);
97900d674cbSMasahiro Yamada 
98000d674cbSMasahiro Yamada 	fprintf(out, "\n$(deps_config): ;\n");
98100d674cbSMasahiro Yamada 
98200d674cbSMasahiro Yamada 	if (ferror(out)) /* error check for all fprintf() calls */
98300d674cbSMasahiro Yamada 		return -1;
98400d674cbSMasahiro Yamada 
98500d674cbSMasahiro Yamada 	fclose(out);
98600d674cbSMasahiro Yamada 
98700d674cbSMasahiro Yamada 	if (rename(tmp, name)) {
98800d674cbSMasahiro Yamada 		perror("rename");
98900d674cbSMasahiro Yamada 		return -1;
99000d674cbSMasahiro Yamada 	}
99100d674cbSMasahiro Yamada 
992a2ff4040SMasahiro Yamada 	return 0;
993a2ff4040SMasahiro Yamada }
994a2ff4040SMasahiro Yamada 
9950849d212SMasahiro Yamada static int conf_touch_deps(void)
9962e3646e5SRoman Zippel {
99712122f62SMarkus Heidelberg 	const char *name;
9982e3646e5SRoman Zippel 	struct symbol *sym;
9991508fec8SMasahiro Yamada 	int res, i;
10001508fec8SMasahiro Yamada 
10011508fec8SMasahiro Yamada 	strcpy(depfile_path, "include/config/");
10021508fec8SMasahiro Yamada 	depfile_prefix_len = strlen(depfile_path);
10032e3646e5SRoman Zippel 
100412122f62SMarkus Heidelberg 	name = conf_get_autoconfig_name();
10052e3646e5SRoman Zippel 	conf_read_simple(name, S_DEF_AUTO);
10066b87b70cSAl Viro 	sym_calc_value(modules_sym);
10072e3646e5SRoman Zippel 
10082e3646e5SRoman Zippel 	for_all_symbols(i, sym) {
10092e3646e5SRoman Zippel 		sym_calc_value(sym);
1010693359f7SDirk Gouders 		if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
10112e3646e5SRoman Zippel 			continue;
10122e3646e5SRoman Zippel 		if (sym->flags & SYMBOL_WRITE) {
10132e3646e5SRoman Zippel 			if (sym->flags & SYMBOL_DEF_AUTO) {
10142e3646e5SRoman Zippel 				/*
10152e3646e5SRoman Zippel 				 * symbol has old and new value,
10162e3646e5SRoman Zippel 				 * so compare them...
10172e3646e5SRoman Zippel 				 */
10182e3646e5SRoman Zippel 				switch (sym->type) {
10192e3646e5SRoman Zippel 				case S_BOOLEAN:
10202e3646e5SRoman Zippel 				case S_TRISTATE:
10212e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) ==
10222e3646e5SRoman Zippel 					    sym->def[S_DEF_AUTO].tri)
10232e3646e5SRoman Zippel 						continue;
10242e3646e5SRoman Zippel 					break;
10252e3646e5SRoman Zippel 				case S_STRING:
10262e3646e5SRoman Zippel 				case S_HEX:
10272e3646e5SRoman Zippel 				case S_INT:
10282e3646e5SRoman Zippel 					if (!strcmp(sym_get_string_value(sym),
10292e3646e5SRoman Zippel 						    sym->def[S_DEF_AUTO].val))
10302e3646e5SRoman Zippel 						continue;
10312e3646e5SRoman Zippel 					break;
10322e3646e5SRoman Zippel 				default:
10332e3646e5SRoman Zippel 					break;
10342e3646e5SRoman Zippel 				}
10352e3646e5SRoman Zippel 			} else {
10362e3646e5SRoman Zippel 				/*
10372e3646e5SRoman Zippel 				 * If there is no old value, only 'no' (unset)
10382e3646e5SRoman Zippel 				 * is allowed as new value.
10392e3646e5SRoman Zippel 				 */
10402e3646e5SRoman Zippel 				switch (sym->type) {
10412e3646e5SRoman Zippel 				case S_BOOLEAN:
10422e3646e5SRoman Zippel 				case S_TRISTATE:
10432e3646e5SRoman Zippel 					if (sym_get_tristate_value(sym) == no)
10442e3646e5SRoman Zippel 						continue;
10452e3646e5SRoman Zippel 					break;
10462e3646e5SRoman Zippel 				default:
10472e3646e5SRoman Zippel 					break;
10482e3646e5SRoman Zippel 				}
10492e3646e5SRoman Zippel 			}
10502e3646e5SRoman Zippel 		} else if (!(sym->flags & SYMBOL_DEF_AUTO))
10512e3646e5SRoman Zippel 			/* There is neither an old nor a new value. */
10522e3646e5SRoman Zippel 			continue;
10532e3646e5SRoman Zippel 		/* else
10542e3646e5SRoman Zippel 		 *	There is an old value, but no new value ('no' (unset)
10552e3646e5SRoman Zippel 		 *	isn't saved in auto.conf, so the old value is always
10562e3646e5SRoman Zippel 		 *	different from 'no').
10572e3646e5SRoman Zippel 		 */
10582e3646e5SRoman Zippel 
10591508fec8SMasahiro Yamada 		res = conf_touch_dep(sym->name);
10601508fec8SMasahiro Yamada 		if (res)
10612e3646e5SRoman Zippel 			return res;
10622e3646e5SRoman Zippel 	}
10632e3646e5SRoman Zippel 
10641508fec8SMasahiro Yamada 	return 0;
10651508fec8SMasahiro Yamada }
10661508fec8SMasahiro Yamada 
106757ddd07cSMasahiro Yamada static int __conf_write_autoconf(const char *filename,
106857ddd07cSMasahiro Yamada 				 void (*print_symbol)(FILE *, struct symbol *),
106957ddd07cSMasahiro Yamada 				 const struct comment_style *comment_style)
107057ddd07cSMasahiro Yamada {
107157ddd07cSMasahiro Yamada 	char tmp[PATH_MAX];
107257ddd07cSMasahiro Yamada 	FILE *file;
107357ddd07cSMasahiro Yamada 	struct symbol *sym;
107457ddd07cSMasahiro Yamada 	int ret, i;
107557ddd07cSMasahiro Yamada 
107657ddd07cSMasahiro Yamada 	if (make_parent_dir(filename))
107757ddd07cSMasahiro Yamada 		return -1;
107857ddd07cSMasahiro Yamada 
107957ddd07cSMasahiro Yamada 	ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
108057ddd07cSMasahiro Yamada 	if (ret >= sizeof(tmp)) /* check truncation */
108157ddd07cSMasahiro Yamada 		return -1;
108257ddd07cSMasahiro Yamada 
108357ddd07cSMasahiro Yamada 	file = fopen(tmp, "w");
108457ddd07cSMasahiro Yamada 	if (!file) {
108557ddd07cSMasahiro Yamada 		perror("fopen");
108657ddd07cSMasahiro Yamada 		return -1;
108757ddd07cSMasahiro Yamada 	}
108857ddd07cSMasahiro Yamada 
108957ddd07cSMasahiro Yamada 	conf_write_heading(file, comment_style);
109057ddd07cSMasahiro Yamada 
109157ddd07cSMasahiro Yamada 	for_all_symbols(i, sym)
109257ddd07cSMasahiro Yamada 		if ((sym->flags & SYMBOL_WRITE) && sym->name)
109357ddd07cSMasahiro Yamada 			print_symbol(file, sym);
109457ddd07cSMasahiro Yamada 
109557ddd07cSMasahiro Yamada 	/* check possible errors in conf_write_heading() and print_symbol() */
109657ddd07cSMasahiro Yamada 	if (ferror(file))
109757ddd07cSMasahiro Yamada 		return -1;
109857ddd07cSMasahiro Yamada 
109957ddd07cSMasahiro Yamada 	fclose(file);
110057ddd07cSMasahiro Yamada 
110157ddd07cSMasahiro Yamada 	if (rename(tmp, filename)) {
110257ddd07cSMasahiro Yamada 		perror("rename");
110357ddd07cSMasahiro Yamada 		return -1;
110457ddd07cSMasahiro Yamada 	}
110557ddd07cSMasahiro Yamada 
110657ddd07cSMasahiro Yamada 	return 0;
110757ddd07cSMasahiro Yamada }
110857ddd07cSMasahiro Yamada 
110900c864f8SMasahiro Yamada int conf_write_autoconf(int overwrite)
1110c955ccafSRoman Zippel {
1111c955ccafSRoman Zippel 	struct symbol *sym;
111200c864f8SMasahiro Yamada 	const char *autoconf_name = conf_get_autoconfig_name();
111357ddd07cSMasahiro Yamada 	int ret, i;
1114c955ccafSRoman Zippel 
111500c864f8SMasahiro Yamada 	if (!overwrite && is_present(autoconf_name))
111600c864f8SMasahiro Yamada 		return 0;
111700c864f8SMasahiro Yamada 
111800d674cbSMasahiro Yamada 	ret = conf_write_autoconf_cmd(autoconf_name);
111900d674cbSMasahiro Yamada 	if (ret)
112000d674cbSMasahiro Yamada 		return -1;
1121c955ccafSRoman Zippel 
11220849d212SMasahiro Yamada 	if (conf_touch_deps())
11232e3646e5SRoman Zippel 		return 1;
11242e3646e5SRoman Zippel 
112557ddd07cSMasahiro Yamada 	for_all_symbols(i, sym)
1126953742c8SArnaud Lacombe 		sym_calc_value(sym);
1127953742c8SArnaud Lacombe 
112857ddd07cSMasahiro Yamada 	ret = __conf_write_autoconf(conf_get_autoheader_name(),
112957ddd07cSMasahiro Yamada 				    print_symbol_for_c,
113057ddd07cSMasahiro Yamada 				    &comment_style_c);
113157ddd07cSMasahiro Yamada 	if (ret)
113257ddd07cSMasahiro Yamada 		return ret;
1133c955ccafSRoman Zippel 
1134c955ccafSRoman Zippel 	/*
113557ddd07cSMasahiro Yamada 	 * Create include/config/auto.conf. This must be the last step because
113657ddd07cSMasahiro Yamada 	 * Kbuild has a dependency on auto.conf and this marks the successful
113757ddd07cSMasahiro Yamada 	 * completion of the previous steps.
1138c955ccafSRoman Zippel 	 */
113957ddd07cSMasahiro Yamada 	ret = __conf_write_autoconf(conf_get_autoconfig_name(),
114057ddd07cSMasahiro Yamada 				    print_symbol_for_autoconf,
114157ddd07cSMasahiro Yamada 				    &comment_style_pound);
114257ddd07cSMasahiro Yamada 	if (ret)
114357ddd07cSMasahiro Yamada 		return ret;
1144c955ccafSRoman Zippel 
1145c955ccafSRoman Zippel 	return 0;
1146c955ccafSRoman Zippel }
1147b3214293SKarsten Wiese 
11485ee54659SMasahiro Yamada static bool conf_changed;
11493b354c55SKarsten Wiese static void (*conf_changed_callback)(void);
1150bfc10001SKarsten Wiese 
11515ee54659SMasahiro Yamada void conf_set_changed(bool val)
1152bfc10001SKarsten Wiese {
11535ee54659SMasahiro Yamada 	if (conf_changed_callback && conf_changed != val)
11543b354c55SKarsten Wiese 		conf_changed_callback();
1155bfc10001SKarsten Wiese 
11565ee54659SMasahiro Yamada 	conf_changed = val;
1157bfc10001SKarsten Wiese }
1158bfc10001SKarsten Wiese 
1159b3214293SKarsten Wiese bool conf_get_changed(void)
1160b3214293SKarsten Wiese {
11615ee54659SMasahiro Yamada 	return conf_changed;
1162b3214293SKarsten Wiese }
11633b354c55SKarsten Wiese 
11643b354c55SKarsten Wiese void conf_set_changed_callback(void (*fn)(void))
11653b354c55SKarsten Wiese {
11663b354c55SKarsten Wiese 	conf_changed_callback = fn;
11673b354c55SKarsten Wiese }
1168dc7862e5SRoman Zippel 
1169fbe98bb9SArve Hjønnevåg void set_all_choice_values(struct symbol *csym)
1170a64b44eaSSam Ravnborg {
1171a64b44eaSSam Ravnborg 	struct property *prop;
1172a64b44eaSSam Ravnborg 	struct symbol *sym;
1173a64b44eaSSam Ravnborg 	struct expr *e;
1174a64b44eaSSam Ravnborg 
1175a64b44eaSSam Ravnborg 	prop = sym_get_choice_prop(csym);
1176a64b44eaSSam Ravnborg 
1177a64b44eaSSam Ravnborg 	/*
1178a64b44eaSSam Ravnborg 	 * Set all non-assinged choice values to no
1179a64b44eaSSam Ravnborg 	 */
1180a64b44eaSSam Ravnborg 	expr_list_for_each_sym(prop->expr, e, sym) {
1181a64b44eaSSam Ravnborg 		if (!sym_has_value(sym))
1182a64b44eaSSam Ravnborg 			sym->def[S_DEF_USER].tri = no;
1183a64b44eaSSam Ravnborg 	}
1184a64b44eaSSam Ravnborg 	csym->flags |= SYMBOL_DEF_USER;
1185a64b44eaSSam Ravnborg 	/* clear VALID to get value calculated */
1186fbe98bb9SArve Hjønnevåg 	csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1187a64b44eaSSam Ravnborg }
1188