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