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