xref: /linux/scripts/kconfig/confdata.c (revision 10a4b2772e7643247ddb5316c644f1fe7c4dccca)
1 /*
2  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3  * Released under the terms of the GNU GPL v2.0.
4  */
5 
6 #include <sys/stat.h>
7 #include <ctype.h>
8 #include <errno.h>
9 #include <fcntl.h>
10 #include <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15 #include <unistd.h>
16 
17 #define LKC_DIRECT_LINK
18 #include "lkc.h"
19 
20 static void conf_warning(const char *fmt, ...)
21 	__attribute__ ((format (printf, 1, 2)));
22 
23 static void conf_message(const char *fmt, ...)
24 	__attribute__ ((format (printf, 1, 2)));
25 
26 static const char *conf_filename;
27 static int conf_lineno, conf_warnings, conf_unsaved;
28 
29 const char conf_defname[] = "arch/$ARCH/defconfig";
30 
31 static void conf_warning(const char *fmt, ...)
32 {
33 	va_list ap;
34 	va_start(ap, fmt);
35 	fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
36 	vfprintf(stderr, fmt, ap);
37 	fprintf(stderr, "\n");
38 	va_end(ap);
39 	conf_warnings++;
40 }
41 
42 static void conf_default_message_callback(const char *fmt, va_list ap)
43 {
44 	printf("#\n# ");
45 	vprintf(fmt, ap);
46 	printf("\n#\n");
47 }
48 
49 static void (*conf_message_callback) (const char *fmt, va_list ap) =
50 	conf_default_message_callback;
51 void conf_set_message_callback(void (*fn) (const char *fmt, va_list ap))
52 {
53 	conf_message_callback = fn;
54 }
55 
56 static void conf_message(const char *fmt, ...)
57 {
58 	va_list ap;
59 
60 	va_start(ap, fmt);
61 	if (conf_message_callback)
62 		conf_message_callback(fmt, ap);
63 }
64 
65 const char *conf_get_configname(void)
66 {
67 	char *name = getenv("KCONFIG_CONFIG");
68 
69 	return name ? name : ".config";
70 }
71 
72 const char *conf_get_autoconfig_name(void)
73 {
74 	char *name = getenv("KCONFIG_AUTOCONFIG");
75 
76 	return name ? name : "include/config/auto.conf";
77 }
78 
79 static char *conf_expand_value(const char *in)
80 {
81 	struct symbol *sym;
82 	const char *src;
83 	static char res_value[SYMBOL_MAXLENGTH];
84 	char *dst, name[SYMBOL_MAXLENGTH];
85 
86 	res_value[0] = 0;
87 	dst = name;
88 	while ((src = strchr(in, '$'))) {
89 		strncat(res_value, in, src - in);
90 		src++;
91 		dst = name;
92 		while (isalnum(*src) || *src == '_')
93 			*dst++ = *src++;
94 		*dst = 0;
95 		sym = sym_lookup(name, 0);
96 		sym_calc_value(sym);
97 		strcat(res_value, sym_get_string_value(sym));
98 		in = src;
99 	}
100 	strcat(res_value, in);
101 
102 	return res_value;
103 }
104 
105 char *conf_get_default_confname(void)
106 {
107 	struct stat buf;
108 	static char fullname[PATH_MAX+1];
109 	char *env, *name;
110 
111 	name = conf_expand_value(conf_defname);
112 	env = getenv(SRCTREE);
113 	if (env) {
114 		sprintf(fullname, "%s/%s", env, name);
115 		if (!stat(fullname, &buf))
116 			return fullname;
117 	}
118 	return name;
119 }
120 
121 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
122 {
123 	char *p2;
124 
125 	switch (sym->type) {
126 	case S_TRISTATE:
127 		if (p[0] == 'm') {
128 			sym->def[def].tri = mod;
129 			sym->flags |= def_flags;
130 			break;
131 		}
132 		/* fall through */
133 	case S_BOOLEAN:
134 		if (p[0] == 'y') {
135 			sym->def[def].tri = yes;
136 			sym->flags |= def_flags;
137 			break;
138 		}
139 		if (p[0] == 'n') {
140 			sym->def[def].tri = no;
141 			sym->flags |= def_flags;
142 			break;
143 		}
144 		conf_warning("symbol value '%s' invalid for %s", p, sym->name);
145 		return 1;
146 	case S_OTHER:
147 		if (*p != '"') {
148 			for (p2 = p; *p2 && !isspace(*p2); p2++)
149 				;
150 			sym->type = S_STRING;
151 			goto done;
152 		}
153 		/* fall through */
154 	case S_STRING:
155 		if (*p++ != '"')
156 			break;
157 		for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
158 			if (*p2 == '"') {
159 				*p2 = 0;
160 				break;
161 			}
162 			memmove(p2, p2 + 1, strlen(p2));
163 		}
164 		if (!p2) {
165 			conf_warning("invalid string found");
166 			return 1;
167 		}
168 		/* fall through */
169 	case S_INT:
170 	case S_HEX:
171 	done:
172 		if (sym_string_valid(sym, p)) {
173 			sym->def[def].val = strdup(p);
174 			sym->flags |= def_flags;
175 		} else {
176 			conf_warning("symbol value '%s' invalid for %s", p, sym->name);
177 			return 1;
178 		}
179 		break;
180 	default:
181 		;
182 	}
183 	return 0;
184 }
185 
186 int conf_read_simple(const char *name, int def)
187 {
188 	FILE *in = NULL;
189 	char line[1024];
190 	char *p, *p2;
191 	struct symbol *sym;
192 	int i, def_flags;
193 
194 	if (name) {
195 		in = zconf_fopen(name);
196 	} else {
197 		struct property *prop;
198 
199 		name = conf_get_configname();
200 		in = zconf_fopen(name);
201 		if (in)
202 			goto load;
203 		sym_add_change_count(1);
204 		if (!sym_defconfig_list) {
205 			if (modules_sym)
206 				sym_calc_value(modules_sym);
207 			return 1;
208 		}
209 
210 		for_all_defaults(sym_defconfig_list, prop) {
211 			if (expr_calc_value(prop->visible.expr) == no ||
212 			    prop->expr->type != E_SYMBOL)
213 				continue;
214 			name = conf_expand_value(prop->expr->left.sym->name);
215 			in = zconf_fopen(name);
216 			if (in) {
217 				conf_message(_("using defaults found in %s"),
218 					 name);
219 				goto load;
220 			}
221 		}
222 	}
223 	if (!in)
224 		return 1;
225 
226 load:
227 	conf_filename = name;
228 	conf_lineno = 0;
229 	conf_warnings = 0;
230 	conf_unsaved = 0;
231 
232 	def_flags = SYMBOL_DEF << def;
233 	for_all_symbols(i, sym) {
234 		sym->flags |= SYMBOL_CHANGED;
235 		sym->flags &= ~(def_flags|SYMBOL_VALID);
236 		if (sym_is_choice(sym))
237 			sym->flags |= def_flags;
238 		switch (sym->type) {
239 		case S_INT:
240 		case S_HEX:
241 		case S_STRING:
242 			if (sym->def[def].val)
243 				free(sym->def[def].val);
244 			/* fall through */
245 		default:
246 			sym->def[def].val = NULL;
247 			sym->def[def].tri = no;
248 		}
249 	}
250 
251 	while (fgets(line, sizeof(line), in)) {
252 		conf_lineno++;
253 		sym = NULL;
254 		if (line[0] == '#') {
255 			if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
256 				continue;
257 			p = strchr(line + 2 + strlen(CONFIG_), ' ');
258 			if (!p)
259 				continue;
260 			*p++ = 0;
261 			if (strncmp(p, "is not set", 10))
262 				continue;
263 			if (def == S_DEF_USER) {
264 				sym = sym_find(line + 2 + strlen(CONFIG_));
265 				if (!sym) {
266 					sym_add_change_count(1);
267 					goto setsym;
268 				}
269 			} else {
270 				sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
271 				if (sym->type == S_UNKNOWN)
272 					sym->type = S_BOOLEAN;
273 			}
274 			if (sym->flags & def_flags) {
275 				conf_warning("override: reassigning to symbol %s", sym->name);
276 			}
277 			switch (sym->type) {
278 			case S_BOOLEAN:
279 			case S_TRISTATE:
280 				sym->def[def].tri = no;
281 				sym->flags |= def_flags;
282 				break;
283 			default:
284 				;
285 			}
286 		} else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
287 			p = strchr(line + strlen(CONFIG_), '=');
288 			if (!p)
289 				continue;
290 			*p++ = 0;
291 			p2 = strchr(p, '\n');
292 			if (p2) {
293 				*p2-- = 0;
294 				if (*p2 == '\r')
295 					*p2 = 0;
296 			}
297 			if (def == S_DEF_USER) {
298 				sym = sym_find(line + strlen(CONFIG_));
299 				if (!sym) {
300 					sym_add_change_count(1);
301 					goto setsym;
302 				}
303 			} else {
304 				sym = sym_lookup(line + strlen(CONFIG_), 0);
305 				if (sym->type == S_UNKNOWN)
306 					sym->type = S_OTHER;
307 			}
308 			if (sym->flags & def_flags) {
309 				conf_warning("override: reassigning to symbol %s", sym->name);
310 			}
311 			if (conf_set_sym_val(sym, def, def_flags, p))
312 				continue;
313 		} else {
314 			if (line[0] != '\r' && line[0] != '\n')
315 				conf_warning("unexpected data");
316 			continue;
317 		}
318 setsym:
319 		if (sym && sym_is_choice_value(sym)) {
320 			struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
321 			switch (sym->def[def].tri) {
322 			case no:
323 				break;
324 			case mod:
325 				if (cs->def[def].tri == yes) {
326 					conf_warning("%s creates inconsistent choice state", sym->name);
327 					cs->flags &= ~def_flags;
328 				}
329 				break;
330 			case yes:
331 				if (cs->def[def].tri != no)
332 					conf_warning("override: %s changes choice state", sym->name);
333 				cs->def[def].val = sym;
334 				break;
335 			}
336 			cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
337 		}
338 	}
339 	fclose(in);
340 
341 	if (modules_sym)
342 		sym_calc_value(modules_sym);
343 	return 0;
344 }
345 
346 int conf_read(const char *name)
347 {
348 	struct symbol *sym, *choice_sym;
349 	struct property *prop;
350 	struct expr *e;
351 	int i, flags;
352 
353 	sym_set_change_count(0);
354 
355 	if (conf_read_simple(name, S_DEF_USER))
356 		return 1;
357 
358 	for_all_symbols(i, sym) {
359 		sym_calc_value(sym);
360 		if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
361 			goto sym_ok;
362 		if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
363 			/* check that calculated value agrees with saved value */
364 			switch (sym->type) {
365 			case S_BOOLEAN:
366 			case S_TRISTATE:
367 				if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
368 					break;
369 				if (!sym_is_choice(sym))
370 					goto sym_ok;
371 				/* fall through */
372 			default:
373 				if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
374 					goto sym_ok;
375 				break;
376 			}
377 		} else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
378 			/* no previous value and not saved */
379 			goto sym_ok;
380 		conf_unsaved++;
381 		/* maybe print value in verbose mode... */
382 	sym_ok:
383 		if (!sym_is_choice(sym))
384 			continue;
385 		/* The choice symbol only has a set value (and thus is not new)
386 		 * if all its visible childs have values.
387 		 */
388 		prop = sym_get_choice_prop(sym);
389 		flags = sym->flags;
390 		expr_list_for_each_sym(prop->expr, e, choice_sym)
391 			if (choice_sym->visible != no)
392 				flags &= choice_sym->flags;
393 		sym->flags &= flags | ~SYMBOL_DEF_USER;
394 	}
395 
396 	for_all_symbols(i, sym) {
397 		if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
398 			/* Reset values of generates values, so they'll appear
399 			 * as new, if they should become visible, but that
400 			 * doesn't quite work if the Kconfig and the saved
401 			 * configuration disagree.
402 			 */
403 			if (sym->visible == no && !conf_unsaved)
404 				sym->flags &= ~SYMBOL_DEF_USER;
405 			switch (sym->type) {
406 			case S_STRING:
407 			case S_INT:
408 			case S_HEX:
409 				/* Reset a string value if it's out of range */
410 				if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
411 					break;
412 				sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
413 				conf_unsaved++;
414 				break;
415 			default:
416 				break;
417 			}
418 		}
419 	}
420 
421 	sym_add_change_count(conf_warnings || conf_unsaved);
422 
423 	return 0;
424 }
425 
426 /* Write a S_STRING */
427 static void conf_write_string(bool headerfile, const char *name,
428                               const char *str, FILE *out)
429 {
430 	int l;
431 	if (headerfile)
432 		fprintf(out, "#define %s%s \"", CONFIG_, name);
433 	else
434 		fprintf(out, "%s%s=\"", CONFIG_, name);
435 
436 	while (1) {
437 		l = strcspn(str, "\"\\");
438 		if (l) {
439 			xfwrite(str, l, 1, out);
440 			str += l;
441 		}
442 		if (!*str)
443 			break;
444 		fprintf(out, "\\%c", *str++);
445 	}
446 	fputs("\"\n", out);
447 }
448 
449 static void conf_write_symbol(struct symbol *sym, FILE *out, bool write_no)
450 {
451 	const char *str;
452 
453 	switch (sym->type) {
454 	case S_BOOLEAN:
455 	case S_TRISTATE:
456 		switch (sym_get_tristate_value(sym)) {
457 		case no:
458 			if (write_no)
459 				fprintf(out, "# %s%s is not set\n",
460 				    CONFIG_, sym->name);
461 			break;
462 		case mod:
463 			fprintf(out, "%s%s=m\n", CONFIG_, sym->name);
464 			break;
465 		case yes:
466 			fprintf(out, "%s%s=y\n", CONFIG_, sym->name);
467 			break;
468 		}
469 		break;
470 	case S_STRING:
471 		conf_write_string(false, sym->name, sym_get_string_value(sym), out);
472 		break;
473 	case S_HEX:
474 	case S_INT:
475 		str = sym_get_string_value(sym);
476 		fprintf(out, "%s%s=%s\n", CONFIG_, sym->name, str);
477 		break;
478 	case S_OTHER:
479 	case S_UNKNOWN:
480 		break;
481 	}
482 }
483 
484 /*
485  * Write out a minimal config.
486  * All values that has default values are skipped as this is redundant.
487  */
488 int conf_write_defconfig(const char *filename)
489 {
490 	struct symbol *sym;
491 	struct menu *menu;
492 	FILE *out;
493 
494 	out = fopen(filename, "w");
495 	if (!out)
496 		return 1;
497 
498 	sym_clear_all_valid();
499 
500 	/* Traverse all menus to find all relevant symbols */
501 	menu = rootmenu.list;
502 
503 	while (menu != NULL)
504 	{
505 		sym = menu->sym;
506 		if (sym == NULL) {
507 			if (!menu_is_visible(menu))
508 				goto next_menu;
509 		} else if (!sym_is_choice(sym)) {
510 			sym_calc_value(sym);
511 			if (!(sym->flags & SYMBOL_WRITE))
512 				goto next_menu;
513 			sym->flags &= ~SYMBOL_WRITE;
514 			/* If we cannot change the symbol - skip */
515 			if (!sym_is_changable(sym))
516 				goto next_menu;
517 			/* If symbol equals to default value - skip */
518 			if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
519 				goto next_menu;
520 
521 			/*
522 			 * If symbol is a choice value and equals to the
523 			 * default for a choice - skip.
524 			 * But only if value is bool and equal to "y" and
525 			 * choice is not "optional".
526 			 * (If choice is "optional" then all values can be "n")
527 			 */
528 			if (sym_is_choice_value(sym)) {
529 				struct symbol *cs;
530 				struct symbol *ds;
531 
532 				cs = prop_get_symbol(sym_get_choice_prop(sym));
533 				ds = sym_choice_default(cs);
534 				if (!sym_is_optional(cs) && sym == ds) {
535 					if ((sym->type == S_BOOLEAN) &&
536 					    sym_get_tristate_value(sym) == yes)
537 						goto next_menu;
538 				}
539 			}
540 			conf_write_symbol(sym, out, true);
541 		}
542 next_menu:
543 		if (menu->list != NULL) {
544 			menu = menu->list;
545 		}
546 		else if (menu->next != NULL) {
547 			menu = menu->next;
548 		} else {
549 			while ((menu = menu->parent)) {
550 				if (menu->next != NULL) {
551 					menu = menu->next;
552 					break;
553 				}
554 			}
555 		}
556 	}
557 	fclose(out);
558 	return 0;
559 }
560 
561 int conf_write(const char *name)
562 {
563 	FILE *out;
564 	struct symbol *sym;
565 	struct menu *menu;
566 	const char *basename;
567 	const char *str;
568 	char dirname[PATH_MAX+1], tmpname[PATH_MAX+1], newname[PATH_MAX+1];
569 	char *env;
570 
571 	dirname[0] = 0;
572 	if (name && name[0]) {
573 		struct stat st;
574 		char *slash;
575 
576 		if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
577 			strcpy(dirname, name);
578 			strcat(dirname, "/");
579 			basename = conf_get_configname();
580 		} else if ((slash = strrchr(name, '/'))) {
581 			int size = slash - name + 1;
582 			memcpy(dirname, name, size);
583 			dirname[size] = 0;
584 			if (slash[1])
585 				basename = slash + 1;
586 			else
587 				basename = conf_get_configname();
588 		} else
589 			basename = name;
590 	} else
591 		basename = conf_get_configname();
592 
593 	sprintf(newname, "%s%s", dirname, basename);
594 	env = getenv("KCONFIG_OVERWRITECONFIG");
595 	if (!env || !*env) {
596 		sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
597 		out = fopen(tmpname, "w");
598 	} else {
599 		*tmpname = 0;
600 		out = fopen(newname, "w");
601 	}
602 	if (!out)
603 		return 1;
604 
605 	fprintf(out, _("#\n"
606 		       "# Automatically generated make config: don't edit\n"
607 		       "# %s\n"
608 		       "#\n"),
609 		     rootmenu.prompt->text);
610 
611 	if (!conf_get_changed())
612 		sym_clear_all_valid();
613 
614 	menu = rootmenu.list;
615 	while (menu) {
616 		sym = menu->sym;
617 		if (!sym) {
618 			if (!menu_is_visible(menu))
619 				goto next;
620 			str = menu_get_prompt(menu);
621 			fprintf(out, "\n"
622 				     "#\n"
623 				     "# %s\n"
624 				     "#\n", str);
625 		} else if (!(sym->flags & SYMBOL_CHOICE)) {
626 			sym_calc_value(sym);
627 			if (!(sym->flags & SYMBOL_WRITE))
628 				goto next;
629 			sym->flags &= ~SYMBOL_WRITE;
630 			/* Write config symbol to file */
631 			conf_write_symbol(sym, out, true);
632 		}
633 
634 next:
635 		if (menu->list) {
636 			menu = menu->list;
637 			continue;
638 		}
639 		if (menu->next)
640 			menu = menu->next;
641 		else while ((menu = menu->parent)) {
642 			if (menu->next) {
643 				menu = menu->next;
644 				break;
645 			}
646 		}
647 	}
648 	fclose(out);
649 
650 	if (*tmpname) {
651 		strcat(dirname, basename);
652 		strcat(dirname, ".old");
653 		rename(newname, dirname);
654 		if (rename(tmpname, newname))
655 			return 1;
656 	}
657 
658 	conf_message(_("configuration written to %s"), newname);
659 
660 	sym_set_change_count(0);
661 
662 	return 0;
663 }
664 
665 static int conf_split_config(void)
666 {
667 	const char *name;
668 	char path[PATH_MAX+1];
669 	char *s, *d, c;
670 	struct symbol *sym;
671 	struct stat sb;
672 	int res, i, fd;
673 
674 	name = conf_get_autoconfig_name();
675 	conf_read_simple(name, S_DEF_AUTO);
676 
677 	if (chdir("include/config"))
678 		return 1;
679 
680 	res = 0;
681 	for_all_symbols(i, sym) {
682 		sym_calc_value(sym);
683 		if ((sym->flags & SYMBOL_AUTO) || !sym->name)
684 			continue;
685 		if (sym->flags & SYMBOL_WRITE) {
686 			if (sym->flags & SYMBOL_DEF_AUTO) {
687 				/*
688 				 * symbol has old and new value,
689 				 * so compare them...
690 				 */
691 				switch (sym->type) {
692 				case S_BOOLEAN:
693 				case S_TRISTATE:
694 					if (sym_get_tristate_value(sym) ==
695 					    sym->def[S_DEF_AUTO].tri)
696 						continue;
697 					break;
698 				case S_STRING:
699 				case S_HEX:
700 				case S_INT:
701 					if (!strcmp(sym_get_string_value(sym),
702 						    sym->def[S_DEF_AUTO].val))
703 						continue;
704 					break;
705 				default:
706 					break;
707 				}
708 			} else {
709 				/*
710 				 * If there is no old value, only 'no' (unset)
711 				 * is allowed as new value.
712 				 */
713 				switch (sym->type) {
714 				case S_BOOLEAN:
715 				case S_TRISTATE:
716 					if (sym_get_tristate_value(sym) == no)
717 						continue;
718 					break;
719 				default:
720 					break;
721 				}
722 			}
723 		} else if (!(sym->flags & SYMBOL_DEF_AUTO))
724 			/* There is neither an old nor a new value. */
725 			continue;
726 		/* else
727 		 *	There is an old value, but no new value ('no' (unset)
728 		 *	isn't saved in auto.conf, so the old value is always
729 		 *	different from 'no').
730 		 */
731 
732 		/* Replace all '_' and append ".h" */
733 		s = sym->name;
734 		d = path;
735 		while ((c = *s++)) {
736 			c = tolower(c);
737 			*d++ = (c == '_') ? '/' : c;
738 		}
739 		strcpy(d, ".h");
740 
741 		/* Assume directory path already exists. */
742 		fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
743 		if (fd == -1) {
744 			if (errno != ENOENT) {
745 				res = 1;
746 				break;
747 			}
748 			/*
749 			 * Create directory components,
750 			 * unless they exist already.
751 			 */
752 			d = path;
753 			while ((d = strchr(d, '/'))) {
754 				*d = 0;
755 				if (stat(path, &sb) && mkdir(path, 0755)) {
756 					res = 1;
757 					goto out;
758 				}
759 				*d++ = '/';
760 			}
761 			/* Try it again. */
762 			fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
763 			if (fd == -1) {
764 				res = 1;
765 				break;
766 			}
767 		}
768 		close(fd);
769 	}
770 out:
771 	if (chdir("../.."))
772 		return 1;
773 
774 	return res;
775 }
776 
777 int conf_write_autoconf(void)
778 {
779 	struct symbol *sym;
780 	const char *str;
781 	const char *name;
782 	FILE *out, *tristate, *out_h;
783 	int i;
784 
785 	sym_clear_all_valid();
786 
787 	file_write_dep("include/config/auto.conf.cmd");
788 
789 	if (conf_split_config())
790 		return 1;
791 
792 	out = fopen(".tmpconfig", "w");
793 	if (!out)
794 		return 1;
795 
796 	tristate = fopen(".tmpconfig_tristate", "w");
797 	if (!tristate) {
798 		fclose(out);
799 		return 1;
800 	}
801 
802 	out_h = fopen(".tmpconfig.h", "w");
803 	if (!out_h) {
804 		fclose(out);
805 		fclose(tristate);
806 		return 1;
807 	}
808 
809 	fprintf(out, "#\n"
810 		     "# Automatically generated make config: don't edit\n"
811 		     "# %s\n"
812 		     "#\n",
813 		     rootmenu.prompt->text);
814 	fprintf(tristate, "#\n"
815 			  "# Automatically generated - do not edit\n"
816 			  "\n");
817 	fprintf(out_h, "/*\n"
818 		       " * Automatically generated C config: don't edit\n"
819 		       " * %s\n"
820 		       " */\n",
821 		       rootmenu.prompt->text);
822 
823 	for_all_symbols(i, sym) {
824 		sym_calc_value(sym);
825 		if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
826 			continue;
827 
828 		/* write symbol to config file */
829 		conf_write_symbol(sym, out, false);
830 
831 		/* update autoconf and tristate files */
832 		switch (sym->type) {
833 		case S_BOOLEAN:
834 		case S_TRISTATE:
835 			switch (sym_get_tristate_value(sym)) {
836 			case no:
837 				break;
838 			case mod:
839 				fprintf(tristate, "%s%s=M\n",
840 				    CONFIG_, sym->name);
841 				fprintf(out_h, "#define %s%s_MODULE 1\n",
842 				    CONFIG_, sym->name);
843 				break;
844 			case yes:
845 				if (sym->type == S_TRISTATE)
846 					fprintf(tristate,"%s%s=Y\n",
847 					    CONFIG_, sym->name);
848 				fprintf(out_h, "#define %s%s 1\n",
849 				    CONFIG_, sym->name);
850 				break;
851 			}
852 			break;
853 		case S_STRING:
854 			conf_write_string(true, sym->name, sym_get_string_value(sym), out_h);
855 			break;
856 		case S_HEX:
857 			str = sym_get_string_value(sym);
858 			if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
859 				fprintf(out_h, "#define %s%s 0x%s\n",
860 				    CONFIG_, sym->name, str);
861 				break;
862 			}
863 		case S_INT:
864 			str = sym_get_string_value(sym);
865 			fprintf(out_h, "#define %s%s %s\n",
866 			    CONFIG_, sym->name, str);
867 			break;
868 		default:
869 			break;
870 		}
871 	}
872 	fclose(out);
873 	fclose(tristate);
874 	fclose(out_h);
875 
876 	name = getenv("KCONFIG_AUTOHEADER");
877 	if (!name)
878 		name = "include/generated/autoconf.h";
879 	if (rename(".tmpconfig.h", name))
880 		return 1;
881 	name = getenv("KCONFIG_TRISTATE");
882 	if (!name)
883 		name = "include/config/tristate.conf";
884 	if (rename(".tmpconfig_tristate", name))
885 		return 1;
886 	name = conf_get_autoconfig_name();
887 	/*
888 	 * This must be the last step, kbuild has a dependency on auto.conf
889 	 * and this marks the successful completion of the previous steps.
890 	 */
891 	if (rename(".tmpconfig", name))
892 		return 1;
893 
894 	return 0;
895 }
896 
897 static int sym_change_count;
898 static void (*conf_changed_callback)(void);
899 
900 void sym_set_change_count(int count)
901 {
902 	int _sym_change_count = sym_change_count;
903 	sym_change_count = count;
904 	if (conf_changed_callback &&
905 	    (bool)_sym_change_count != (bool)count)
906 		conf_changed_callback();
907 }
908 
909 void sym_add_change_count(int count)
910 {
911 	sym_set_change_count(count + sym_change_count);
912 }
913 
914 bool conf_get_changed(void)
915 {
916 	return sym_change_count;
917 }
918 
919 void conf_set_changed_callback(void (*fn)(void))
920 {
921 	conf_changed_callback = fn;
922 }
923 
924 static void randomize_choice_values(struct symbol *csym)
925 {
926 	struct property *prop;
927 	struct symbol *sym;
928 	struct expr *e;
929 	int cnt, def;
930 
931 	/*
932 	 * If choice is mod then we may have more items selected
933 	 * and if no then no-one.
934 	 * In both cases stop.
935 	 */
936 	if (csym->curr.tri != yes)
937 		return;
938 
939 	prop = sym_get_choice_prop(csym);
940 
941 	/* count entries in choice block */
942 	cnt = 0;
943 	expr_list_for_each_sym(prop->expr, e, sym)
944 		cnt++;
945 
946 	/*
947 	 * find a random value and set it to yes,
948 	 * set the rest to no so we have only one set
949 	 */
950 	def = (rand() % cnt);
951 
952 	cnt = 0;
953 	expr_list_for_each_sym(prop->expr, e, sym) {
954 		if (def == cnt++) {
955 			sym->def[S_DEF_USER].tri = yes;
956 			csym->def[S_DEF_USER].val = sym;
957 		}
958 		else {
959 			sym->def[S_DEF_USER].tri = no;
960 		}
961 	}
962 	csym->flags |= SYMBOL_DEF_USER;
963 	/* clear VALID to get value calculated */
964 	csym->flags &= ~(SYMBOL_VALID);
965 }
966 
967 static void set_all_choice_values(struct symbol *csym)
968 {
969 	struct property *prop;
970 	struct symbol *sym;
971 	struct expr *e;
972 
973 	prop = sym_get_choice_prop(csym);
974 
975 	/*
976 	 * Set all non-assinged choice values to no
977 	 */
978 	expr_list_for_each_sym(prop->expr, e, sym) {
979 		if (!sym_has_value(sym))
980 			sym->def[S_DEF_USER].tri = no;
981 	}
982 	csym->flags |= SYMBOL_DEF_USER;
983 	/* clear VALID to get value calculated */
984 	csym->flags &= ~(SYMBOL_VALID);
985 }
986 
987 void conf_set_all_new_symbols(enum conf_def_mode mode)
988 {
989 	struct symbol *sym, *csym;
990 	int i, cnt;
991 
992 	for_all_symbols(i, sym) {
993 		if (sym_has_value(sym))
994 			continue;
995 		switch (sym_get_type(sym)) {
996 		case S_BOOLEAN:
997 		case S_TRISTATE:
998 			switch (mode) {
999 			case def_yes:
1000 				sym->def[S_DEF_USER].tri = yes;
1001 				break;
1002 			case def_mod:
1003 				sym->def[S_DEF_USER].tri = mod;
1004 				break;
1005 			case def_no:
1006 				sym->def[S_DEF_USER].tri = no;
1007 				break;
1008 			case def_random:
1009 				cnt = sym_get_type(sym) == S_TRISTATE ? 3 : 2;
1010 				sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt);
1011 				break;
1012 			default:
1013 				continue;
1014 			}
1015 			if (!(sym_is_choice(sym) && mode == def_random))
1016 				sym->flags |= SYMBOL_DEF_USER;
1017 			break;
1018 		default:
1019 			break;
1020 		}
1021 
1022 	}
1023 
1024 	sym_clear_all_valid();
1025 
1026 	/*
1027 	 * We have different type of choice blocks.
1028 	 * If curr.tri equals to mod then we can select several
1029 	 * choice symbols in one block.
1030 	 * In this case we do nothing.
1031 	 * If curr.tri equals yes then only one symbol can be
1032 	 * selected in a choice block and we set it to yes,
1033 	 * and the rest to no.
1034 	 */
1035 	for_all_symbols(i, csym) {
1036 		if (sym_has_value(csym) || !sym_is_choice(csym))
1037 			continue;
1038 
1039 		sym_calc_value(csym);
1040 		if (mode == def_random)
1041 			randomize_choice_values(csym);
1042 		else
1043 			set_all_choice_values(csym);
1044 	}
1045 }
1046