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