kconfig: remove S_OTHER symbol type and correct dependency tracking
[carl9170fw.git] / config / 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 <stdarg.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <time.h>
15 #include <unistd.h>
16
17 #include "lkc.h"
18
19 /* return true if 'path' exists, false otherwise */
20 static bool is_present(const char *path)
21 {
22         struct stat st;
23
24         return !stat(path, &st);
25 }
26
27 /* return true if 'path' exists and it is a directory, false otherwise */
28 static bool is_dir(const char *path)
29 {
30         struct stat st;
31
32         if (stat(path, &st))
33                 return 0;
34
35         return S_ISDIR(st.st_mode);
36 }
37
38 /*
39  * Create the parent directory of the given path.
40  *
41  * For example, if 'include/config/auto.conf' is given, create 'include/config'.
42  */
43 static int make_parent_dir(const char *path)
44 {
45         char tmp[PATH_MAX + 1];
46         char *p;
47
48         strncpy(tmp, path, sizeof(tmp));
49         tmp[sizeof(tmp) - 1] = 0;
50
51         /* Remove the base name. Just return if nothing is left */
52         p = strrchr(tmp, '/');
53         if (!p)
54                 return 0;
55         *(p + 1) = 0;
56
57         /* Just in case it is an absolute path */
58         p = tmp;
59         while (*p == '/')
60                 p++;
61
62         while ((p = strchr(p, '/'))) {
63                 *p = 0;
64
65                 /* skip if the directory exists */
66                 if (!is_dir(tmp) && mkdir(tmp, 0755))
67                         return -1;
68
69                 *p = '/';
70                 while (*p == '/')
71                         p++;
72         }
73
74         return 0;
75 }
76
77 static char depfile_path[PATH_MAX];
78 static size_t depfile_prefix_len;
79
80 /* touch depfile for symbol 'name' */
81 static int conf_touch_dep(const char *name)
82 {
83         int fd, ret;
84         const char *s;
85         char *d, c;
86
87         /* check overflow: prefix + name + ".h" + '\0' must fit in buffer. */
88         if (depfile_prefix_len + strlen(name) + 3 > sizeof(depfile_path))
89                 return -1;
90
91         d = depfile_path + depfile_prefix_len;
92         s = name;
93
94         while ((c = *s++))
95                 *d++ = (c == '_') ? '/' : tolower(c);
96         strcpy(d, ".h");
97
98         /* Assume directory path already exists. */
99         fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
100         if (fd == -1) {
101                 if (errno != ENOENT)
102                         return -1;
103
104                 ret = make_parent_dir(depfile_path);
105                 if (ret)
106                         return ret;
107
108                 /* Try it again. */
109                 fd = open(depfile_path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
110                 if (fd == -1)
111                         return -1;
112         }
113         close(fd);
114
115         return 0;
116 }
117
118 struct conf_printer {
119         void (*print_symbol)(FILE *, struct symbol *, const char *, void *);
120         void (*print_comment)(FILE *, const char *, void *);
121 };
122
123 static void conf_warning(const char *fmt, ...)
124         __attribute__ ((format (printf, 1, 2)));
125
126 static void conf_message(const char *fmt, ...)
127         __attribute__ ((format (printf, 1, 2)));
128
129 static const char *conf_filename;
130 static int conf_lineno, conf_warnings;
131
132 const char conf_defname[] = "include/generated/defconfig";
133
134 static void conf_warning(const char *fmt, ...)
135 {
136         va_list ap;
137         va_start(ap, fmt);
138         fprintf(stderr, "%s:%d:warning: ", conf_filename, conf_lineno);
139         vfprintf(stderr, fmt, ap);
140         fprintf(stderr, "\n");
141         va_end(ap);
142         conf_warnings++;
143 }
144
145 static void conf_default_message_callback(const char *s)
146 {
147         printf("#\n# ");
148         printf("%s", s);
149         printf("\n#\n");
150 }
151
152 static void (*conf_message_callback)(const char *s) =
153         conf_default_message_callback;
154 void conf_set_message_callback(void (*fn)(const char *s))
155 {
156         conf_message_callback = fn;
157 }
158
159 static void conf_message(const char *fmt, ...)
160 {
161         va_list ap;
162         char buf[4096];
163
164         if (!conf_message_callback)
165                 return;
166
167         va_start(ap, fmt);
168
169         vsnprintf(buf, sizeof(buf), fmt, ap);
170         conf_message_callback(buf);
171         va_end(ap);
172 }
173
174 const char *conf_get_configname(void)
175 {
176         char *name = getenv("KCONFIG_CONFIG");
177
178         return name ? name : ".config";
179 }
180
181 const char *conf_get_autoconfig_name(void)
182 {
183         char *name = getenv("KCONFIG_AUTOCONFIG");
184
185         return name ? name : "include/generated/auto.conf";
186 }
187
188 char *conf_get_default_confname(void)
189 {
190         static char fullname[PATH_MAX+1];
191         char *env, *name;
192
193         name = expand_string(conf_defname);
194         env = getenv(SRCTREE);
195         if (env) {
196                 sprintf(fullname, "%s/%s", env, name);
197                 if (is_present(fullname))
198                         return fullname;
199         }
200         return name;
201 }
202
203 static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
204 {
205         char *p2;
206
207         switch (sym->type) {
208         case S_TRISTATE:
209                 if (p[0] == 'm') {
210                         sym->def[def].tri = mod;
211                         sym->flags |= def_flags;
212                         break;
213                 }
214                 /* fall through */
215         case S_BOOLEAN:
216                 if (p[0] == 'y') {
217                         sym->def[def].tri = yes;
218                         sym->flags |= def_flags;
219                         break;
220                 }
221                 if (p[0] == 'n') {
222                         sym->def[def].tri = no;
223                         sym->flags |= def_flags;
224                         break;
225                 }
226                 if (def != S_DEF_AUTO)
227                         conf_warning("symbol value '%s' invalid for %s",
228                                      p, sym->name);
229                 return 1;
230         case S_STRING:
231                 if (*p++ != '"')
232                         break;
233                 for (p2 = p; (p2 = strpbrk(p2, "\"\\")); p2++) {
234                         if (*p2 == '"') {
235                                 *p2 = 0;
236                                 break;
237                         }
238                         memmove(p2, p2 + 1, strlen(p2));
239                 }
240                 if (!p2) {
241                         if (def != S_DEF_AUTO)
242                                 conf_warning("invalid string found");
243                         return 1;
244                 }
245                 /* fall through */
246         case S_INT:
247         case S_HEX:
248                 if (sym_string_valid(sym, p)) {
249                         sym->def[def].val = xstrdup(p);
250                         sym->flags |= def_flags;
251                 } else {
252                         if (def != S_DEF_AUTO)
253                                 conf_warning("symbol value '%s' invalid for %s",
254                                              p, sym->name);
255                         return 1;
256                 }
257                 break;
258         default:
259                 ;
260         }
261         return 0;
262 }
263
264 #define LINE_GROWTH 16
265 static int add_byte(int c, char **lineptr, size_t slen, size_t *n)
266 {
267         char *nline;
268         size_t new_size = slen + 1;
269         if (new_size > *n) {
270                 new_size += LINE_GROWTH - 1;
271                 new_size *= 2;
272                 nline = xrealloc(*lineptr, new_size);
273                 if (!nline)
274                         return -1;
275
276                 *lineptr = nline;
277                 *n = new_size;
278         }
279
280         (*lineptr)[slen] = c;
281
282         return 0;
283 }
284
285 static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
286 {
287         char *line = *lineptr;
288         size_t slen = 0;
289
290         for (;;) {
291                 int c = getc(stream);
292
293                 switch (c) {
294                 case '\n':
295                         if (add_byte(c, &line, slen, n) < 0)
296                                 goto e_out;
297                         slen++;
298                         /* fall through */
299                 case EOF:
300                         if (add_byte('\0', &line, slen, n) < 0)
301                                 goto e_out;
302                         *lineptr = line;
303                         if (slen == 0)
304                                 return -1;
305                         return slen;
306                 default:
307                         if (add_byte(c, &line, slen, n) < 0)
308                                 goto e_out;
309                         slen++;
310                 }
311         }
312
313 e_out:
314         line[slen-1] = '\0';
315         *lineptr = line;
316         return -1;
317 }
318
319 int conf_read_simple(const char *name, int def)
320 {
321         FILE *in = NULL;
322         char   *line = NULL;
323         size_t  line_asize = 0;
324         char *p, *p2;
325         struct symbol *sym;
326         int i, def_flags;
327
328         if (name) {
329                 in = zconf_fopen(name);
330         } else {
331                 struct property *prop;
332
333                 name = conf_get_configname();
334                 in = zconf_fopen(name);
335                 if (in)
336                         goto load;
337                 sym_add_change_count(1);
338                 if (!sym_defconfig_list)
339                         return 1;
340
341                 for_all_defaults(sym_defconfig_list, prop) {
342                         if (expr_calc_value(prop->visible.expr) == no ||
343                             prop->expr->type != E_SYMBOL)
344                                 continue;
345                         sym_calc_value(prop->expr->left.sym);
346                         name = sym_get_string_value(prop->expr->left.sym);
347                         in = zconf_fopen(name);
348                         if (in) {
349                                 conf_message("using defaults found in %s",
350                                          name);
351                                 goto load;
352                         }
353                 }
354         }
355         if (!in)
356                 return 1;
357
358 load:
359         conf_filename = name;
360         conf_lineno = 0;
361         conf_warnings = 0;
362
363         def_flags = SYMBOL_DEF << def;
364         for_all_symbols(i, sym) {
365                 sym->flags |= SYMBOL_CHANGED;
366                 sym->flags &= ~(def_flags|SYMBOL_VALID);
367                 if (sym_is_choice(sym))
368                         sym->flags |= def_flags;
369                 switch (sym->type) {
370                 case S_INT:
371                 case S_HEX:
372                 case S_STRING:
373                         if (sym->def[def].val)
374                                 free(sym->def[def].val);
375                         /* fall through */
376                 default:
377                         sym->def[def].val = NULL;
378                         sym->def[def].tri = no;
379                 }
380         }
381
382         while (compat_getline(&line, &line_asize, in) != -1) {
383                 conf_lineno++;
384                 sym = NULL;
385                 if (line[0] == '#') {
386                         if (memcmp(line + 2, CONFIG_, strlen(CONFIG_)))
387                                 continue;
388                         p = strchr(line + 2 + strlen(CONFIG_), ' ');
389                         if (!p)
390                                 continue;
391                         *p++ = 0;
392                         if (strncmp(p, "is not set", 10))
393                                 continue;
394                         if (def == S_DEF_USER) {
395                                 sym = sym_find(line + 2 + strlen(CONFIG_));
396                                 if (!sym) {
397                                         sym_add_change_count(1);
398                                         continue;
399                                 }
400                         } else {
401                                 sym = sym_lookup(line + 2 + strlen(CONFIG_), 0);
402                                 if (sym->type == S_UNKNOWN)
403                                         sym->type = S_BOOLEAN;
404                         }
405                         if (sym->flags & def_flags) {
406                                 conf_warning("override: reassigning to symbol %s", sym->name);
407                         }
408                         switch (sym->type) {
409                         case S_BOOLEAN:
410                         case S_TRISTATE:
411                                 sym->def[def].tri = no;
412                                 sym->flags |= def_flags;
413                                 break;
414                         default:
415                                 ;
416                         }
417                 } else if (memcmp(line, CONFIG_, strlen(CONFIG_)) == 0) {
418                         p = strchr(line + strlen(CONFIG_), '=');
419                         if (!p)
420                                 continue;
421                         *p++ = 0;
422                         p2 = strchr(p, '\n');
423                         if (p2) {
424                                 *p2-- = 0;
425                                 if (*p2 == '\r')
426                                         *p2 = 0;
427                         }
428
429                         sym = sym_find(line + strlen(CONFIG_));
430                         if (!sym) {
431                                 if (def == S_DEF_AUTO)
432                                         /*
433                                          * Reading from include/config/auto.conf
434                                          * If CONFIG_FOO previously existed in
435                                          * auto.conf but it is missing now,
436                                          * include/config/foo.h must be touched.
437                                          */
438                                         conf_touch_dep(line + strlen(CONFIG_));
439                                 else
440                                         sym_add_change_count(1);
441                                 continue;
442                         }
443
444                         if (sym->flags & def_flags) {
445                                 conf_warning("override: reassigning to symbol %s", sym->name);
446                         }
447                         if (conf_set_sym_val(sym, def, def_flags, p))
448                                 continue;
449                 } else {
450                         if (line[0] != '\r' && line[0] != '\n')
451                                 conf_warning("unexpected data: %.*s",
452                                              (int)strcspn(line, "\r\n"), line);
453
454                         continue;
455                 }
456
457                 if (sym && sym_is_choice_value(sym)) {
458                         struct symbol *cs = prop_get_symbol(sym_get_choice_prop(sym));
459                         switch (sym->def[def].tri) {
460                         case no:
461                                 break;
462                         case mod:
463                                 if (cs->def[def].tri == yes) {
464                                         conf_warning("%s creates inconsistent choice state", sym->name);
465                                         cs->flags &= ~def_flags;
466                                 }
467                                 break;
468                         case yes:
469                                 if (cs->def[def].tri != no)
470                                         conf_warning("override: %s changes choice state", sym->name);
471                                 cs->def[def].val = sym;
472                                 break;
473                         }
474                         cs->def[def].tri = EXPR_OR(cs->def[def].tri, sym->def[def].tri);
475                 }
476         }
477         free(line);
478         fclose(in);
479         return 0;
480 }
481
482 int conf_read(const char *name)
483 {
484         struct symbol *sym;
485         int conf_unsaved = 0;
486         int i;
487
488         sym_set_change_count(0);
489
490         if (conf_read_simple(name, S_DEF_USER)) {
491                 sym_calc_value(modules_sym);
492                 return 1;
493         }
494
495         sym_calc_value(modules_sym);
496
497         for_all_symbols(i, sym) {
498                 sym_calc_value(sym);
499                 if (sym_is_choice(sym) || (sym->flags & SYMBOL_NO_WRITE))
500                         continue;
501                 if (sym_has_value(sym) && (sym->flags & SYMBOL_WRITE)) {
502                         /* check that calculated value agrees with saved value */
503                         switch (sym->type) {
504                         case S_BOOLEAN:
505                         case S_TRISTATE:
506                                 if (sym->def[S_DEF_USER].tri != sym_get_tristate_value(sym))
507                                         break;
508                                 if (!sym_is_choice(sym))
509                                         continue;
510                                 /* fall through */
511                         default:
512                                 if (!strcmp(sym->curr.val, sym->def[S_DEF_USER].val))
513                                         continue;
514                                 break;
515                         }
516                 } else if (!sym_has_value(sym) && !(sym->flags & SYMBOL_WRITE))
517                         /* no previous value and not saved */
518                         continue;
519                 conf_unsaved++;
520                 /* maybe print value in verbose mode... */
521         }
522
523         for_all_symbols(i, sym) {
524                 if (sym_has_value(sym) && !sym_is_choice_value(sym)) {
525                         /* Reset values of generates values, so they'll appear
526                          * as new, if they should become visible, but that
527                          * doesn't quite work if the Kconfig and the saved
528                          * configuration disagree.
529                          */
530                         if (sym->visible == no && !conf_unsaved)
531                                 sym->flags &= ~SYMBOL_DEF_USER;
532                         switch (sym->type) {
533                         case S_STRING:
534                         case S_INT:
535                         case S_HEX:
536                                 /* Reset a string value if it's out of range */
537                                 if (sym_string_within_range(sym, sym->def[S_DEF_USER].val))
538                                         break;
539                                 sym->flags &= ~(SYMBOL_VALID|SYMBOL_DEF_USER);
540                                 conf_unsaved++;
541                                 break;
542                         default:
543                                 break;
544                         }
545                 }
546         }
547
548         sym_add_change_count(conf_warnings || conf_unsaved);
549
550         return 0;
551 }
552
553 /*
554  * Kconfig configuration printer
555  *
556  * This printer is used when generating the resulting configuration after
557  * kconfig invocation and `defconfig' files. Unset symbol might be omitted by
558  * passing a non-NULL argument to the printer.
559  *
560  */
561 static void
562 kconfig_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
563 {
564
565         switch (sym->type) {
566         case S_BOOLEAN:
567         case S_TRISTATE:
568                 if (*value == 'n') {
569                         bool skip_unset = (arg != NULL);
570
571                         if (!skip_unset)
572                                 fprintf(fp, "# %s%s is not set\n",
573                                     CONFIG_, sym->name);
574                         return;
575                 }
576                 break;
577         default:
578                 break;
579         }
580
581         fprintf(fp, "%s%s=%s\n", CONFIG_, sym->name, value);
582 }
583
584 static void
585 kconfig_print_cmake_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
586 {
587
588         switch (sym->type) {
589         case S_BOOLEAN:
590         case S_TRISTATE:
591                 if (*value == 'n') {
592                         bool skip_unset = (arg != NULL);
593
594                         if (!skip_unset)
595                                 fprintf(fp, "set(%s%s false)\n",
596                                         CONFIG_, sym->name, value);
597                         return;
598                 } else if (*value == 'm') {
599                         abort();
600                 } else {
601                         fprintf(fp, "set(%s%s true)\n", CONFIG_, sym->name, value);
602                 }
603                 break;
604         case S_HEX: {
605                 const char *prefix = "";
606
607                 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
608                         prefix = "0x";
609                 fprintf(fp, "set(%s%s %s%s)\n",
610                     CONFIG_, sym->name, prefix, value);
611                 break;
612         }
613         case S_STRING:
614         case S_INT:
615                 fprintf(fp, "set(%s%s %s)\n",
616                     CONFIG_, sym->name, value);
617                 break;
618         default:
619                 break;
620         }
621
622 }
623
624 static void
625 kconfig_print_comment(FILE *fp, const char *value, void *arg)
626 {
627         const char *p = value;
628         size_t l;
629
630         for (;;) {
631                 l = strcspn(p, "\n");
632                 fprintf(fp, "#");
633                 if (l) {
634                         fprintf(fp, " ");
635                         xfwrite(p, l, 1, fp);
636                         p += l;
637                 }
638                 fprintf(fp, "\n");
639                 if (*p++ == '\0')
640                         break;
641         }
642 }
643
644 static struct conf_printer kconfig_printer_cb =
645 {
646         .print_symbol = kconfig_print_symbol,
647         .print_comment = kconfig_print_comment,
648 };
649
650 static struct conf_printer kconfig_printer_cmake_cb =
651 {
652         .print_symbol = kconfig_print_cmake_symbol,
653         .print_comment = kconfig_print_comment,
654 };
655
656 /*
657  * Header printer
658  *
659  * This printer is used when generating the `include/generated/autoconf.h' file.
660  */
661 static void
662 header_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
663 {
664
665         switch (sym->type) {
666         case S_BOOLEAN:
667         case S_TRISTATE: {
668                 const char *suffix = "";
669
670                 switch (*value) {
671                 case 'n':
672                         break;
673                 case 'm':
674                         suffix = "_MODULE";
675                         /* fall through */
676                 default:
677                         fprintf(fp, "#define %s%s%s 1\n",
678                             CONFIG_, sym->name, suffix);
679                 }
680                 break;
681         }
682         case S_HEX: {
683                 const char *prefix = "";
684
685                 if (value[0] != '0' || (value[1] != 'x' && value[1] != 'X'))
686                         prefix = "0x";
687                 fprintf(fp, "#define %s%s %s%s\n",
688                     CONFIG_, sym->name, prefix, value);
689                 break;
690         }
691         case S_STRING:
692         case S_INT:
693                 fprintf(fp, "#define %s%s %s\n",
694                     CONFIG_, sym->name, value);
695                 break;
696         default:
697                 break;
698         }
699
700 }
701
702 static void
703 header_print_comment(FILE *fp, const char *value, void *arg)
704 {
705         const char *p = value;
706         size_t l;
707
708         fprintf(fp, "/*\n");
709         for (;;) {
710                 l = strcspn(p, "\n");
711                 fprintf(fp, " *");
712                 if (l) {
713                         fprintf(fp, " ");
714                         xfwrite(p, l, 1, fp);
715                         p += l;
716                 }
717                 fprintf(fp, "\n");
718                 if (*p++ == '\0')
719                         break;
720         }
721         fprintf(fp, " */\n");
722 }
723
724 static struct conf_printer header_printer_cb =
725 {
726         .print_symbol = header_print_symbol,
727         .print_comment = header_print_comment,
728 };
729
730 /*
731  * Tristate printer
732  *
733  * This printer is used when generating the `include/generated/tristate.conf' file.
734  */
735 static void
736 tristate_print_symbol(FILE *fp, struct symbol *sym, const char *value, void *arg)
737 {
738
739         if (sym->type == S_TRISTATE && *value != 'n')
740                 fprintf(fp, "%s%s=%c\n", CONFIG_, sym->name, (char)toupper(*value));
741 }
742
743 static struct conf_printer tristate_printer_cb =
744 {
745         .print_symbol = tristate_print_symbol,
746         .print_comment = kconfig_print_comment,
747 };
748
749 static void conf_write_symbol(FILE *fp, struct symbol *sym,
750                               struct conf_printer *printer, void *printer_arg)
751 {
752         const char *str;
753
754         switch (sym->type) {
755         case S_UNKNOWN:
756                 break;
757         case S_STRING:
758                 str = sym_get_string_value(sym);
759                 str = sym_escape_string_value(str);
760                 printer->print_symbol(fp, sym, str, printer_arg);
761                 free((void *)str);
762                 break;
763         default:
764                 str = sym_get_string_value(sym);
765                 printer->print_symbol(fp, sym, str, printer_arg);
766         }
767 }
768
769 static void
770 conf_write_heading(FILE *fp, struct conf_printer *printer, void *printer_arg)
771 {
772         char buf[256];
773
774         snprintf(buf, sizeof(buf),
775             "\n"
776             "Automatically generated file; DO NOT EDIT.\n"
777             "%s\n",
778             rootmenu.prompt->text);
779
780         printer->print_comment(fp, buf, printer_arg);
781 }
782
783 /*
784  * Write out a minimal config.
785  * All values that has default values are skipped as this is redundant.
786  */
787 int conf_write_defconfig(const char *filename)
788 {
789         struct symbol *sym;
790         struct menu *menu;
791         FILE *out;
792
793         out = fopen(filename, "w");
794         if (!out)
795                 return 1;
796
797         sym_clear_all_valid();
798
799         /* Traverse all menus to find all relevant symbols */
800         menu = rootmenu.list;
801
802         while (menu != NULL)
803         {
804                 sym = menu->sym;
805                 if (sym == NULL) {
806                         if (!menu_is_visible(menu))
807                                 goto next_menu;
808                 } else if (!sym_is_choice(sym)) {
809                         sym_calc_value(sym);
810                         if (!(sym->flags & SYMBOL_WRITE))
811                                 goto next_menu;
812                         sym->flags &= ~SYMBOL_WRITE;
813                         /* If we cannot change the symbol - skip */
814                         if (!sym_is_changable(sym))
815                                 goto next_menu;
816                         /* If symbol equals to default value - skip */
817                         if (strcmp(sym_get_string_value(sym), sym_get_string_default(sym)) == 0)
818                                 goto next_menu;
819
820                         /*
821                          * If symbol is a choice value and equals to the
822                          * default for a choice - skip.
823                          * But only if value is bool and equal to "y" and
824                          * choice is not "optional".
825                          * (If choice is "optional" then all values can be "n")
826                          */
827                         if (sym_is_choice_value(sym)) {
828                                 struct symbol *cs;
829                                 struct symbol *ds;
830
831                                 cs = prop_get_symbol(sym_get_choice_prop(sym));
832                                 ds = sym_choice_default(cs);
833                                 if (!sym_is_optional(cs) && sym == ds) {
834                                         if ((sym->type == S_BOOLEAN) &&
835                                             sym_get_tristate_value(sym) == yes)
836                                                 goto next_menu;
837                                 }
838                         }
839                         conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
840                 }
841 next_menu:
842                 if (menu->list != NULL) {
843                         menu = menu->list;
844                 }
845                 else if (menu->next != NULL) {
846                         menu = menu->next;
847                 } else {
848                         while ((menu = menu->parent)) {
849                                 if (menu->next != NULL) {
850                                         menu = menu->next;
851                                         break;
852                                 }
853                         }
854                 }
855         }
856         fclose(out);
857         return 0;
858 }
859
860 int conf_write(const char *name)
861 {
862         FILE *out;
863         struct symbol *sym;
864         struct menu *menu;
865         const char *basename;
866         const char *str;
867         char dirname[PATH_MAX+1], tmpname[PATH_MAX+22], newname[PATH_MAX+8];
868         char *env;
869
870         dirname[0] = 0;
871         if (name && name[0]) {
872                 char *slash;
873
874                 if (is_dir(name)) {
875                         strcpy(dirname, name);
876                         strcat(dirname, "/");
877                         basename = conf_get_configname();
878                 } else if ((slash = strrchr(name, '/'))) {
879                         int size = slash - name + 1;
880                         memcpy(dirname, name, size);
881                         dirname[size] = 0;
882                         if (slash[1])
883                                 basename = slash + 1;
884                         else
885                                 basename = conf_get_configname();
886                 } else
887                         basename = name;
888         } else
889                 basename = conf_get_configname();
890
891         sprintf(newname, "%s%s", dirname, basename);
892         env = getenv("KCONFIG_OVERWRITECONFIG");
893         if (!env || !*env) {
894                 sprintf(tmpname, "%s.tmpconfig.%d", dirname, (int)getpid());
895                 out = fopen(tmpname, "w");
896         } else {
897                 *tmpname = 0;
898                 out = fopen(newname, "w");
899         }
900         if (!out)
901                 return 1;
902
903         conf_write_heading(out, &kconfig_printer_cb, NULL);
904
905         if (!conf_get_changed())
906                 sym_clear_all_valid();
907
908         menu = rootmenu.list;
909         while (menu) {
910                 sym = menu->sym;
911                 if (!sym) {
912                         if (!menu_is_visible(menu))
913                                 goto next;
914                         str = menu_get_prompt(menu);
915                         fprintf(out, "\n"
916                                      "#\n"
917                                      "# %s\n"
918                                      "#\n", str);
919                 } else if (!(sym->flags & SYMBOL_CHOICE)) {
920                         sym_calc_value(sym);
921                         if (!(sym->flags & SYMBOL_WRITE))
922                                 goto next;
923                         sym->flags &= ~SYMBOL_WRITE;
924
925                         conf_write_symbol(out, sym, &kconfig_printer_cb, NULL);
926                 }
927
928 next:
929                 if (menu->list) {
930                         menu = menu->list;
931                         continue;
932                 }
933                 if (menu->next)
934                         menu = menu->next;
935                 else while ((menu = menu->parent)) {
936                         if (menu->next) {
937                                 menu = menu->next;
938                                 break;
939                         }
940                 }
941         }
942         fclose(out);
943
944         if (*tmpname) {
945                 strcat(dirname, basename);
946                 strcat(dirname, ".old");
947                 rename(newname, dirname);
948                 if (rename(tmpname, newname))
949                         return 1;
950         }
951
952         conf_message("configuration written to %s", newname);
953
954         sym_set_change_count(0);
955
956         return 0;
957 }
958
959 /* write a dependency file as used by kbuild to track dependencies */
960 static int conf_write_dep(const char *name)
961 {
962         struct file *file;
963         FILE *out;
964
965         if (!name)
966                 name = ".kconfig.d";
967         out = fopen("..config.tmp", "w");
968         if (!out)
969                 return 1;
970         fprintf(out, "deps_config := \\\n");
971         for (file = file_list; file; file = file->next) {
972                 if (file->next)
973                         fprintf(out, "\t%s \\\n", file->name);
974                 else
975                         fprintf(out, "\t%s\n", file->name);
976         }
977         fprintf(out, "\n%s: \\\n"
978                      "\t$(deps_config)\n\n", conf_get_autoconfig_name());
979
980         env_write_dep(out, conf_get_autoconfig_name());
981
982         fprintf(out, "\n$(deps_config): ;\n");
983         fclose(out);
984
985         if (make_parent_dir(name))
986                 return 1;
987         rename("..config.tmp", name);
988         return 0;
989 }
990
991 static int conf_touch_deps(void)
992 {
993         const char *name;
994         struct symbol *sym;
995         int res, i;
996
997         strcpy(depfile_path, "include/generated/");
998         depfile_prefix_len = strlen(depfile_path);
999
1000         name = conf_get_autoconfig_name();
1001         conf_read_simple(name, S_DEF_AUTO);
1002         sym_calc_value(modules_sym);
1003
1004         for_all_symbols(i, sym) {
1005                 sym_calc_value(sym);
1006                 if ((sym->flags & SYMBOL_NO_WRITE) || !sym->name)
1007                         continue;
1008                 if (sym->flags & SYMBOL_WRITE) {
1009                         if (sym->flags & SYMBOL_DEF_AUTO) {
1010                                 /*
1011                                  * symbol has old and new value,
1012                                  * so compare them...
1013                                  */
1014                                 switch (sym->type) {
1015                                 case S_BOOLEAN:
1016                                 case S_TRISTATE:
1017                                         if (sym_get_tristate_value(sym) ==
1018                                             sym->def[S_DEF_AUTO].tri)
1019                                                 continue;
1020                                         break;
1021                                 case S_STRING:
1022                                 case S_HEX:
1023                                 case S_INT:
1024                                         if (!strcmp(sym_get_string_value(sym),
1025                                                     sym->def[S_DEF_AUTO].val))
1026                                                 continue;
1027                                         break;
1028                                 default:
1029                                         break;
1030                                 }
1031                         } else {
1032                                 /*
1033                                  * If there is no old value, only 'no' (unset)
1034                                  * is allowed as new value.
1035                                  */
1036                                 switch (sym->type) {
1037                                 case S_BOOLEAN:
1038                                 case S_TRISTATE:
1039                                         if (sym_get_tristate_value(sym) == no)
1040                                                 continue;
1041                                         break;
1042                                 default:
1043                                         break;
1044                                 }
1045                         }
1046                 } else if (!(sym->flags & SYMBOL_DEF_AUTO))
1047                         /* There is neither an old nor a new value. */
1048                         continue;
1049                 /* else
1050                  *      There is an old value, but no new value ('no' (unset)
1051                  *      isn't saved in auto.conf, so the old value is always
1052                  *      different from 'no').
1053                  */
1054
1055                 res = conf_touch_dep(sym->name);
1056                 if (res)
1057                         return res;
1058         }
1059
1060         return 0;
1061 }
1062
1063 int conf_write_autoconf(void)
1064 {
1065         struct symbol *sym;
1066         const char *name;
1067         FILE *out, *tristate, *out_h, *out_c;
1068         int i;
1069
1070         sym_clear_all_valid();
1071
1072         conf_write_dep("include/generated/auto.conf.cmd");
1073
1074         if (conf_touch_deps())
1075                 return 1;
1076
1077         out = fopen(".tmpconfig", "w");
1078         if (!out)
1079                 return 1;
1080
1081         tristate = fopen(".tmpconfig_tristate", "w");
1082         if (!tristate) {
1083                 fclose(out);
1084                 return 1;
1085         }
1086
1087         out_h = fopen(".tmpconfig.h", "w");
1088         if (!out_h) {
1089                 fclose(out);
1090                 fclose(tristate);
1091                 return 1;
1092         }
1093
1094         out_c = fopen(".tmpconfig.cmake", "w");
1095         if (!out_c) {
1096                 fclose(out);
1097                 fclose(tristate);
1098                 fclose(out_h);
1099         }
1100
1101         conf_write_heading(out, &kconfig_printer_cb, NULL);
1102
1103         conf_write_heading(tristate, &tristate_printer_cb, NULL);
1104
1105         conf_write_heading(out_h, &header_printer_cb, NULL);
1106
1107         conf_write_heading(out_c, &kconfig_printer_cmake_cb, NULL);
1108
1109         for_all_symbols(i, sym) {
1110                 sym_calc_value(sym);
1111                 if (!(sym->flags & SYMBOL_WRITE) || !sym->name)
1112                         continue;
1113
1114                 /* write symbol to auto.conf, tristate and header files */
1115                 conf_write_symbol(out, sym, &kconfig_printer_cb, (void *)1);
1116
1117                 conf_write_symbol(tristate, sym, &tristate_printer_cb, (void *)1);
1118
1119                 conf_write_symbol(out_h, sym, &header_printer_cb, NULL);
1120
1121                 conf_write_symbol(out_c, sym, &kconfig_printer_cmake_cb, NULL);
1122         }
1123         fclose(out);
1124         fclose(tristate);
1125         fclose(out_h);
1126         fclose(out_c);
1127
1128         name = getenv("KCONFIG_AUTOHEADER");
1129         if (!name)
1130                 name = "include/generated/autoconf.h";
1131         if (make_parent_dir(name))
1132                 return 1;
1133         if (rename(".tmpconfig.h", name))
1134                 return 1;
1135
1136         name = getenv("KCONFIG_TRISTATE");
1137         if (!name)
1138                 name = "include/generated/tristate.conf";
1139         if (make_parent_dir(name))
1140                 return 1;
1141         if (rename(".tmpconfig_tristate", name))
1142                 return 1;
1143
1144         name = getenv("KCONFIG_CMAKE");
1145         if (!name)
1146                 name = "config.cmake";
1147         if (make_parent_dir(name))
1148                 return 1;
1149         if (rename(".tmpconfig.cmake", name))
1150                 return 1;
1151
1152         name = conf_get_autoconfig_name();
1153         if (make_parent_dir(name))
1154                 return 1;
1155
1156         /*
1157          * This must be the last step, kbuild has a dependency on auto.conf
1158          * and this marks the successful completion of the previous steps.
1159          */
1160         if (rename(".tmpconfig", name))
1161                 return 1;
1162
1163         return 0;
1164 }
1165
1166 static int sym_change_count;
1167 static void (*conf_changed_callback)(void);
1168
1169 void sym_set_change_count(int count)
1170 {
1171         int _sym_change_count = sym_change_count;
1172         sym_change_count = count;
1173         if (conf_changed_callback &&
1174             (bool)_sym_change_count != (bool)count)
1175                 conf_changed_callback();
1176 }
1177
1178 void sym_add_change_count(int count)
1179 {
1180         sym_set_change_count(count + sym_change_count);
1181 }
1182
1183 bool conf_get_changed(void)
1184 {
1185         return sym_change_count;
1186 }
1187
1188 void conf_set_changed_callback(void (*fn)(void))
1189 {
1190         conf_changed_callback = fn;
1191 }
1192
1193 static bool randomize_choice_values(struct symbol *csym)
1194 {
1195         struct property *prop;
1196         struct symbol *sym;
1197         struct expr *e;
1198         int cnt, def;
1199
1200         /*
1201          * If choice is mod then we may have more items selected
1202          * and if no then no-one.
1203          * In both cases stop.
1204          */
1205         if (csym->curr.tri != yes)
1206                 return false;
1207
1208         prop = sym_get_choice_prop(csym);
1209
1210         /* count entries in choice block */
1211         cnt = 0;
1212         expr_list_for_each_sym(prop->expr, e, sym)
1213                 cnt++;
1214
1215         /*
1216          * find a random value and set it to yes,
1217          * set the rest to no so we have only one set
1218          */
1219         def = (rand() % cnt);
1220
1221         cnt = 0;
1222         expr_list_for_each_sym(prop->expr, e, sym) {
1223                 if (def == cnt++) {
1224                         sym->def[S_DEF_USER].tri = yes;
1225                         csym->def[S_DEF_USER].val = sym;
1226                 }
1227                 else {
1228                         sym->def[S_DEF_USER].tri = no;
1229                 }
1230                 sym->flags |= SYMBOL_DEF_USER;
1231                 /* clear VALID to get value calculated */
1232                 sym->flags &= ~SYMBOL_VALID;
1233         }
1234         csym->flags |= SYMBOL_DEF_USER;
1235         /* clear VALID to get value calculated */
1236         csym->flags &= ~(SYMBOL_VALID);
1237
1238         return true;
1239 }
1240
1241 void set_all_choice_values(struct symbol *csym)
1242 {
1243         struct property *prop;
1244         struct symbol *sym;
1245         struct expr *e;
1246
1247         prop = sym_get_choice_prop(csym);
1248
1249         /*
1250          * Set all non-assinged choice values to no
1251          */
1252         expr_list_for_each_sym(prop->expr, e, sym) {
1253                 if (!sym_has_value(sym))
1254                         sym->def[S_DEF_USER].tri = no;
1255         }
1256         csym->flags |= SYMBOL_DEF_USER;
1257         /* clear VALID to get value calculated */
1258         csym->flags &= ~(SYMBOL_VALID | SYMBOL_NEED_SET_CHOICE_VALUES);
1259 }
1260
1261 bool conf_set_all_new_symbols(enum conf_def_mode mode)
1262 {
1263         struct symbol *sym, *csym;
1264         int i, cnt, pby, pty, ptm;      /* pby: probability of bool     = y
1265                                          * pty: probability of tristate = y
1266                                          * ptm: probability of tristate = m
1267                                          */
1268
1269         pby = 50; pty = ptm = 33; /* can't go as the default in switch-case
1270                                    * below, otherwise gcc whines about
1271                                    * -Wmaybe-uninitialized */
1272         if (mode == def_random) {
1273                 int n, p[3];
1274                 char *env = getenv("KCONFIG_PROBABILITY");
1275                 n = 0;
1276                 while( env && *env ) {
1277                         char *endp;
1278                         int tmp = strtol( env, &endp, 10 );
1279                         if( tmp >= 0 && tmp <= 100 ) {
1280                                 p[n++] = tmp;
1281                         } else {
1282                                 errno = ERANGE;
1283                                 perror( "KCONFIG_PROBABILITY" );
1284                                 exit( 1 );
1285                         }
1286                         env = (*endp == ':') ? endp+1 : endp;
1287                         if( n >=3 ) {
1288                                 break;
1289                         }
1290                 }
1291                 switch( n ) {
1292                 case 1:
1293                         pby = p[0]; ptm = pby/2; pty = pby-ptm;
1294                         break;
1295                 case 2:
1296                         pty = p[0]; ptm = p[1]; pby = pty + ptm;
1297                         break;
1298                 case 3:
1299                         pby = p[0]; pty = p[1]; ptm = p[2];
1300                         break;
1301                 }
1302
1303                 if( pty+ptm > 100 ) {
1304                         errno = ERANGE;
1305                         perror( "KCONFIG_PROBABILITY" );
1306                         exit( 1 );
1307                 }
1308         }
1309         bool has_changed = false;
1310
1311         for_all_symbols(i, sym) {
1312                 if (sym_has_value(sym) || (sym->flags & SYMBOL_VALID))
1313                         continue;
1314                 switch (sym_get_type(sym)) {
1315                 case S_BOOLEAN:
1316                 case S_TRISTATE:
1317                         has_changed = true;
1318                         switch (mode) {
1319                         case def_yes:
1320                                 sym->def[S_DEF_USER].tri = yes;
1321                                 break;
1322                         case def_mod:
1323                                 sym->def[S_DEF_USER].tri = mod;
1324                                 break;
1325                         case def_no:
1326                                 if (sym->flags & SYMBOL_ALLNOCONFIG_Y)
1327                                         sym->def[S_DEF_USER].tri = yes;
1328                                 else
1329                                         sym->def[S_DEF_USER].tri = no;
1330                                 break;
1331                         case def_random:
1332                                 sym->def[S_DEF_USER].tri = no;
1333                                 cnt = rand() % 100;
1334                                 if (sym->type == S_TRISTATE) {
1335                                         if (cnt < pty)
1336                                                 sym->def[S_DEF_USER].tri = yes;
1337                                         else if (cnt < (pty+ptm))
1338                                                 sym->def[S_DEF_USER].tri = mod;
1339                                 } else if (cnt < pby)
1340                                         sym->def[S_DEF_USER].tri = yes;
1341                                 break;
1342                         default:
1343                                 continue;
1344                         }
1345                         if (!(sym_is_choice(sym) && mode == def_random))
1346                                 sym->flags |= SYMBOL_DEF_USER;
1347                         break;
1348                 default:
1349                         break;
1350                 }
1351
1352         }
1353
1354         sym_clear_all_valid();
1355
1356         /*
1357          * We have different type of choice blocks.
1358          * If curr.tri equals to mod then we can select several
1359          * choice symbols in one block.
1360          * In this case we do nothing.
1361          * If curr.tri equals yes then only one symbol can be
1362          * selected in a choice block and we set it to yes,
1363          * and the rest to no.
1364          */
1365         if (mode != def_random) {
1366                 for_all_symbols(i, csym) {
1367                         if ((sym_is_choice(csym) && !sym_has_value(csym)) ||
1368                             sym_is_choice_value(csym))
1369                                 csym->flags |= SYMBOL_NEED_SET_CHOICE_VALUES;
1370                 }
1371         }
1372
1373         for_all_symbols(i, csym) {
1374                 if (sym_has_value(csym) || !sym_is_choice(csym))
1375                         continue;
1376
1377                 sym_calc_value(csym);
1378                 if (mode == def_random)
1379                         has_changed = randomize_choice_values(csym);
1380                 else {
1381                         set_all_choice_values(csym);
1382                         has_changed = true;
1383                 }
1384         }
1385
1386         return has_changed;
1387 }