xref: /openbmc/linux/scripts/kconfig/confdata.c (revision ee8ec048)
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 		if (*p++ != '"')
248 			break;
249 		for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
250 			if (*p2 == '"') {
251 				*p2 = 0;
252 				break;
253 			}
254 			memmove(p2, p2 + 1, strlen(p2));
255 		}
256 		if (!p2) {
257 			if (def != S_DEF_AUTO)
258 				conf_warning("invalid string found");
259 			return 1;
260 		}
261 		/* fall through */
262 	case S_INT:
263 	case S_HEX:
264 		if (sym_string_valid(sym, p)) {
265 			sym->def[def].val = xstrdup(p);
266 			sym->flags |= def_flags;
267 		} else {
268 			if (def != S_DEF_AUTO)
269 				conf_warning("symbol value '%s' invalid for %s",
270 					     p, sym->name);
271 			return 1;
272 		}
273 		break;
274 	default:
275 		;
276 	}
277 	return 0;
278 }
279 
280 #define LINE_GROWTH 16
281 static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
282 {
283 	char *nline;
284 	size_t new_size = slen + 1;
285 	if (new_size > *n) {
286 		new_size += LINE_GROWTH - 1;
287 		new_size *= 2;
288 		nline = xrealloc(*lineptr, new_size);
289 		if (!nline)
290 			return -1;
291 
292 		*lineptr = nline;
293 		*n = new_size;
294 	}
295 
296 	(*lineptr)[slen] = c;
297 
298 	return 0;
299 }
300 
301 static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
302 {
303 	char *line = *lineptr;
304 	size_t slen = 0;
305 
306 	for (;;) {
307 		int c = getc(stream);
308 
309 		switch (c) {
310 		case '\n':
311 			if (add_byte(c, &line, slen, n) < 0)
312 				goto e_out;
313 			slen++;
314 			/* fall through */
315 		case EOF:
316 			if (add_byte('\0', &line, slen, n) < 0)
317 				goto e_out;
318 			*lineptr = line;
319 			if (slen == 0)
320 				return -1;
321 			return slen;
322 		default:
323 			if (add_byte(c, &line, slen, n) < 0)
324 				goto e_out;
325 			slen++;
326 		}
327 	}
328 
329 e_out:
330 	line[slen-1] = '\0';
331 	*lineptr = line;
332 	return -1;
333 }
334 
335 int conf_read_simple(const char *name, int def)
336 {
337 	FILE *in = NULL;
338 	char   *line = NULL;
339 	size_t  line_asize = 0;
340 	char *p, *p2;
341 	struct symbol *sym;
342 	int i, def_flags;
343 
344 	if (name) {
345 		in = zconf_fopen(name);
346 	} else {
347 		char *env;
348 
349 		name = conf_get_configname();
350 		in = zconf_fopen(name);
351 		if (in)
352 			goto load;
353 		conf_set_changed(true);
354 
355 		env = getenv("KCONFIG_DEFCONFIG_LIST");
356 		if (!env)
357 			return 1;
358 
359 		while (1) {
360 			bool is_last;
361 
362 			while (isspace(*env))
363 				env++;
364 
365 			if (!*env)
366 				break;
367 
368 			p = env;
369 			while (*p && !isspace(*p))
370 				p++;
371 
372 			is_last = (*p == '\0');
373 
374 			*p = '\0';
375 
376 			in = zconf_fopen(env);
377 			if (in) {
378 				conf_message("using defaults found in %s",
379 					     env);
380 				goto load;
381 			}
382 
383 			if (is_last)
384 				break;
385 
386 			env = p + 1;
387 		}
388 	}
389 	if (!in)
390 		return 1;
391 
392 load:
393 	conf_filename = name;
394 	conf_lineno = 0;
395 	conf_warnings = 0;
396 
397 	def_flags = SYMBOL_DEF << def;
398 	for_all_symbols(i, sym) {
399 		sym->flags |= SYMBOL_CHANGED;
400 		sym->flags &= ~(def_flags|SYMBOL_VALID);
401 		if (sym_is_choice(sym))
402 			sym->flags |= def_flags;
403 		switch (sym->type) {
404 		case S_INT:
405 		case S_HEX:
406 		case S_STRING:
407 			if (sym->def[def].val)
408 				free(sym->def[def].val);
409 			/* fall through */
410 		default:
411 			sym->def[def].val = NULL;
412 			sym->def[def].tri = no;
413 		}
414 	}
415 
416 	while (compat_getline(&line, &line_asize, in) != -1) {
417 		conf_lineno++;
418 		sym = NULL;
419 		if (line[0] == '#') {
420 			if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
421 				continue;
422 			p = strchr(line + 2 + strlen(CONFIG_), ' ');
423 			if (!p)
424 				continue;
425 			*p++ = 0;
426 			if (strncmp(p, "is not set", 10))
427 				continue;
428 			if (def == S_DEF_USER) {
429 				sym = sym_find(line + 2 + strlen(CONFIG_));
430 				if (!sym) {
431 					conf_set_changed(true);
432 					continue;
433 				}
434 			} else {
435 				sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
436 				if (sym->type == S_UNKNOWN)
437 					sym->type = S_BOOLEAN;
438 			}
439 			if (sym->flags & def_flags) {
440 				conf_warning("override: reassigning to symbol %s", sym->name);
441 			}
442 			switch (sym->type) {
443 			case S_BOOLEAN:
444 			case S_TRISTATE:
445 				sym->def[def].tri = no;
446 				sym->flags |= def_flags;
447 				break;
448 			default:
449 				;
450 			}
451 		} else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
452 			p = strchr(line + strlen(CONFIG_), '=');
453 			if (!p)
454 				continue;
455 			*p++ = 0;
456 			p2 = strchr(p, '\n');
457 			if (p2) {
458 				*p2-- = 0;
459 				if (*p2 == '\r')
460 					*p2 = 0;
461 			}
462 
463 			sym = sym_find(line + strlen(CONFIG_));
464 			if (!sym) {
465 				if (def == S_DEF_AUTO)
466 					/*
467 					 * Reading from include/config/auto.conf
468 					 * If CONFIG_FOO previously existed in
469 					 * auto.conf but it is missing now,
470 					 * include/config/FOO must be touched.
471 					 */
472 					conf_touch_dep(line + strlen(CONFIG_));
473 				else
474 					conf_set_changed(true);
475 				continue;
476 			}
477 
478 			if (sym->flags & def_flags) {
479 				conf_warning("override: reassigning to symbol %s", sym->name);
480 			}
481 			if (conf_set_sym_val(sym, def, def_flags, p))
482 				continue;
483 		} else {
484 			if (line[0] != '\r' && line[0] != '\n')
485 				conf_warning("unexpected data: %.*s",
486 					     (int)strcspn(line, "\r\n"), line);
487 
488 			continue;
489 		}
490 
491 		if (sym && sym_is_choice_value(sym)) {
492 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
493 			switch (sym->def[def].tri) {
494 			case no:
495 				break;
496 			case mod:
497 				if (cs->def[def].tri == yes) {
498 					conf_warning("%s creates inconsistent choice state", sym->name);
499 					cs->flags &= ~def_flags;
500 				}
501 				break;
502 			case yes:
503 				if (cs->def[def].tri != no)
504 					conf_warning("override: %s changes choice state", sym->name);
505 				cs->def[def].val = sym;
506 				break;
507 			}
508 			cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
509 		}
510 	}
511 	free(line);
512 	fclose(in);
513 	return 0;
514 }
515 
516 int conf_read(const char *name)
517 {
518 	struct symbol *sym;
519 	int conf_unsaved = 0;
520 	int i;
521 
522 	conf_set_changed(false);
523 
524 	if (conf_read_simple(name, S_DEF_USER)) {
525 		sym_calc_value(modules_sym);
526 		return 1;
527 	}
528 
529 	sym_calc_value(modules_sym);
530 
531 	for_all_symbols(i, sym) {
532 		sym_calc_value(sym);
533 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
534 			continue;
535 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
536 			/* check that calculated value agrees with saved value */
537 			switch (sym->type) {
538 			case S_BOOLEAN:
539 			case S_TRISTATE:
540 				if (sym->def[S_DEF_USER].tri == sym_get_tristate_value(sym))
541 					continue;
542 				break;
543 			default:
544 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
545 					continue;
546 				break;
547 			}
548 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
549 			/* no previous value and not saved */
550 			continue;
551 		conf_unsaved++;
552 		/* maybe print value in verbose mode... */
553 	}
554 
555 	for_all_symbols(i, sym) {
556 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
557 			/* Reset values of generates values, so they'll appear
558 			 * as new, if they should become visible, but that
559 			 * doesn't quite work if the Kconfig and the saved
560 			 * configuration disagree.
561 			 */
562 			if (sym->visible == no && !conf_unsaved)
563 				sym->flags &= ~SYMBOL_DEF_USER;
564 			switch (sym->type) {
565 			case S_STRING:
566 			case S_INT:
567 			case S_HEX:
568 				/* Reset a string value if it's out of range */
569 				if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
570 					break;
571 				sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
572 				conf_unsaved++;
573 				break;
574 			default:
575 				break;
576 			}
577 		}
578 	}
579 
580 	if (conf_warnings || conf_unsaved)
581 		conf_set_changed(true);
582 
583 	return 0;
584 }
585 
586 struct comment_style {
587 	const char *decoration;
588 	const char *prefix;
589 	const char *postfix;
590 };
591 
592 static const struct comment_style comment_style_pound = {
593 	.decoration = "#",
594 	.prefix = "#",
595 	.postfix = "#",
596 };
597 
598 static const struct comment_style comment_style_c = {
599 	.decoration = " *",
600 	.prefix = "/*",
601 	.postfix = " */",
602 };
603 
604 static void conf_write_heading(FILE *fp, const struct comment_style *cs)
605 {
606 	fprintf(fp, "%s\n", cs->prefix);
607 
608 	fprintf(fp, "%s Automatically generated file; DO NOT EDIT.\n",
609 		cs->decoration);
610 
611 	fprintf(fp, "%s %s\n", cs->decoration, rootmenu.prompt->text);
612 
613 	fprintf(fp, "%s\n", cs->postfix);
614 }
615 
616 /* The returned pointer must be freed on the caller side */
617 static char *escape_string_value(const char *in)
618 {
619 	const char *p;
620 	char *out;
621 	size_t len;
622 
623 	len = strlen(in) + strlen("\"\"") + 1;
624 
625 	p = in;
626 	while (1) {
627 		p += strcspn(p, "\"\\");
628 
629 		if (p[0] == '\0')
630 			break;
631 
632 		len++;
633 		p++;
634 	}
635 
636 	out = xmalloc(len);
637 	out[0] = '\0';
638 
639 	strcat(out, "\"");
640 
641 	p = in;
642 	while (1) {
643 		len = strcspn(p, "\"\\");
644 		strncat(out, p, len);
645 		p += len;
646 
647 		if (p[0] == '\0')
648 			break;
649 
650 		strcat(out, "\\");
651 		strncat(out, p++, 1);
652 	}
653 
654 	strcat(out, "\"");
655 
656 	return out;
657 }
658 
659 /*
660  * Kconfig configuration printer
661  *
662  * This printer is used when generating the resulting configuration after
663  * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
664  * passing a non-NULL argument to the printer.
665  */
666 enum output_n { OUTPUT_N, OUTPUT_N_AS_UNSET, OUTPUT_N_NONE };
667 
668 static void __print_symbol(FILE *fp, struct symbol *sym, enum output_n output_n,
669 			   bool escape_string)
670 {
671 	const char *val;
672 	char *escaped = NULL;
673 
674 	if (sym->type == S_UNKNOWN)
675 		return;
676 
677 	val = sym_get_string_value(sym);
678 
679 	if ((sym->type == S_BOOLEAN || sym->type == S_TRISTATE) &&
680 	    output_n != OUTPUT_N && *val == 'n') {
681 		if (output_n == OUTPUT_N_AS_UNSET)
682 			fprintf(fp, "# %s%s is not set\n", CONFIG_, sym->name);
683 		return;
684 	}
685 
686 	if (sym->type == S_STRING && escape_string) {
687 		escaped = escape_string_value(val);
688 		val = escaped;
689 	}
690 
691 	fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, val);
692 
693 	free(escaped);
694 }
695 
696 static void print_symbol_for_dotconfig(FILE *fp, struct symbol *sym)
697 {
698 	__print_symbol(fp, sym, OUTPUT_N_AS_UNSET, true);
699 }
700 
701 static void print_symbol_for_autoconf(FILE *fp, struct symbol *sym)
702 {
703 	__print_symbol(fp, sym, OUTPUT_N_NONE, true);
704 }
705 
706 void print_symbol_for_listconfig(struct symbol *sym)
707 {
708 	__print_symbol(stdout, sym, OUTPUT_N, true);
709 }
710 
711 static void print_symbol_for_c(FILE *fp, struct symbol *sym)
712 {
713 	const char *val;
714 	const char *sym_suffix = "";
715 	const char *val_prefix = "";
716 	char *escaped = NULL;
717 
718 	if (sym->type == S_UNKNOWN)
719 		return;
720 
721 	val = sym_get_string_value(sym);
722 
723 	switch (sym->type) {
724 	case S_BOOLEAN:
725 	case S_TRISTATE:
726 		switch (*val) {
727 		case 'n':
728 			return;
729 		case 'm':
730 			sym_suffix = "_MODULE";
731 			/* fall through */
732 		default:
733 			val = "1";
734 		}
735 		break;
736 	case S_HEX:
737 		if (val[0] != '0' || (val[1] != 'x' && val[1] != 'X'))
738 			val_prefix = "0x";
739 		break;
740 	case S_STRING:
741 		escaped = escape_string_value(val);
742 		val = escaped;
743 	default:
744 		break;
745 	}
746 
747 	fprintf(fp, "#define %s%s%s %s%s\n", CONFIG_, sym->name, sym_suffix,
748 		val_prefix, val);
749 
750 	free(escaped);
751 }
752 
753 /*
754  * Write out a minimal config.
755  * All values that has default values are skipped as this is redundant.
756  */
757 int conf_write_defconfig(const char *filename)
758 {
759 	struct symbol *sym;
760 	struct menu *menu;
761 	FILE *out;
762 
763 	out = fopen(filename, "w");
764 	if (!out)
765 		return 1;
766 
767 	sym_clear_all_valid();
768 
769 	/* Traverse all menus to find all relevant symbols */
770 	menu = rootmenu.list;
771 
772 	while (menu != NULL)
773 	{
774 		sym = menu->sym;
775 		if (sym == NULL) {
776 			if (!menu_is_visible(menu))
777 				goto next_menu;
778 		} else if (!sym_is_choice(sym)) {
779 			sym_calc_value(sym);
780 			if (!(sym->flags & SYMBOL_WRITE))
781 				goto next_menu;
782 			sym->flags &= ~SYMBOL_WRITE;
783 			/* If we cannot change the symbol - skip */
784 			if (!sym_is_changeable(sym))
785 				goto next_menu;
786 			/* If symbol equals to default value - skip */
787 			if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
788 				goto next_menu;
789 
790 			/*
791 			 * If symbol is a choice value and equals to the
792 			 * default for a choice - skip.
793 			 * But only if value is bool and equal to "y" and
794 			 * choice is not "optional".
795 			 * (If choice is "optional" then all values can be "n")
796 			 */
797 			if (sym_is_choice_value(sym)) {
798 				struct symbol *cs;
799 				struct symbol *ds;
800 
801 				cs = prop_get_symbol(sym_get_choice_prop(sym));
802 				ds = sym_choice_default(cs);
803 				if (!sym_is_optional(cs) && sym == ds) {
804 					if ((sym->type == S_BOOLEAN) &&
805 					    sym_get_tristate_value(sym) == yes)
806 						goto next_menu;
807 				}
808 			}
809 			print_symbol_for_dotconfig(out, sym);
810 		}
811 next_menu:
812 		if (menu->list != NULL) {
813 			menu = menu->list;
814 		}
815 		else if (menu->next != NULL) {
816 			menu = menu->next;
817 		} else {
818 			while ((menu = menu->parent)) {
819 				if (menu->next != NULL) {
820 					menu = menu->next;
821 					break;
822 				}
823 			}
824 		}
825 	}
826 	fclose(out);
827 	return 0;
828 }
829 
830 int conf_write(const char *name)
831 {
832 	FILE *out;
833 	struct symbol *sym;
834 	struct menu *menu;
835 	const char *str;
836 	char tmpname[PATH_MAX + 1], oldname[PATH_MAX + 1];
837 	char *env;
838 	int i;
839 	bool need_newline = false;
840 
841 	if (!name)
842 		name = conf_get_configname();
843 
844 	if (!*name) {
845 		fprintf(stderr, "config name is empty\n");
846 		return -1;
847 	}
848 
849 	if (is_dir(name)) {
850 		fprintf(stderr, "%s: Is a directory\n", name);
851 		return -1;
852 	}
853 
854 	if (make_parent_dir(name))
855 		return -1;
856 
857 	env = getenv("KCONFIG_OVERWRITECONFIG");
858 	if (env && *env) {
859 		*tmpname = 0;
860 		out = fopen(name, "w");
861 	} else {
862 		snprintf(tmpname, sizeof(tmpname), "%s.%d.tmp",
863 			 name, (int)getpid());
864 		out = fopen(tmpname, "w");
865 	}
866 	if (!out)
867 		return 1;
868 
869 	conf_write_heading(out, &comment_style_pound);
870 
871 	if (!conf_get_changed())
872 		sym_clear_all_valid();
873 
874 	menu = rootmenu.list;
875 	while (menu) {
876 		sym = menu->sym;
877 		if (!sym) {
878 			if (!menu_is_visible(menu))
879 				goto next;
880 			str = menu_get_prompt(menu);
881 			fprintf(out, "\n"
882 				     "#\n"
883 				     "# %s\n"
884 				     "#\n", str);
885 			need_newline = false;
886 		} else if (!(sym->flags & SYMBOL_CHOICE) &&
887 			   !(sym->flags & SYMBOL_WRITTEN)) {
888 			sym_calc_value(sym);
889 			if (!(sym->flags & SYMBOL_WRITE))
890 				goto next;
891 			if (need_newline) {
892 				fprintf(out, "\n");
893 				need_newline = false;
894 			}
895 			sym->flags |= SYMBOL_WRITTEN;
896 			print_symbol_for_dotconfig(out, sym);
897 		}
898 
899 next:
900 		if (menu->list) {
901 			menu = menu->list;
902 			continue;
903 		}
904 		if (menu->next)
905 			menu = menu->next;
906 		else while ((menu = menu->parent)) {
907 			if (!menu->sym && menu_is_visible(menu) &&
908 			    menu != &rootmenu) {
909 				str = menu_get_prompt(menu);
910 				fprintf(out, "# end of %s\n", str);
911 				need_newline = true;
912 			}
913 			if (menu->next) {
914 				menu = menu->next;
915 				break;
916 			}
917 		}
918 	}
919 	fclose(out);
920 
921 	for_all_symbols(i, sym)
922 		sym->flags &= ~SYMBOL_WRITTEN;
923 
924 	if (*tmpname) {
925 		if (is_same(name, tmpname)) {
926 			conf_message("No change to %s", name);
927 			unlink(tmpname);
928 			conf_set_changed(false);
929 			return 0;
930 		}
931 
932 		snprintf(oldname, sizeof(oldname), "%s.old", name);
933 		rename(name, oldname);
934 		if (rename(tmpname, name))
935 			return 1;
936 	}
937 
938 	conf_message("configuration written to %s", name);
939 
940 	conf_set_changed(false);
941 
942 	return 0;
943 }
944 
945 /* write a dependency file as used by kbuild to track dependencies */
946 static int conf_write_autoconf_cmd(const char *autoconf_name)
947 {
948 	char name[PATH_MAX], tmp[PATH_MAX];
949 	struct file *file;
950 	FILE *out;
951 	int ret;
952 
953 	ret = snprintf(name, sizeof(name), "%s.cmd", autoconf_name);
954 	if (ret >= sizeof(name)) /* check truncation */
955 		return -1;
956 
957 	if (make_parent_dir(name))
958 		return -1;
959 
960 	ret = snprintf(tmp, sizeof(tmp), "%s.cmd.tmp", autoconf_name);
961 	if (ret >= sizeof(tmp)) /* check truncation */
962 		return -1;
963 
964 	out = fopen(tmp, "w");
965 	if (!out) {
966 		perror("fopen");
967 		return -1;
968 	}
969 
970 	fprintf(out, "deps_config := \\\n");
971 	for (file = file_list; file; file = file->next)
972 		fprintf(out, "\t%s \\\n", file->name);
973 
974 	fprintf(out, "\n%s: $(deps_config)\n\n", autoconf_name);
975 
976 	env_write_dep(out, autoconf_name);
977 
978 	fprintf(out, "\n$(deps_config): ;\n");
979 
980 	if (ferror(out)) /* error check for all fprintf() calls */
981 		return -1;
982 
983 	fclose(out);
984 
985 	if (rename(tmp, name)) {
986 		perror("rename");
987 		return -1;
988 	}
989 
990 	return 0;
991 }
992 
993 static int conf_touch_deps(void)
994 {
995 	const char *name;
996 	struct symbol *sym;
997 	int res, i;
998 
999 	strcpy(depfile_path, "include/config/");
1000 	depfile_prefix_len = strlen(depfile_path);
1001 
1002 	name = conf_get_autoconfig_name();
1003 	conf_read_simple(name, S_DEF_AUTO);
1004 	sym_calc_value(modules_sym);
1005 
1006 	for_all_symbols(i, sym) {
1007 		sym_calc_value(sym);
1008 		if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
1009 			continue;
1010 		if (sym->flags & SYMBOL_WRITE) {
1011 			if (sym->flags & SYMBOL_DEF_AUTO) {
1012 				/*
1013 				 * symbol has old and new value,
1014 				 * so compare them...
1015 				 */
1016 				switch (sym->type) {
1017 				case S_BOOLEAN:
1018 				case S_TRISTATE:
1019 					if (sym_get_tristate_value(sym) ==
1020 					    sym->def[S_DEF_AUTO].tri)
1021 						continue;
1022 					break;
1023 				case S_STRING:
1024 				case S_HEX:
1025 				case S_INT:
1026 					if (!strcmp(sym_get_string_value(sym),
1027 						    sym->def[S_DEF_AUTO].val))
1028 						continue;
1029 					break;
1030 				default:
1031 					break;
1032 				}
1033 			} else {
1034 				/*
1035 				 * If there is no old value, only 'no' (unset)
1036 				 * is allowed as new value.
1037 				 */
1038 				switch (sym->type) {
1039 				case S_BOOLEAN:
1040 				case S_TRISTATE:
1041 					if (sym_get_tristate_value(sym) == no)
1042 						continue;
1043 					break;
1044 				default:
1045 					break;
1046 				}
1047 			}
1048 		} else if (!(sym->flags & SYMBOL_DEF_AUTO))
1049 			/* There is neither an old nor a new value. */
1050 			continue;
1051 		/* else
1052 		 *	There is an old value, but no new value ('no' (unset)
1053 		 *	isn't saved in auto.conf, so the old value is always
1054 		 *	different from 'no').
1055 		 */
1056 
1057 		res = conf_touch_dep(sym->name);
1058 		if (res)
1059 			return res;
1060 	}
1061 
1062 	return 0;
1063 }
1064 
1065 static int __conf_write_autoconf(const char *filename,
1066 				 void (*print_symbol)(FILE *, struct symbol *),
1067 				 const struct comment_style *comment_style)
1068 {
1069 	char tmp[PATH_MAX];
1070 	FILE *file;
1071 	struct symbol *sym;
1072 	int ret, i;
1073 
1074 	if (make_parent_dir(filename))
1075 		return -1;
1076 
1077 	ret = snprintf(tmp, sizeof(tmp), "%s.tmp", filename);
1078 	if (ret >= sizeof(tmp)) /* check truncation */
1079 		return -1;
1080 
1081 	file = fopen(tmp, "w");
1082 	if (!file) {
1083 		perror("fopen");
1084 		return -1;
1085 	}
1086 
1087 	conf_write_heading(file, comment_style);
1088 
1089 	for_all_symbols(i, sym)
1090 		if ((sym->flags & SYMBOL_WRITE) && sym->name)
1091 			print_symbol(file, sym);
1092 
1093 	/* check possible errors in conf_write_heading() and print_symbol() */
1094 	if (ferror(file))
1095 		return -1;
1096 
1097 	fclose(file);
1098 
1099 	if (rename(tmp, filename)) {
1100 		perror("rename");
1101 		return -1;
1102 	}
1103 
1104 	return 0;
1105 }
1106 
1107 int conf_write_autoconf(int overwrite)
1108 {
1109 	struct symbol *sym;
1110 	const char *autoconf_name = conf_get_autoconfig_name();
1111 	int ret, i;
1112 
1113 	if (!overwrite && is_present(autoconf_name))
1114 		return 0;
1115 
1116 	ret = conf_write_autoconf_cmd(autoconf_name);
1117 	if (ret)
1118 		return -1;
1119 
1120 	if (conf_touch_deps())
1121 		return 1;
1122 
1123 	for_all_symbols(i, sym)
1124 		sym_calc_value(sym);
1125 
1126 	ret = __conf_write_autoconf(conf_get_autoheader_name(),
1127 				    print_symbol_for_c,
1128 				    &comment_style_c);
1129 	if (ret)
1130 		return ret;
1131 
1132 	/*
1133 	 * Create include/config/auto.conf. This must be the last step because
1134 	 * Kbuild has a dependency on auto.conf and this marks the successful
1135 	 * completion of the previous steps.
1136 	 */
1137 	ret = __conf_write_autoconf(conf_get_autoconfig_name(),
1138 				    print_symbol_for_autoconf,
1139 				    &comment_style_pound);
1140 	if (ret)
1141 		return ret;
1142 
1143 	return 0;
1144 }
1145 
1146 static bool conf_changed;
1147 static void (*conf_changed_callback)(void);
1148 
1149 void conf_set_changed(bool val)
1150 {
1151 	if (conf_changed_callback && conf_changed != val)
1152 		conf_changed_callback();
1153 
1154 	conf_changed = val;
1155 }
1156 
1157 bool conf_get_changed(void)
1158 {
1159 	return conf_changed;
1160 }
1161 
1162 void conf_set_changed_callback(void (*fn)(void))
1163 {
1164 	conf_changed_callback = fn;
1165 }
1166 
1167 void set_all_choice_values(struct symbol *csym)
1168 {
1169 	struct property *prop;
1170 	struct symbol *sym;
1171 	struct expr *e;
1172 
1173 	prop = sym_get_choice_prop(csym);
1174 
1175 	/*
1176 	 * Set all non-assinged choice values to no
1177 	 */
1178 	expr_list_for_each_sym(prop->expr, e, sym) {
1179 		if (!sym_has_value(sym))
1180 			sym->def[S_DEF_USER].tri = no;
1181 	}
1182 	csym->flags |= SYMBOL_DEF_USER;
1183 	/* clear VALID to get value calculated */
1184 	csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1185 }
1186