kconfig: Don't go out from read config loop when you read new symbol
[muen/linux.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                 if (line[0] == '#') {
225                         if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
226                                 continue;
227                         p = strchr(line + 2 + strlen(CONFIG_), ' ');
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 + 2 + strlen(CONFIG_));
235                                 if (!sym) {
236                                         sym_add_change_count(1);
237                                         goto setsym;
238                                 }
239                         } else {
240                                 sym = sym_lookup(line + 2 + strlen(CONFIG_), 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                 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
257                         p = strchr(line + strlen(CONFIG_), '=');
258                         if (!p)
259                                 continue;
260                         *p++ = 0;
261                         p2 = strchr(p, '\n');
262                         if (p2) {
263                                 *p2-- = 0;
264                                 if (*p2 == '\r')
265                                         *p2 = 0;
266                         }
267                         if (def == S_DEF_USER) {
268                                 sym = sym_find(line + strlen(CONFIG_));
269                                 if (!sym) {
270                                         sym_add_change_count(1);
271                                         goto setsym;
272                                 }
273                         } else {
274                                 sym = sym_lookup(line + strlen(CONFIG_), 0);
275                                 if (sym->type == S_UNKNOWN)
276                                         sym->type = S_OTHER;
277                         }
278                         if (sym->flags & def_flags) {
279                                 conf_warning("override: reassigning to symbol %s", sym->name);
280                         }
281                         if (conf_set_sym_val(sym, def, def_flags, p))
282                                 continue;
283                 } else {
284                         if (line[0] != '\r' && line[0] != '\n')
285                                 conf_warning("unexpected data");
286                         continue;
287                 }
288 setsym:
289                 if (sym && sym_is_choice_value(sym)) {
290                         struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
291                         switch (sym->def[def].tri) {
292                         case no:
293                                 break;
294                         case mod:
295                                 if (cs->def[def].tri == yes) {
296                                         conf_warning("%s creates inconsistent choice state", sym->name);
297                                         cs->flags &= ~def_flags;
298                                 }
299                                 break;
300                         case yes:
301                                 if (cs->def[def].tri != no)
302                                         conf_warning("override: %s changes choice state", sym->name);
303                                 cs->def[def].val = sym;
304                                 break;
305                         }
306                         cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
307                 }
308         }
309         fclose(in);
310
311         if (modules_sym)
312                 sym_calc_value(modules_sym);
313         return 0;
314 }
315
316 int conf_read(const char *name)
317 {
318         struct symbol *sym, *choice_sym;
319         struct property *prop;
320         struct expr *e;
321         int i, flags;
322
323         sym_set_change_count(0);
324
325         if (conf_read_simple(name, S_DEF_USER))
326                 return 1;
327
328         for_all_symbols(i, sym) {
329                 sym_calc_value(sym);
330                 if (sym_is_choice(sym) || (sym->flags & SYMBOL_AUTO))
331                         goto sym_ok;
332                 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
333                         /* check that calculated value agrees with saved value */
334                         switch (sym->type) {
335                         case S_BOOLEAN:
336                         case S_TRISTATE:
337                                 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
338                                         break;
339                                 if (!sym_is_choice(sym))
340                                         goto sym_ok;
341                         default:
342                                 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
343                                         goto sym_ok;
344                                 break;
345                         }
346                 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
347                         /* no previous value and not saved */
348                         goto sym_ok;
349                 conf_unsaved++;
350                 /* maybe print value in verbose mode... */
351         sym_ok:
352                 if (!sym_is_choice(sym))
353                         continue;
354                 /* The choice symbol only has a set value (and thus is not new)
355                  * if all its visible childs have values.
356                  */
357                 prop = sym_get_choice_prop(sym);
358                 flags = sym->flags;
359                 expr_list_for_each_sym(prop->expr, e, choice_sym)
360                         if (choice_sym->visible != no)
361                                 flags &= choice_sym->flags;
362                 sym->flags &= flags | ~SYMBOL_DEF_USER;
363         }
364
365         for_all_symbols(i, sym) {
366                 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
367                         /* Reset values of generates values, so they'll appear
368                          * as new, if they should become visible, but that
369                          * doesn't quite work if the Kconfig and the saved
370                          * configuration disagree.
371                          */
372                         if (sym->visible == no && !conf_unsaved)
373                                 sym->flags &= ~SYMBOL_DEF_USER;
374                         switch (sym->type) {
375                         case S_STRING:
376                         case S_INT:
377                         case S_HEX:
378                                 /* Reset a string value if it's out of range */
379                                 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
380                                         break;
381                                 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
382                                 conf_unsaved++;
383                                 break;
384                         default:
385                                 break;
386                         }
387                 }
388         }
389
390         sym_add_change_count(conf_warnings || conf_unsaved);
391
392         return 0;
393 }
394
395 /* Write a S_STRING */
396 static void conf_write_string(bool headerfile, const char *name,
397                               const char *str, FILE *out)
398 {
399         int l;
400         if (headerfile)
401                 fprintf(out, "#define %s%s \"", CONFIG_, name);
402         else
403                 fprintf(out, "%s%s=\"", CONFIG_, name);
404
405         while (1) {
406                 l = strcspn(str, "\"\\");
407                 if (l) {
408                         xfwrite(str, l, 1, out);
409                         str += l;
410                 }
411                 if (!*str)
412                         break;
413                 fprintf(out, "\\%c", *str++);
414         }
415         fputs("\"\n", out);
416 }
417
418 static void conf_write_symbol(struct symbol *sym, enum symbol_type type,
419                               FILE *out, bool write_no)
420 {
421         const char *str;
422
423         switch (type) {
424         case S_BOOLEAN:
425         case S_TRISTATE:
426                 switch (sym_get_tristate_value(sym)) {
427                 case no:
428                         if (write_no)
429                                 fprintf(out, "# %s%s is not set\n",
430                                     CONFIG_, sym->name);
431                         break;
432                 case mod:
433                         fprintf(out, "%s%s=m\n", CONFIG_, sym->name);
434                         break;
435                 case yes:
436                         fprintf(out, "%s%s=y\n", CONFIG_, sym->name);
437                         break;
438                 }
439                 break;
440         case S_STRING:
441                 conf_write_string(false, sym->name, sym_get_string_value(sym), out);
442                 break;
443         case S_HEX:
444         case S_INT:
445                 str = sym_get_string_value(sym);
446                 fprintf(out, "%s%s=%s\n", CONFIG_, sym->name, str);
447                 break;
448         case S_OTHER:
449         case S_UNKNOWN:
450                 break;
451         }
452 }
453
454 /*
455  * Write out a minimal config.
456  * All values that has default values are skipped as this is redundant.
457  */
458 int conf_write_defconfig(const char *filename)
459 {
460         struct symbol *sym;
461         struct menu *menu;
462         FILE *out;
463
464         out = fopen(filename, "w");
465         if (!out)
466                 return 1;
467
468         sym_clear_all_valid();
469
470         /* Traverse all menus to find all relevant symbols */
471         menu = rootmenu.list;
472
473         while (menu != NULL)
474         {
475                 sym = menu->sym;
476                 if (sym == NULL) {
477                         if (!menu_is_visible(menu))
478                                 goto next_menu;
479                 } else if (!sym_is_choice(sym)) {
480                         sym_calc_value(sym);
481                         if (!(sym->flags & SYMBOL_WRITE))
482                                 goto next_menu;
483                         sym->flags &= ~SYMBOL_WRITE;
484                         /* If we cannot change the symbol - skip */
485                         if (!sym_is_changable(sym))
486                                 goto next_menu;
487                         /* If symbol equals to default value - skip */
488                         if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
489                                 goto next_menu;
490
491                         /*
492                          * If symbol is a choice value and equals to the
493                          * default for a choice - skip.
494                          * But only if value is bool and equal to "y" and
495                          * choice is not "optional".
496                          * (If choice is "optional" then all values can be "n")
497                          */
498                         if (sym_is_choice_value(sym)) {
499                                 struct symbol *cs;
500                                 struct symbol *ds;
501
502                                 cs = prop_get_symbol(sym_get_choice_prop(sym));
503                                 ds = sym_choice_default(cs);
504                                 if (!sym_is_optional(cs) && sym == ds) {
505                                         if ((sym->type == S_BOOLEAN) &&
506                                             sym_get_tristate_value(sym) == yes)
507                                                 goto next_menu;
508                                 }
509                         }
510                         conf_write_symbol(sym, sym->type, out, true);
511                 }
512 next_menu:
513                 if (menu->list != NULL) {
514                         menu = menu->list;
515                 }
516                 else if (menu->next != NULL) {
517                         menu = menu->next;
518                 } else {
519                         while ((menu = menu->parent)) {
520                                 if (menu->next != NULL) {
521                                         menu = menu->next;
522                                         break;
523                                 }
524                         }
525                 }
526         }
527         fclose(out);
528         return 0;
529 }
530
531 int conf_write(const char *name)
532 {
533         FILE *out;
534         struct symbol *sym;
535         struct menu *menu;
536         const char *basename;
537         const char *str;
538         char dirname[128], tmpname[128], newname[128];
539         enum symbol_type type;
540         time_t now;
541         int use_timestamp = 1;
542         char *env;
543
544         dirname[0] = 0;
545         if (name && name[0]) {
546                 struct stat st;
547                 char *slash;
548
549                 if (!stat(name, &st) && S_ISDIR(st.st_mode)) {
550                         strcpy(dirname, name);
551                         strcat(dirname, "/");
552                         basename = conf_get_configname();
553                 } else if ((slash = strrchr(name, '/'))) {
554                         int size = slash - name + 1;
555                         memcpy(dirname, name, size);
556                         dirname[size] = 0;
557                         if (slash[1])
558                                 basename = slash + 1;
559                         else
560                                 basename = conf_get_configname();
561                 } else
562                         basename = name;
563         } else
564                 basename = conf_get_configname();
565
566         sprintf(newname, "%s%s", dirname, basename);
567         env = getenv("KCONFIG_OVERWRITECONFIG");
568         if (!env || !*env) {
569                 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
570                 out = fopen(tmpname, "w");
571         } else {
572                 *tmpname = 0;
573                 out = fopen(newname, "w");
574         }
575         if (!out)
576                 return 1;
577
578         time(&now);
579         env = getenv("KCONFIG_NOTIMESTAMP");
580         if (env && *env)
581                 use_timestamp = 0;
582
583         fprintf(out, _("#\n"
584                        "# Automatically generated make config: don't edit\n"
585                        "# %s\n"
586                        "%s%s"
587                        "#\n"),
588                      rootmenu.prompt->text,
589                      use_timestamp ? "# " : "",
590                      use_timestamp ? ctime(&now) : "");
591
592         if (!conf_get_changed())
593                 sym_clear_all_valid();
594
595         menu = rootmenu.list;
596         while (menu) {
597                 sym = menu->sym;
598                 if (!sym) {
599                         if (!menu_is_visible(menu))
600                                 goto next;
601                         str = menu_get_prompt(menu);
602                         fprintf(out, "\n"
603                                      "#\n"
604                                      "# %s\n"
605                                      "#\n", str);
606                 } else if (!(sym->flags & SYMBOL_CHOICE)) {
607                         sym_calc_value(sym);
608                         if (!(sym->flags & SYMBOL_WRITE))
609                                 goto next;
610                         sym->flags &= ~SYMBOL_WRITE;
611                         type = sym->type;
612                         if (type == S_TRISTATE) {
613                                 sym_calc_value(modules_sym);
614                                 if (modules_sym->curr.tri == no)
615                                         type = S_BOOLEAN;
616                         }
617                         /* Write config symbol to file */
618                         conf_write_symbol(sym, type, out, true);
619                 }
620
621 next:
622                 if (menu->list) {
623                         menu = menu->list;
624                         continue;
625                 }
626                 if (menu->next)
627                         menu = menu->next;
628                 else while ((menu = menu->parent)) {
629                         if (menu->next) {
630                                 menu = menu->next;
631                                 break;
632                         }
633                 }
634         }
635         fclose(out);
636
637         if (*tmpname) {
638                 strcat(dirname, basename);
639                 strcat(dirname, ".old");
640                 rename(newname, dirname);
641                 if (rename(tmpname, newname))
642                         return 1;
643         }
644
645         printf(_("#\n"
646                  "# configuration written to %s\n"
647                  "#\n"), newname);
648
649         sym_set_change_count(0);
650
651         return 0;
652 }
653
654 static int conf_split_config(void)
655 {
656         const char *name;
657         char path[128];
658         char *s, *d, c;
659         struct symbol *sym;
660         struct stat sb;
661         int res, i, fd;
662
663         name = conf_get_autoconfig_name();
664         conf_read_simple(name, S_DEF_AUTO);
665
666         if (chdir("include/config"))
667                 return 1;
668
669         res = 0;
670         for_all_symbols(i, sym) {
671                 sym_calc_value(sym);
672                 if ((sym->flags & SYMBOL_AUTO) || !sym->name)
673                         continue;
674                 if (sym->flags & SYMBOL_WRITE) {
675                         if (sym->flags & SYMBOL_DEF_AUTO) {
676                                 /*
677                                  * symbol has old and new value,
678                                  * so compare them...
679                                  */
680                                 switch (sym->type) {
681                                 case S_BOOLEAN:
682                                 case S_TRISTATE:
683                                         if (sym_get_tristate_value(sym) ==
684                                             sym->def[S_DEF_AUTO].tri)
685                                                 continue;
686                                         break;
687                                 case S_STRING:
688                                 case S_HEX:
689                                 case S_INT:
690                                         if (!strcmp(sym_get_string_value(sym),
691                                                     sym->def[S_DEF_AUTO].val))
692                                                 continue;
693                                         break;
694                                 default:
695                                         break;
696                                 }
697                         } else {
698                                 /*
699                                  * If there is no old value, only 'no' (unset)
700                                  * is allowed as new value.
701                                  */
702                                 switch (sym->type) {
703                                 case S_BOOLEAN:
704                                 case S_TRISTATE:
705                                         if (sym_get_tristate_value(sym) == no)
706                                                 continue;
707                                         break;
708                                 default:
709                                         break;
710                                 }
711                         }
712                 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
713                         /* There is neither an old nor a new value. */
714                         continue;
715                 /* else
716                  *      There is an old value, but no new value ('no' (unset)
717                  *      isn't saved in auto.conf, so the old value is always
718                  *      different from 'no').
719                  */
720
721                 /* Replace all '_' and append ".h" */
722                 s = sym->name;
723                 d = path;
724                 while ((c = *s++)) {
725                         c = tolower(c);
726                         *d++ = (c == '_') ? '/' : c;
727                 }
728                 strcpy(d, ".h");
729
730                 /* Assume directory path already exists. */
731                 fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
732                 if (fd == -1) {
733                         if (errno != ENOENT) {
734                                 res = 1;
735                                 break;
736                         }
737                         /*
738                          * Create directory components,
739                          * unless they exist already.
740                          */
741                         d = path;
742                         while ((d = strchr(d, '/'))) {
743                                 *d = 0;
744                                 if (stat(path, &sb) && mkdir(path, 0755)) {
745                                         res = 1;
746                                         goto out;
747                                 }
748                                 *d++ = '/';
749                         }
750                         /* Try it again. */
751                         fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
752                         if (fd == -1) {
753                                 res = 1;
754                                 break;
755                         }
756                 }
757                 close(fd);
758         }
759 out:
760         if (chdir("../.."))
761                 return 1;
762
763         return res;
764 }
765
766 int conf_write_autoconf(void)
767 {
768         struct symbol *sym;
769         const char *str;
770         const char *name;
771         FILE *out, *tristate, *out_h;
772         time_t now;
773         int i;
774
775         sym_clear_all_valid();
776
777         file_write_dep("include/config/auto.conf.cmd");
778
779         if (conf_split_config())
780                 return 1;
781
782         out = fopen(".tmpconfig", "w");
783         if (!out)
784                 return 1;
785
786         tristate = fopen(".tmpconfig_tristate", "w");
787         if (!tristate) {
788                 fclose(out);
789                 return 1;
790         }
791
792         out_h = fopen(".tmpconfig.h", "w");
793         if (!out_h) {
794                 fclose(out);
795                 fclose(tristate);
796                 return 1;
797         }
798
799         time(&now);
800         fprintf(out, "#\n"
801                      "# Automatically generated make config: don't edit\n"
802                      "# %s\n"
803                      "# %s"
804                      "#\n",
805                      rootmenu.prompt->text, ctime(&now));
806         fprintf(tristate, "#\n"
807                           "# Automatically generated - do not edit\n"
808                           "\n");
809         fprintf(out_h, "/*\n"
810                        " * Automatically generated C config: don't edit\n"
811                        " * %s\n"
812                        " * %s"
813                        " */\n"
814                        "#define AUTOCONF_INCLUDED\n",
815                        rootmenu.prompt->text, ctime(&now));
816
817         for_all_symbols(i, sym) {
818                 sym_calc_value(sym);
819                 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
820                         continue;
821
822                 /* write symbol to config file */
823                 conf_write_symbol(sym, sym->type, out, false);
824
825                 /* update autoconf and tristate files */
826                 switch (sym->type) {
827                 case S_BOOLEAN:
828                 case S_TRISTATE:
829                         switch (sym_get_tristate_value(sym)) {
830                         case no:
831                                 break;
832                         case mod:
833                                 fprintf(tristate, "%s%s=M\n",
834                                     CONFIG_, sym->name);
835                                 fprintf(out_h, "#define %s%s_MODULE 1\n",
836                                     CONFIG_, sym->name);
837                                 break;
838                         case yes:
839                                 if (sym->type == S_TRISTATE)
840                                         fprintf(tristate,"%s%s=Y\n",
841                                             CONFIG_, sym->name);
842                                 fprintf(out_h, "#define %s%s 1\n",
843                                     CONFIG_, sym->name);
844                                 break;
845                         }
846                         break;
847                 case S_STRING:
848                         conf_write_string(true, sym->name, sym_get_string_value(sym), out_h);
849                         break;
850                 case S_HEX:
851                         str = sym_get_string_value(sym);
852                         if (str[0] != '0' || (str[1] != 'x' && str[1] != 'X')) {
853                                 fprintf(out_h, "#define %s%s 0x%s\n",
854                                     CONFIG_, sym->name, str);
855                                 break;
856                         }
857                 case S_INT:
858                         str = sym_get_string_value(sym);
859                         fprintf(out_h, "#define %s%s %s\n",
860                             CONFIG_, sym->name, str);
861                         break;
862                 default:
863                         break;
864                 }
865         }
866         fclose(out);
867         fclose(tristate);
868         fclose(out_h);
869
870         name = getenv("KCONFIG_AUTOHEADER");
871         if (!name)
872                 name = "include/generated/autoconf.h";
873         if (rename(".tmpconfig.h", name))
874                 return 1;
875         name = getenv("KCONFIG_TRISTATE");
876         if (!name)
877                 name = "include/config/tristate.conf";
878         if (rename(".tmpconfig_tristate", name))
879                 return 1;
880         name = conf_get_autoconfig_name();
881         /*
882          * This must be the last step, kbuild has a dependency on auto.conf
883          * and this marks the successful completion of the previous steps.
884          */
885         if (rename(".tmpconfig", name))
886                 return 1;
887
888         return 0;
889 }
890
891 static int sym_change_count;
892 static void (*conf_changed_callback)(void);
893
894 void sym_set_change_count(int count)
895 {
896         int _sym_change_count = sym_change_count;
897         sym_change_count = count;
898         if (conf_changed_callback &&
899             (bool)_sym_change_count != (bool)count)
900                 conf_changed_callback();
901 }
902
903 void sym_add_change_count(int count)
904 {
905         sym_set_change_count(count + sym_change_count);
906 }
907
908 bool conf_get_changed(void)
909 {
910         return sym_change_count;
911 }
912
913 void conf_set_changed_callback(void (*fn)(void))
914 {
915         conf_changed_callback = fn;
916 }
917
918 static void randomize_choice_values(struct symbol *csym)
919 {
920         struct property *prop;
921         struct symbol *sym;
922         struct expr *e;
923         int cnt, def;
924
925         /*
926          * If choice is mod then we may have more items slected
927          * and if no then no-one.
928          * In both cases stop.
929          */
930         if (csym->curr.tri != yes)
931                 return;
932
933         prop = sym_get_choice_prop(csym);
934
935         /* count entries in choice block */
936         cnt = 0;
937         expr_list_for_each_sym(prop->expr, e, sym)
938                 cnt++;
939
940         /*
941          * find a random value and set it to yes,
942          * set the rest to no so we have only one set
943          */
944         def = (rand() % cnt);
945
946         cnt = 0;
947         expr_list_for_each_sym(prop->expr, e, sym) {
948                 if (def == cnt++) {
949                         sym->def[S_DEF_USER].tri = yes;
950                         csym->def[S_DEF_USER].val = sym;
951                 }
952                 else {
953                         sym->def[S_DEF_USER].tri = no;
954                 }
955         }
956         csym->flags |= SYMBOL_DEF_USER;
957         /* clear VALID to get value calculated */
958         csym->flags &= ~(SYMBOL_VALID);
959 }
960
961 static void set_all_choice_values(struct symbol *csym)
962 {
963         struct property *prop;
964         struct symbol *sym;
965         struct expr *e;
966
967         prop = sym_get_choice_prop(csym);
968
969         /*
970          * Set all non-assinged choice values to no
971          */
972         expr_list_for_each_sym(prop->expr, e, sym) {
973                 if (!sym_has_value(sym))
974                         sym->def[S_DEF_USER].tri = no;
975         }
976         csym->flags |= SYMBOL_DEF_USER;
977         /* clear VALID to get value calculated */
978         csym->flags &= ~(SYMBOL_VALID);
979 }
980
981 void conf_set_all_new_symbols(enum conf_def_mode mode)
982 {
983         struct symbol *sym, *csym;
984         int i, cnt;
985
986         for_all_symbols(i, sym) {
987                 if (sym_has_value(sym))
988                         continue;
989                 switch (sym_get_type(sym)) {
990                 case S_BOOLEAN:
991                 case S_TRISTATE:
992                         switch (mode) {
993                         case def_yes:
994                                 sym->def[S_DEF_USER].tri = yes;
995                                 break;
996                         case def_mod:
997                                 sym->def[S_DEF_USER].tri = mod;
998                                 break;
999                         case def_no:
1000                                 sym->def[S_DEF_USER].tri = no;
1001                                 break;
1002                         case def_random:
1003                                 cnt = sym_get_type(sym) == S_TRISTATE ? 3 : 2;
1004                                 sym->def[S_DEF_USER].tri = (tristate)(rand() % cnt);
1005                                 break;
1006                         default:
1007                                 continue;
1008                         }
1009                         if (!(sym_is_choice(sym) && mode == def_random))
1010                                 sym->flags |= SYMBOL_DEF_USER;
1011                         break;
1012                 default:
1013                         break;
1014                 }
1015
1016         }
1017
1018         sym_clear_all_valid();
1019
1020         /*
1021          * We have different type of choice blocks.
1022          * If curr.tri equal to mod then we can select several
1023          * choice symbols in one block.
1024          * In this case we do nothing.
1025          * If curr.tri equal yes then only one symbol can be
1026          * selected in a choice block and we set it to yes,
1027          * and the rest to no.
1028          */
1029         for_all_symbols(i, csym) {
1030                 if (sym_has_value(csym) || !sym_is_choice(csym))
1031                         continue;
1032
1033                 sym_calc_value(csym);
1034                 if (mode == def_random)
1035                         randomize_choice_values(csym);
1036                 else
1037                         set_all_choice_values(csym);
1038         }
1039 }