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