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