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