xref: /openbmc/linux/scripts/kconfig/confdata.c (revision 7a6ee0bb)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4  */
5 
6 #include <sys/mman.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <ctype.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <limits.h>
13 #include <stdarg.h>
14 #include <stdbool.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <time.h>
19 #include <unistd.h>
20 
21 #include "lkc.h"
22 
23 /* return true if 'path' exists, false otherwise */
24 static bool is_present(const char *path)
25 {
26 	struct stat st;
27 
28 	return !stat(path, &st);
29 }
30 
31 /* return true if 'path' exists and it is a directory, false otherwise */
32 static bool is_dir(const char *path)
33 {
34 	struct stat st;
35 
36 	if (stat(path, &st))
37 		return false;
38 
39 	return S_ISDIR(st.st_mode);
40 }
41 
42 /* return true if the given two files are the same, false otherwise */
43 static bool is_same(const char *file1, const char *file2)
44 {
45 	int fd1, fd2;
46 	struct stat st1, st2;
47 	void *map1, *map2;
48 	bool ret = false;
49 
50 	fd1 = open(file1, O_RDONLY);
51 	if (fd1 < 0)
52 		return ret;
53 
54 	fd2 = open(file2, O_RDONLY);
55 	if (fd2 < 0)
56 		goto close1;
57 
58 	ret = fstat(fd1, &st1);
59 	if (ret)
60 		goto close2;
61 	ret = fstat(fd2, &st2);
62 	if (ret)
63 		goto close2;
64 
65 	if (st1.st_size != st2.st_size)
66 		goto close2;
67 
68 	map1 = mmap(NULL, st1.st_size, PROT_READ, MAP_PRIVATE, fd1, 0);
69 	if (map1 == MAP_FAILED)
70 		goto close2;
71 
72 	map2 = mmap(NULL, st2.st_size, PROT_READ, MAP_PRIVATE, fd2, 0);
73 	if (map2 == MAP_FAILED)
74 		goto close2;
75 
76 	if (bcmp(map1, map2, st1.st_size))
77 		goto close2;
78 
79 	ret = true;
80 close2:
81 	close(fd2);
82 close1:
83 	close(fd1);
84 
85 	return ret;
86 }
87 
88 /*
89  * Create the parent directory of the given path.
90  *
91  * For example, if 'include/config/auto.conf' is given, create 'include/config'.
92  */
93 static int make_parent_dir(const char *path)
94 {
95 	char tmp[PATH_MAX + 1];
96 	char *p;
97 
98 	strncpy(tmp, path, sizeof(tmp));
99 	tmp[sizeof(tmp) - 1] = 0;
100 
101 	/* Remove the base name. Just return if nothing is left */
102 	p = strrchr(tmp, '/');
103 	if (!p)
104 		return 0;
105 	*(p + 1) = 0;
106 
107 	/* Just in case it is an absolute path */
108 	p = tmp;
109 	while (*p == '/')
110 		p++;
111 
112 	while ((p = strchr(p, '/'))) {
113 		*p = 0;
114 
115 		/* skip if the directory exists */
116 		if (!is_dir(tmp) && mkdir(tmp, 0755))
117 			return -1;
118 
119 		*p = '/';
120 		while (*p == '/')
121 			p++;
122 	}
123 
124 	return 0;
125 }
126 
127 static char depfile_path[PATH_MAX];
128 static size_t depfile_prefix_len;
129 
130 /* touch depfile for symbol 'name' */
131 static int conf_touch_dep(const char *name)
132 {
133 	int fd;
134 
135 	/* check overflow: prefix + name + '\0' must fit in buffer. */
136 	if (depfile_prefix_len + strlen(name) + 1 > sizeof(depfile_path))
137 		return -1;
138 
139 	strcpy(depfile_path + depfile_prefix_len, name);
140 
141 	fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
142 	if (fd == -1)
143 		return -1;
144 	close(fd);
145 
146 	return 0;
147 }
148 
149 static void conf_warning(const char *fmt, ...)
150 	__attribute__ ((format (printf, 1, 2)));
151 
152 static void conf_message(const char *fmt, ...)
153 	__attribute__ ((format (printf, 1, 2)));
154 
155 static const char *conf_filename;
156 static int conf_lineno, conf_warnings;
157 
158 static void conf_warning(const char *fmt, ...)
159 {
160 	va_list ap;
161 	va_start(ap, fmt);
162 	fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
163 	vfprintf(stderr, fmt, ap);
164 	fprintf(stderr, "\n");
165 	va_end(ap);
166 	conf_warnings++;
167 }
168 
169 static void conf_default_message_callback(const char *s)
170 {
171 	printf("#\n# ");
172 	printf("%s", s);
173 	printf("\n#\n");
174 }
175 
176 static void (*conf_message_callback)(const char *s) =
177 	conf_default_message_callback;
178 void conf_set_message_callback(void (*fn)(const char *s))
179 {
180 	conf_message_callback = fn;
181 }
182 
183 static void conf_message(const char *fmt, ...)
184 {
185 	va_list ap;
186 	char buf[4096];
187 
188 	if (!conf_message_callback)
189 		return;
190 
191 	va_start(ap, fmt);
192 
193 	vsnprintf(buf, sizeof(buf), fmt, ap);
194 	conf_message_callback(buf);
195 	va_end(ap);
196 }
197 
198 const char *conf_get_configname(void)
199 {
200 	char *name = getenv("KCONFIG_CONFIG");
201 
202 	return name ? name : ".config";
203 }
204 
205 static const char *conf_get_autoconfig_name(void)
206 {
207 	char *name = getenv("KCONFIG_AUTOCONFIG");
208 
209 	return name ? name : "include/config/auto.conf";
210 }
211 
212 static const char *conf_get_autoheader_name(void)
213 {
214 	char *name = getenv("KCONFIG_AUTOHEADER");
215 
216 	return name ? name : "include/generated/autoconf.h";
217 }
218 
219 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
220 {
221 	char *p2;
222 
223 	switch (sym->type) {
224 	case S_TRISTATE:
225 		if (p[0] == 'm') {
226 			sym->def[def].tri = mod;
227 			sym->flags |= def_flags;
228 			break;
229 		}
230 		/* fall through */
231 	case S_BOOLEAN:
232 		if (p[0] == 'y') {
233 			sym->def[def].tri = yes;
234 			sym->flags |= def_flags;
235 			break;
236 		}
237 		if (p[0] == 'n') {
238 			sym->def[def].tri = no;
239 			sym->flags |= def_flags;
240 			break;
241 		}
242 		if (def != S_DEF_AUTO)
243 			conf_warning("symbol value '%s' invalid for %s",
244 				     p, sym->name);
245 		return 1;
246 	case S_STRING:
247 		/* No escaping for S_DEF_AUTO (include/config/auto.conf) */
248 		if (def != S_DEF_AUTO) {
249 			if (*p++ != '"')
250 				break;
251 			for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
252 				if (*p2 == '"') {
253 					*p2 = 0;
254 					break;
255 				}
256 				memmove(p2, p2 + 1, strlen(p2));
257 			}
258 			if (!p2) {
259 				conf_warning("invalid string found");
260 				return 1;
261 			}
262 		}
263 		/* fall through */
264 	case S_INT:
265 	case S_HEX:
266 		if (sym_string_valid(sym, p)) {
267 			sym->def[def].val = xstrdup(p);
268 			sym->flags |= def_flags;
269 		} else {
270 			if (def != S_DEF_AUTO)
271 				conf_warning("symbol value '%s' invalid for %s",
272 					     p, sym->name);
273 			return 1;
274 		}
275 		break;
276 	default:
277 		;
278 	}
279 	return 0;
280 }
281 
282 #define LINE_GROWTH 16
283 static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
284 {
285 	char *nline;
286 	size_t new_size = slen + 1;
287 	if (new_size > *n) {
288 		new_size += LINE_GROWTH - 1;
289 		new_size *= 2;
290 		nline = xrealloc(*lineptr, new_size);
291 		if (!nline)
292 			return -1;
293 
294 		*lineptr = nline;
295 		*n = new_size;
296 	}
297 
298 	(*lineptr)[slen] = c;
299 
300 	return 0;
301 }
302 
303 static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
304 {
305 	char *line = *lineptr;
306 	size_t slen = 0;
307 
308 	for (;;) {
309 		int c = getc(stream);
310 
311 		switch (c) {
312 		case '\n':
313 			if (add_byte(c, &line, slen, n) < 0)
314 				goto e_out;
315 			slen++;
316 			/* fall through */
317 		case EOF:
318 			if (add_byte('\0', &line, slen, n) < 0)
319 				goto e_out;
320 			*lineptr = line;
321 			if (slen == 0)
322 				return -1;
323 			return slen;
324 		default:
325 			if (add_byte(c, &line, slen, n) < 0)
326 				goto e_out;
327 			slen++;
328 		}
329 	}
330 
331 e_out:
332 	line[slen-1] = '\0';
333 	*lineptr = line;
334 	return -1;
335 }
336 
337 int conf_read_simple(const char *name, int def)
338 {
339 	FILE *in = NULL;
340 	char   *line = NULL;
341 	size_t  line_asize = 0;
342 	char *p, *p2;
343 	struct symbol *sym;
344 	int i, def_flags;
345 
346 	if (name) {
347 		in = zconf_fopen(name);
348 	} else {
349 		char *env;
350 
351 		name = conf_get_configname();
352 		in = zconf_fopen(name);
353 		if (in)
354 			goto load;
355 		conf_set_changed(true);
356 
357 		env = getenv("KCONFIG_DEFCONFIG_LIST");
358 		if (!env)
359 			return 1;
360 
361 		while (1) {
362 			bool is_last;
363 
364 			while (isspace(*env))
365 				env++;
366 
367 			if (!*env)
368 				break;
369 
370 			p = env;
371 			while (*p && !isspace(*p))
372 				p++;
373 
374 			is_last = (*p == '\0');
375 
376 			*p = '\0';
377 
378 			in = zconf_fopen(env);
379 			if (in) {
380 				conf_message("using defaults found in %s",
381 					     env);
382 				goto load;
383 			}
384 
385 			if (is_last)
386 				break;
387 
388 			env = p + 1;
389 		}
390 	}
391 	if (!in)
392 		return 1;
393 
394 load:
395 	conf_filename = name;
396 	conf_lineno = 0;
397 	conf_warnings = 0;
398 
399 	def_flags = SYMBOL_DEF << def;
400 	for_all_symbols(i, sym) {
401 		sym->flags |= SYMBOL_CHANGED;
402 		sym->flags &= ~(def_flags|SYMBOL_VALID);
403 		if (sym_is_choice(sym))
404 			sym->flags |= def_flags;
405 		switch (sym->type) {
406 		case S_INT:
407 		case S_HEX:
408 		case S_STRING:
409 			if (sym->def[def].val)
410 				free(sym->def[def].val);
411 			/* fall through */
412 		default:
413 			sym->def[def].val = NULL;
414 			sym->def[def].tri = no;
415 		}
416 	}
417 
418 	while (compat_getline(&line, &line_asize, in) != -1) {
419 		conf_lineno++;
420 		sym = NULL;
421 		if (line[0] == '#') {
422 			if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
423 				continue;
424 			p = strchr(line + 2 + strlen(CONFIG_), ' ');
425 			if (!p)
426 				continue;
427 			*p++ = 0;
428 			if (strncmp(p, "is not set", 10))
429 				continue;
430 			if (def == S_DEF_USER) {
431 				sym = sym_find(line + 2 + strlen(CONFIG_));
432 				if (!sym) {
433 					conf_set_changed(true);
434 					continue;
435 				}
436 			} else {
437 				sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
438 				if (sym->type == S_UNKNOWN)
439 					sym->type = S_BOOLEAN;
440 			}
441 			if (sym->flags & def_flags) {
442 				conf_warning("override: reassigning to symbol %s", sym->name);
443 			}
444 			switch (sym->type) {
445 			case S_BOOLEAN:
446 			case S_TRISTATE:
447 				sym->def[def].tri = no;
448 				sym->flags |= def_flags;
449 				break;
450 			default:
451 				;
452 			}
453 		} else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
454 			p = strchr(line + strlen(CONFIG_), '=');
455 			if (!p)
456 				continue;
457 			*p++ = 0;
458 			p2 = strchr(p, '\n');
459 			if (p2) {
460 				*p2-- = 0;
461 				if (*p2 == '\r')
462 					*p2 = 0;
463 			}
464 
465 			sym = sym_find(line + strlen(CONFIG_));
466 			if (!sym) {
467 				if (def == S_DEF_AUTO)
468 					/*
469 					 * Reading from include/config/auto.conf
470 					 * If CONFIG_FOO previously existed in
471 					 * auto.conf but it is missing now,
472 					 * include/config/FOO must be touched.
473 					 */
474 					conf_touch_dep(line + strlen(CONFIG_));
475 				else
476 					conf_set_changed(true);
477 				continue;
478 			}
479 
480 			if (sym->flags & def_flags) {
481 				conf_warning("override: reassigning to symbol %s", sym->name);
482 			}
483 			if (conf_set_sym_val(sym, def, def_flags, p))
484 				continue;
485 		} else {
486 			if (line[0] != '\r' && line[0] != '\n')
487 				conf_warning("unexpected data: %.*s",
488 					     (int)strcspn(line, "\r\n"), line);
489 
490 			continue;
491 		}
492 
493 		if (sym && sym_is_choice_value(sym)) {
494 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
495 			switch (sym->def[def].tri) {
496 			case no:
497 				break;
498 			case mod:
499 				if (cs->def[def].tri == yes) {
500 					conf_warning("%s creates inconsistent choice state", sym->name);
501 					cs->flags &= ~def_flags;
502 				}
503 				break;
504 			case yes:
505 				if (cs->def[def].tri != no)
506 					conf_warning("override: %s changes choice state", sym->name);
507 				cs->def[def].val = sym;
508 				break;
509 			}
510 			cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
511 		}
512 	}
513 	free(line);
514 	fclose(in);
515 	return 0;
516 }
517 
518 int conf_read(const char *name)
519 {
520 	struct symbol *sym;
521 	int conf_unsaved = 0;
522 	int i;
523 
524 	conf_set_changed(false);
525 
526 	if (conf_read_simple(name, S_DEF_USER)) {
527 		sym_calc_value(modules_sym);
528 		return 1;
529 	}
530 
531 	sym_calc_value(modules_sym);
532 
533 	for_all_symbols(i, sym) {
534 		sym_calc_value(sym);
535 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
536 			continue;
537 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
538 			/* check that calculated value agrees with saved value */
539 			switch (sym->type) {
540 			case S_BOOLEAN:
541 			case S_TRISTATE:
542 				if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
543 					continue;
544 				break;
545 			default:
546 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
547 					continue;
548 				break;
549 			}
550 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
551 			/* no previous value and not saved */
552 			continue;
553 		conf_unsaved++;
554 		/* maybe print value in verbose mode... */
555 	}
556 
557 	for_all_symbols(i, sym) {
558 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
559 			/* Reset values of generates values, so they'll appear
560 			 * as new, if they should become visible, but that
561 			 * doesn't quite work if the Kconfig and the saved
562 			 * configuration disagree.
563 			 */
564 			if (sym->visible == no && !conf_unsaved)
565 				sym->flags &= ~SYMBOL_DEF_USER;
566 			switch (sym->type) {
567 			case S_STRING:
568 			case S_INT:
569 			case S_HEX:
570 				/* Reset a string value if it's out of range */
571 				if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
572 					break;
573 				sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
574 				conf_unsaved++;
575 				break;
576 			default:
577 				break;
578 			}
579 		}
580 	}
581 
582 	if (conf_warnings || conf_unsaved)
583 		conf_set_changed(true);
584 
585 	return 0;
586 }
587 
588 struct comment_style {
589 	const char *decoration;
590 	const char *prefix;
591 	const char *postfix;
592 };
593 
594 static const struct comment_style comment_style_pound = {
595 	.decoration = "#",
596 	.prefix = "#",
597 	.postfix = "#",
598 };
599 
600 static const struct comment_style comment_style_c = {
601 	.decoration = " *",
602 	.prefix = "/*",
603 	.postfix = " */",
604 };
605 
606 static void conf_write_heading(FILE *fp, const struct comment_style *cs)
607 {
608 	fprintf(fp, "%s\n", cs->prefix);
609 
610 	fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n",
611 		cs->decoration);
612 
613 	fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text);
614 
615 	fprintf(fp, "%s\n", cs->postfix);
616 }
617 
618 /* The returned pointer must be freed on the caller side */
619 static char *escape_string_value(const char *in)
620 {
621 	const char *p;
622 	char *out;
623 	size_t len;
624 
625 	len = strlen(in) + strlen("\"\"") + 1;
626 
627 	p = in;
628 	while (1) {
629 		p += strcspn(p, "\"\\");
630 
631 		if (p[0] == '\0')
632 			break;
633 
634 		len++;
635 		p++;
636 	}
637 
638 	out = xmalloc(len);
639 	out[0] = '\0';
640 
641 	strcat(out, "\"");
642 
643 	p = in;
644 	while (1) {
645 		len = strcspn(p, "\"\\");
646 		strncat(out, p, len);
647 		p += len;
648 
649 		if (p[0] == '\0')
650 			break;
651 
652 		strcat(out, "\\");
653 		strncat(out, p++, 1);
654 	}
655 
656 	strcat(out, "\"");
657 
658 	return out;
659 }
660 
661 /*
662  * Kconfig configuration printer
663  *
664  * This printer is used when generating the resulting configuration after
665  * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
666  * passing a non-NULL argument to the printer.
667  */
668 enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
669 
670 static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
671 			   bool escape_string)
672 {
673 	const char *val;
674 	char *escaped = NULL;
675 
676 	if (sym->type == S_UNKNOWN)
677 		return;
678 
679 	val = sym_get_string_value(sym);
680 
681 	if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
682 	    output_n != OUTPUT_N && *val == 'n') {
683 		if (output_n == OUTPUT_N_AS_UNSET)
684 			fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
685 		return;
686 	}
687 
688 	if (sym->type == S_STRING && escape_string) {
689 		escaped = escape_string_value(val);
690 		val = escaped;
691 	}
692 
693 	fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
694 
695 	free(escaped);
696 }
697 
698 static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
699 {
700 	__print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
701 }
702 
703 static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
704 {
705 	__print_symbol(fp, sym, OUTPUT_N_NONE, false);
706 }
707 
708 void print_symbol_for_listconfig(struct symbol *sym)
709 {
710 	__print_symbol(stdout, sym, OUTPUT_N, true);
711 }
712 
713 static void print_symbol_for_c(FILE *fp, struct symbol *sym)
714 {
715 	const char *val;
716 	const char *sym_suffix = "";
717 	const char *val_prefix = "";
718 	char *escaped = NULL;
719 
720 	if (sym->type == S_UNKNOWN)
721 		return;
722 
723 	val = sym_get_string_value(sym);
724 
725 	switch (sym->type) {
726 	case S_BOOLEAN:
727 	case S_TRISTATE:
728 		switch (*val) {
729 		case 'n':
730 			return;
731 		case 'm':
732 			sym_suffix = "_MODULE";
733 			/* fall through */
734 		default:
735 			val = "1";
736 		}
737 		break;
738 	case S_HEX:
739 		if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
740 			val_prefix = "0x";
741 		break;
742 	case S_STRING:
743 		escaped = escape_string_value(val);
744 		val = escaped;
745 	default:
746 		break;
747 	}
748 
749 	fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
750 		val_prefix, val);
751 
752 	free(escaped);
753 }
754 
755 /*
756  * Write out a minimal config.
757  * All values that has default values are skipped as this is redundant.
758  */
759 int conf_write_defconfig(const char *filename)
760 {
761 	struct symbol *sym;
762 	struct menu *menu;
763 	FILE *out;
764 
765 	out = fopen(filename, "w");
766 	if (!out)
767 		return 1;
768 
769 	sym_clear_all_valid();
770 
771 	/* Traverse all menus to find all relevant symbols */
772 	menu = rootmenu.list;
773 
774 	while (menu != NULL)
775 	{
776 		sym = menu->sym;
777 		if (sym == NULL) {
778 			if (!menu_is_visible(menu))
779 				goto next_menu;
780 		} else if (!sym_is_choice(sym)) {
781 			sym_calc_value(sym);
782 			if (!(sym->flags & SYMBOL_WRITE))
783 				goto next_menu;
784 			sym->flags &= ~SYMBOL_WRITE;
785 			/* If we cannot change the symbol - skip */
786 			if (!sym_is_changeable(sym))
787 				goto next_menu;
788 			/* If symbol equals to default value - skip */
789 			if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
790 				goto next_menu;
791 
792 			/*
793 			 * If symbol is a choice value and equals to the
794 			 * default for a choice - skip.
795 			 * But only if value is bool and equal to "y" and
796 			 * choice is not "optional".
797 			 * (If choice is "optional" then all values can be "n")
798 			 */
799 			if (sym_is_choice_value(sym)) {
800 				struct symbol *cs;
801 				struct symbol *ds;
802 
803 				cs = prop_get_symbol(sym_get_choice_prop(sym));
804 				ds = sym_choice_default(cs);
805 				if (!sym_is_optional(cs) && sym == ds) {
806 					if ((sym->type == S_BOOLEAN) &&
807 					    sym_get_tristate_value(sym) == yes)
808 						goto next_menu;
809 				}
810 			}
811 			print_symbol_for_dotconfig(out, sym);
812 		}
813 next_menu:
814 		if (menu->list != NULL) {
815 			menu = menu->list;
816 		}
817 		else if (menu->next != NULL) {
818 			menu = menu->next;
819 		} else {
820 			while ((menu = menu->parent)) {
821 				if (menu->next != NULL) {
822 					menu = menu->next;
823 					break;
824 				}
825 			}
826 		}
827 	}
828 	fclose(out);
829 	return 0;
830 }
831 
832 int conf_write(const char *name)
833 {
834 	FILE *out;
835 	struct symbol *sym;
836 	struct menu *menu;
837 	const char *str;
838 	char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
839 	char *env;
840 	int i;
841 	bool need_newline = false;
842 
843 	if (!name)
844 		name = conf_get_configname();
845 
846 	if (!*name) {
847 		fprintf(stderr, "config name is empty\n");
848 		return -1;
849 	}
850 
851 	if (is_dir(name)) {
852 		fprintf(stderr, "%s: Is a directory\n", name);
853 		return -1;
854 	}
855 
856 	if (make_parent_dir(name))
857 		return -1;
858 
859 	env = getenv("KCONFIG_OVERWRITECONFIG");
860 	if (env && *env) {
861 		*tmpname = 0;
862 		out = fopen(name, "w");
863 	} else {
864 		snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
865 			 name, (int)getpid());
866 		out = fopen(tmpname, "w");
867 	}
868 	if (!out)
869 		return 1;
870 
871 	conf_write_heading(out, &comment_style_pound);
872 
873 	if (!conf_get_changed())
874 		sym_clear_all_valid();
875 
876 	menu = rootmenu.list;
877 	while (menu) {
878 		sym = menu->sym;
879 		if (!sym) {
880 			if (!menu_is_visible(menu))
881 				goto next;
882 			str = menu_get_prompt(menu);
883 			fprintf(out, "\n"
884 				     "#\n"
885 				     "# %s\n"
886 				     "#\n", str);
887 			need_newline = false;
888 		} else if (!(sym->flags & SYMBOL_CHOICE) &&
889 			   !(sym->flags & SYMBOL_WRITTEN)) {
890 			sym_calc_value(sym);
891 			if (!(sym->flags & SYMBOL_WRITE))
892 				goto next;
893 			if (need_newline) {
894 				fprintf(out, "\n");
895 				need_newline = false;
896 			}
897 			sym->flags |= SYMBOL_WRITTEN;
898 			print_symbol_for_dotconfig(out, sym);
899 		}
900 
901 next:
902 		if (menu->list) {
903 			menu = menu->list;
904 			continue;
905 		}
906 		if (menu->next)
907 			menu = menu->next;
908 		else while ((menu = menu->parent)) {
909 			if (!menu->sym && menu_is_visible(menu) &&
910 			    menu != &rootmenu) {
911 				str = menu_get_prompt(menu);
912 				fprintf(out, "# end of %s\n", str);
913 				need_newline = true;
914 			}
915 			if (menu->next) {
916 				menu = menu->next;
917 				break;
918 			}
919 		}
920 	}
921 	fclose(out);
922 
923 	for_all_symbols(i, sym)
924 		sym->flags &= ~SYMBOL_WRITTEN;
925 
926 	if (*tmpname) {
927 		if (is_same(name, tmpname)) {
928 			conf_message("No change to %s", name);
929 			unlink(tmpname);
930 			conf_set_changed(false);
931 			return 0;
932 		}
933 
934 		snprintf(oldname, sizeof(oldname), "%s.old", name);
935 		rename(name, oldname);
936 		if (rename(tmpname, name))
937 			return 1;
938 	}
939 
940 	conf_message("configuration written to %s", name);
941 
942 	conf_set_changed(false);
943 
944 	return 0;
945 }
946 
947 /* write a dependency file as used by kbuild to track dependencies */
948 static int conf_write_autoconf_cmd(const char *autoconf_name)
949 {
950 	char name[PATH_MAX], tmp[PATH_MAX];
951 	struct file *file;
952 	FILE *out;
953 	int ret;
954 
955 	ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
956 	if (ret >= sizeof(name)) /* check truncation */
957 		return -1;
958 
959 	if (make_parent_dir(name))
960 		return -1;
961 
962 	ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
963 	if (ret >= sizeof(tmp)) /* check truncation */
964 		return -1;
965 
966 	out = fopen(tmp, "w");
967 	if (!out) {
968 		perror("fopen");
969 		return -1;
970 	}
971 
972 	fprintf(out, "deps_config := \\\n");
973 	for (file = file_list; file; file = file->next)
974 		fprintf(out, "\t%s \\\n", file->name);
975 
976 	fprintf(out, "\n%s: $(deps_config)\n\n", autoconf_name);
977 
978 	env_write_dep(out, autoconf_name);
979 
980 	fprintf(out, "\n$(deps_config): ;\n");
981 
982 	ret = ferror(out); /* error check for all fprintf() calls */
983 	fclose(out);
984 	if (ret)
985 		return -1;
986 
987 	if (rename(tmp, name)) {
988 		perror("rename");
989 		return -1;
990 	}
991 
992 	return 0;
993 }
994 
995 static int conf_touch_deps(void)
996 {
997 	const char *name, *tmp;
998 	struct symbol *sym;
999 	int res, i;
1000 
1001 	name = conf_get_autoconfig_name();
1002 	tmp = strrchr(name, '/');
1003 	depfile_prefix_len = tmp ? tmp - name + 1 : 0;
1004 	if (depfile_prefix_len + 1 > sizeof(depfile_path))
1005 		return -1;
1006 
1007 	strncpy(depfile_path, name, depfile_prefix_len);
1008 	depfile_path[depfile_prefix_len] = 0;
1009 
1010 	conf_read_simple(name, S_DEF_AUTO);
1011 	sym_calc_value(modules_sym);
1012 
1013 	for_all_symbols(i, sym) {
1014 		sym_calc_value(sym);
1015 		if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
1016 			continue;
1017 		if (sym->flags & SYMBOL_WRITE) {
1018 			if (sym->flags & SYMBOL_DEF_AUTO) {
1019 				/*
1020 				 * symbol has old and new value,
1021 				 * so compare them...
1022 				 */
1023 				switch (sym->type) {
1024 				case S_BOOLEAN:
1025 				case S_TRISTATE:
1026 					if (sym_get_tristate_value(sym) ==
1027 					    sym->def[S_DEF_AUTO].tri)
1028 						continue;
1029 					break;
1030 				case S_STRING:
1031 				case S_HEX:
1032 				case S_INT:
1033 					if (!strcmp(sym_get_string_value(sym),
1034 						    sym->def[S_DEF_AUTO].val))
1035 						continue;
1036 					break;
1037 				default:
1038 					break;
1039 				}
1040 			} else {
1041 				/*
1042 				 * If there is no old value, only 'no' (unset)
1043 				 * is allowed as new value.
1044 				 */
1045 				switch (sym->type) {
1046 				case S_BOOLEAN:
1047 				case S_TRISTATE:
1048 					if (sym_get_tristate_value(sym) == no)
1049 						continue;
1050 					break;
1051 				default:
1052 					break;
1053 				}
1054 			}
1055 		} else if (!(sym->flags & SYMBOL_DEF_AUTO))
1056 			/* There is neither an old nor a new value. */
1057 			continue;
1058 		/* else
1059 		 *	There is an old value, but no new value ('no' (unset)
1060 		 *	isn't saved in auto.conf, so the old value is always
1061 		 *	different from 'no').
1062 		 */
1063 
1064 		res = conf_touch_dep(sym->name);
1065 		if (res)
1066 			return res;
1067 	}
1068 
1069 	return 0;
1070 }
1071 
1072 static int __conf_write_autoconf(const char *filename,
1073 				 void (*print_symbol)(FILE *, struct symbol *),
1074 				 const struct comment_style *comment_style)
1075 {
1076 	char tmp[PATH_MAX];
1077 	FILE *file;
1078 	struct symbol *sym;
1079 	int ret, i;
1080 
1081 	if (make_parent_dir(filename))
1082 		return -1;
1083 
1084 	ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
1085 	if (ret >= sizeof(tmp)) /* check truncation */
1086 		return -1;
1087 
1088 	file = fopen(tmp, "w");
1089 	if (!file) {
1090 		perror("fopen");
1091 		return -1;
1092 	}
1093 
1094 	conf_write_heading(file, comment_style);
1095 
1096 	for_all_symbols(i, sym)
1097 		if ((sym->flags & SYMBOL_WRITE) && sym->name)
1098 			print_symbol(file, sym);
1099 
1100 	/* check possible errors in conf_write_heading() and print_symbol() */
1101 	ret = ferror(file);
1102 	fclose(file);
1103 	if (ret)
1104 		return -1;
1105 
1106 	if (rename(tmp, filename)) {
1107 		perror("rename");
1108 		return -1;
1109 	}
1110 
1111 	return 0;
1112 }
1113 
1114 int conf_write_autoconf(int overwrite)
1115 {
1116 	struct symbol *sym;
1117 	const char *autoconf_name = conf_get_autoconfig_name();
1118 	int ret, i;
1119 
1120 	if (!overwrite && is_present(autoconf_name))
1121 		return 0;
1122 
1123 	ret = conf_write_autoconf_cmd(autoconf_name);
1124 	if (ret)
1125 		return -1;
1126 
1127 	if (conf_touch_deps())
1128 		return 1;
1129 
1130 	for_all_symbols(i, sym)
1131 		sym_calc_value(sym);
1132 
1133 	ret = __conf_write_autoconf(conf_get_autoheader_name(),
1134 				    print_symbol_for_c,
1135 				    &comment_style_c);
1136 	if (ret)
1137 		return ret;
1138 
1139 	/*
1140 	 * Create include/config/auto.conf. This must be the last step because
1141 	 * Kbuild has a dependency on auto.conf and this marks the successful
1142 	 * completion of the previous steps.
1143 	 */
1144 	ret = __conf_write_autoconf(conf_get_autoconfig_name(),
1145 				    print_symbol_for_autoconf,
1146 				    &comment_style_pound);
1147 	if (ret)
1148 		return ret;
1149 
1150 	return 0;
1151 }
1152 
1153 static bool conf_changed;
1154 static void (*conf_changed_callback)(void);
1155 
1156 void conf_set_changed(bool val)
1157 {
1158 	if (conf_changed_callback && conf_changed != val)
1159 		conf_changed_callback();
1160 
1161 	conf_changed = val;
1162 }
1163 
1164 bool conf_get_changed(void)
1165 {
1166 	return conf_changed;
1167 }
1168 
1169 void conf_set_changed_callback(void (*fn)(void))
1170 {
1171 	conf_changed_callback = fn;
1172 }
1173 
1174 void set_all_choice_values(struct symbol *csym)
1175 {
1176 	struct property *prop;
1177 	struct symbol *sym;
1178 	struct expr *e;
1179 
1180 	prop = sym_get_choice_prop(csym);
1181 
1182 	/*
1183 	 * Set all non-assinged choice values to no
1184 	 */
1185 	expr_list_for_each_sym(prop->expr, e, sym) {
1186 		if (!sym_has_value(sym))
1187 			sym->def[S_DEF_USER].tri = no;
1188 	}
1189 	csym->flags |= SYMBOL_DEF_USER;
1190 	/* clear VALID to get value calculated */
1191 	csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1192 }
1193