kconfig: move conf_rewrite_mod_or_yes() to conf.c
[sfrench/cifs-2.6.git] / scripts / kconfig / conf.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
4  */
5
6 #include <ctype.h>
7 #include <limits.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include <unistd.h>
13 #include <getopt.h>
14 #include <sys/time.h>
15 #include <errno.h>
16
17 #include "lkc.h"
18
19 static void conf(struct menu *menu);
20 static void check_conf(struct menu *menu);
21
22 enum input_mode {
23         oldaskconfig,
24         syncconfig,
25         oldconfig,
26         allnoconfig,
27         allyesconfig,
28         allmodconfig,
29         alldefconfig,
30         randconfig,
31         defconfig,
32         savedefconfig,
33         listnewconfig,
34         helpnewconfig,
35         olddefconfig,
36         yes2modconfig,
37         mod2yesconfig,
38 };
39 static enum input_mode input_mode = oldaskconfig;
40 static int input_mode_opt;
41 static int indent = 1;
42 static int tty_stdio;
43 static int sync_kconfig;
44 static int conf_cnt;
45 static char line[PATH_MAX];
46 static struct menu *rootEntry;
47
48 static void print_help(struct menu *menu)
49 {
50         struct gstr help = str_new();
51
52         menu_get_ext_help(menu, &help);
53
54         printf("\n%s\n", str_get(&help));
55         str_free(&help);
56 }
57
58 static void strip(char *str)
59 {
60         char *p = str;
61         int l;
62
63         while ((isspace(*p)))
64                 p++;
65         l = strlen(p);
66         if (p != str)
67                 memmove(str, p, l + 1);
68         if (!l)
69                 return;
70         p = str + l - 1;
71         while ((isspace(*p)))
72                 *p-- = 0;
73 }
74
75 /* Helper function to facilitate fgets() by Jean Sacren. */
76 static void xfgets(char *str, int size, FILE *in)
77 {
78         if (!fgets(str, size, in))
79                 fprintf(stderr, "\nError in reading or end of file.\n");
80
81         if (!tty_stdio)
82                 printf("%s", str);
83 }
84
85 static void set_randconfig_seed(void)
86 {
87         unsigned int seed;
88         char *env;
89         bool seed_set = false;
90
91         env = getenv("KCONFIG_SEED");
92         if (env && *env) {
93                 char *endp;
94
95                 seed = strtol(env, &endp, 0);
96                 if (*endp == '\0')
97                         seed_set = true;
98         }
99
100         if (!seed_set) {
101                 struct timeval now;
102
103                 /*
104                  * Use microseconds derived seed, compensate for systems where it may
105                  * be zero.
106                  */
107                 gettimeofday(&now, NULL);
108                 seed = (now.tv_sec + 1) * (now.tv_usec + 1);
109         }
110
111         printf("KCONFIG_SEED=0x%X\n", seed);
112         srand(seed);
113 }
114
115 static void conf_rewrite_mod_or_yes(enum conf_def_mode mode)
116 {
117         struct symbol *sym;
118         int i;
119         tristate old_val = (mode == def_y2m) ? yes : mod;
120         tristate new_val = (mode == def_y2m) ? mod : yes;
121
122         for_all_symbols(i, sym) {
123                 if (sym_get_type(sym) == S_TRISTATE &&
124                     sym->def[S_DEF_USER].tri == old_val)
125                         sym->def[S_DEF_USER].tri = new_val;
126         }
127         sym_clear_all_valid();
128 }
129
130 static int conf_askvalue(struct symbol *sym, const char *def)
131 {
132         if (!sym_has_value(sym))
133                 printf("(NEW) ");
134
135         line[0] = '\n';
136         line[1] = 0;
137
138         if (!sym_is_changeable(sym)) {
139                 printf("%s\n", def);
140                 line[0] = '\n';
141                 line[1] = 0;
142                 return 0;
143         }
144
145         switch (input_mode) {
146         case oldconfig:
147         case syncconfig:
148                 if (sym_has_value(sym)) {
149                         printf("%s\n", def);
150                         return 0;
151                 }
152                 /* fall through */
153         default:
154                 fflush(stdout);
155                 xfgets(line, sizeof(line), stdin);
156                 break;
157         }
158
159         return 1;
160 }
161
162 static int conf_string(struct menu *menu)
163 {
164         struct symbol *sym = menu->sym;
165         const char *def;
166
167         while (1) {
168                 printf("%*s%s ", indent - 1, "", menu->prompt->text);
169                 printf("(%s) ", sym->name);
170                 def = sym_get_string_value(sym);
171                 if (def)
172                         printf("[%s] ", def);
173                 if (!conf_askvalue(sym, def))
174                         return 0;
175                 switch (line[0]) {
176                 case '\n':
177                         break;
178                 case '?':
179                         /* print help */
180                         if (line[1] == '\n') {
181                                 print_help(menu);
182                                 def = NULL;
183                                 break;
184                         }
185                         /* fall through */
186                 default:
187                         line[strlen(line)-1] = 0;
188                         def = line;
189                 }
190                 if (def && sym_set_string_value(sym, def))
191                         return 0;
192         }
193 }
194
195 static int conf_sym(struct menu *menu)
196 {
197         struct symbol *sym = menu->sym;
198         tristate oldval, newval;
199
200         while (1) {
201                 printf("%*s%s ", indent - 1, "", menu->prompt->text);
202                 if (sym->name)
203                         printf("(%s) ", sym->name);
204                 putchar('[');
205                 oldval = sym_get_tristate_value(sym);
206                 switch (oldval) {
207                 case no:
208                         putchar('N');
209                         break;
210                 case mod:
211                         putchar('M');
212                         break;
213                 case yes:
214                         putchar('Y');
215                         break;
216                 }
217                 if (oldval != no && sym_tristate_within_range(sym, no))
218                         printf("/n");
219                 if (oldval != mod && sym_tristate_within_range(sym, mod))
220                         printf("/m");
221                 if (oldval != yes && sym_tristate_within_range(sym, yes))
222                         printf("/y");
223                 printf("/?] ");
224                 if (!conf_askvalue(sym, sym_get_string_value(sym)))
225                         return 0;
226                 strip(line);
227
228                 switch (line[0]) {
229                 case 'n':
230                 case 'N':
231                         newval = no;
232                         if (!line[1] || !strcmp(&line[1], "o"))
233                                 break;
234                         continue;
235                 case 'm':
236                 case 'M':
237                         newval = mod;
238                         if (!line[1])
239                                 break;
240                         continue;
241                 case 'y':
242                 case 'Y':
243                         newval = yes;
244                         if (!line[1] || !strcmp(&line[1], "es"))
245                                 break;
246                         continue;
247                 case 0:
248                         newval = oldval;
249                         break;
250                 case '?':
251                         goto help;
252                 default:
253                         continue;
254                 }
255                 if (sym_set_tristate_value(sym, newval))
256                         return 0;
257 help:
258                 print_help(menu);
259         }
260 }
261
262 static int conf_choice(struct menu *menu)
263 {
264         struct symbol *sym, *def_sym;
265         struct menu *child;
266         bool is_new;
267
268         sym = menu->sym;
269         is_new = !sym_has_value(sym);
270         if (sym_is_changeable(sym)) {
271                 conf_sym(menu);
272                 sym_calc_value(sym);
273                 switch (sym_get_tristate_value(sym)) {
274                 case no:
275                         return 1;
276                 case mod:
277                         return 0;
278                 case yes:
279                         break;
280                 }
281         } else {
282                 switch (sym_get_tristate_value(sym)) {
283                 case no:
284                         return 1;
285                 case mod:
286                         printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
287                         return 0;
288                 case yes:
289                         break;
290                 }
291         }
292
293         while (1) {
294                 int cnt, def;
295
296                 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
297                 def_sym = sym_get_choice_value(sym);
298                 cnt = def = 0;
299                 line[0] = 0;
300                 for (child = menu->list; child; child = child->next) {
301                         if (!menu_is_visible(child))
302                                 continue;
303                         if (!child->sym) {
304                                 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
305                                 continue;
306                         }
307                         cnt++;
308                         if (child->sym == def_sym) {
309                                 def = cnt;
310                                 printf("%*c", indent, '>');
311                         } else
312                                 printf("%*c", indent, ' ');
313                         printf(" %d. %s", cnt, menu_get_prompt(child));
314                         if (child->sym->name)
315                                 printf(" (%s)", child->sym->name);
316                         if (!sym_has_value(child->sym))
317                                 printf(" (NEW)");
318                         printf("\n");
319                 }
320                 printf("%*schoice", indent - 1, "");
321                 if (cnt == 1) {
322                         printf("[1]: 1\n");
323                         goto conf_childs;
324                 }
325                 printf("[1-%d?]: ", cnt);
326                 switch (input_mode) {
327                 case oldconfig:
328                 case syncconfig:
329                         if (!is_new) {
330                                 cnt = def;
331                                 printf("%d\n", cnt);
332                                 break;
333                         }
334                         /* fall through */
335                 case oldaskconfig:
336                         fflush(stdout);
337                         xfgets(line, sizeof(line), stdin);
338                         strip(line);
339                         if (line[0] == '?') {
340                                 print_help(menu);
341                                 continue;
342                         }
343                         if (!line[0])
344                                 cnt = def;
345                         else if (isdigit(line[0]))
346                                 cnt = atoi(line);
347                         else
348                                 continue;
349                         break;
350                 default:
351                         break;
352                 }
353
354         conf_childs:
355                 for (child = menu->list; child; child = child->next) {
356                         if (!child->sym || !menu_is_visible(child))
357                                 continue;
358                         if (!--cnt)
359                                 break;
360                 }
361                 if (!child)
362                         continue;
363                 if (line[0] && line[strlen(line) - 1] == '?') {
364                         print_help(child);
365                         continue;
366                 }
367                 sym_set_choice_value(sym, child->sym);
368                 for (child = child->list; child; child = child->next) {
369                         indent += 2;
370                         conf(child);
371                         indent -= 2;
372                 }
373                 return 1;
374         }
375 }
376
377 static void conf(struct menu *menu)
378 {
379         struct symbol *sym;
380         struct property *prop;
381         struct menu *child;
382
383         if (!menu_is_visible(menu))
384                 return;
385
386         sym = menu->sym;
387         prop = menu->prompt;
388         if (prop) {
389                 const char *prompt;
390
391                 switch (prop->type) {
392                 case P_MENU:
393                         /*
394                          * Except in oldaskconfig mode, we show only menus that
395                          * contain new symbols.
396                          */
397                         if (input_mode != oldaskconfig && rootEntry != menu) {
398                                 check_conf(menu);
399                                 return;
400                         }
401                         /* fall through */
402                 case P_COMMENT:
403                         prompt = menu_get_prompt(menu);
404                         if (prompt)
405                                 printf("%*c\n%*c %s\n%*c\n",
406                                         indent, '*',
407                                         indent, '*', prompt,
408                                         indent, '*');
409                 default:
410                         ;
411                 }
412         }
413
414         if (!sym)
415                 goto conf_childs;
416
417         if (sym_is_choice(sym)) {
418                 conf_choice(menu);
419                 if (sym->curr.tri != mod)
420                         return;
421                 goto conf_childs;
422         }
423
424         switch (sym->type) {
425         case S_INT:
426         case S_HEX:
427         case S_STRING:
428                 conf_string(menu);
429                 break;
430         default:
431                 conf_sym(menu);
432                 break;
433         }
434
435 conf_childs:
436         if (sym)
437                 indent += 2;
438         for (child = menu->list; child; child = child->next)
439                 conf(child);
440         if (sym)
441                 indent -= 2;
442 }
443
444 static void check_conf(struct menu *menu)
445 {
446         struct symbol *sym;
447         struct menu *child;
448
449         if (!menu_is_visible(menu))
450                 return;
451
452         sym = menu->sym;
453         if (sym && !sym_has_value(sym) &&
454             (sym_is_changeable(sym) ||
455              (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) {
456
457                 switch (input_mode) {
458                 case listnewconfig:
459                         if (sym->name) {
460                                 const char *str;
461
462                                 if (sym->type == S_STRING) {
463                                         str = sym_get_string_value(sym);
464                                         str = sym_escape_string_value(str);
465                                         printf("%s%s=%s\n", CONFIG_, sym->name, str);
466                                         free((void *)str);
467                                 } else {
468                                         str = sym_get_string_value(sym);
469                                         printf("%s%s=%s\n", CONFIG_, sym->name, str);
470                                 }
471                         }
472                         break;
473                 case helpnewconfig:
474                         printf("-----\n");
475                         print_help(menu);
476                         printf("-----\n");
477                         break;
478                 default:
479                         if (!conf_cnt++)
480                                 printf("*\n* Restart config...\n*\n");
481                         rootEntry = menu_get_parent_menu(menu);
482                         conf(rootEntry);
483                         break;
484                 }
485         }
486
487         for (child = menu->list; child; child = child->next)
488                 check_conf(child);
489 }
490
491 static struct option long_opts[] = {
492         {"help",          no_argument,       NULL,            'h'},
493         {"silent",        no_argument,       NULL,            's'},
494         {"oldaskconfig",  no_argument,       &input_mode_opt, oldaskconfig},
495         {"oldconfig",     no_argument,       &input_mode_opt, oldconfig},
496         {"syncconfig",    no_argument,       &input_mode_opt, syncconfig},
497         {"defconfig",     required_argument, &input_mode_opt, defconfig},
498         {"savedefconfig", required_argument, &input_mode_opt, savedefconfig},
499         {"allnoconfig",   no_argument,       &input_mode_opt, allnoconfig},
500         {"allyesconfig",  no_argument,       &input_mode_opt, allyesconfig},
501         {"allmodconfig",  no_argument,       &input_mode_opt, allmodconfig},
502         {"alldefconfig",  no_argument,       &input_mode_opt, alldefconfig},
503         {"randconfig",    no_argument,       &input_mode_opt, randconfig},
504         {"listnewconfig", no_argument,       &input_mode_opt, listnewconfig},
505         {"helpnewconfig", no_argument,       &input_mode_opt, helpnewconfig},
506         {"olddefconfig",  no_argument,       &input_mode_opt, olddefconfig},
507         {"yes2modconfig", no_argument,       &input_mode_opt, yes2modconfig},
508         {"mod2yesconfig", no_argument,       &input_mode_opt, mod2yesconfig},
509         {NULL, 0, NULL, 0}
510 };
511
512 static void conf_usage(const char *progname)
513 {
514         printf("Usage: %s [options] <kconfig-file>\n", progname);
515         printf("\n");
516         printf("Generic options:\n");
517         printf("  -h, --help              Print this message and exit.\n");
518         printf("  -s, --silent            Do not print log.\n");
519         printf("\n");
520         printf("Mode options:\n");
521         printf("  --listnewconfig         List new options\n");
522         printf("  --helpnewconfig         List new options and help text\n");
523         printf("  --oldaskconfig          Start a new configuration using a line-oriented program\n");
524         printf("  --oldconfig             Update a configuration using a provided .config as base\n");
525         printf("  --syncconfig            Similar to oldconfig but generates configuration in\n"
526                "                          include/{generated/,config/}\n");
527         printf("  --olddefconfig          Same as oldconfig but sets new symbols to their default value\n");
528         printf("  --defconfig <file>      New config with default defined in <file>\n");
529         printf("  --savedefconfig <file>  Save the minimal current configuration to <file>\n");
530         printf("  --allnoconfig           New config where all options are answered with no\n");
531         printf("  --allyesconfig          New config where all options are answered with yes\n");
532         printf("  --allmodconfig          New config where all options are answered with mod\n");
533         printf("  --alldefconfig          New config with all symbols set to default\n");
534         printf("  --randconfig            New config with random answer to all options\n");
535         printf("  --yes2modconfig         Change answers from yes to mod if possible\n");
536         printf("  --mod2yesconfig         Change answers from mod to yes if possible\n");
537         printf("  (If none of the above is given, --oldaskconfig is the default)\n");
538 }
539
540 int main(int ac, char **av)
541 {
542         const char *progname = av[0];
543         int opt;
544         const char *name, *defconfig_file = NULL /* gcc uninit */;
545         int no_conf_write = 0;
546
547         tty_stdio = isatty(0) && isatty(1);
548
549         while ((opt = getopt_long(ac, av, "hs", long_opts, NULL)) != -1) {
550                 switch (opt) {
551                 case 'h':
552                         conf_usage(progname);
553                         exit(1);
554                         break;
555                 case 's':
556                         conf_set_message_callback(NULL);
557                         break;
558                 case 0:
559                         input_mode = input_mode_opt;
560                         switch (input_mode) {
561                         case syncconfig:
562                                 /*
563                                  * syncconfig is invoked during the build stage.
564                                  * Suppress distracting
565                                  *   "configuration written to ..."
566                                  */
567                                 conf_set_message_callback(NULL);
568                                 sync_kconfig = 1;
569                                 break;
570                         case defconfig:
571                         case savedefconfig:
572                                 defconfig_file = optarg;
573                                 break;
574                         case randconfig:
575                                 set_randconfig_seed();
576                                 break;
577                         default:
578                                 break;
579                         }
580                 default:
581                         break;
582                 }
583         }
584         if (ac == optind) {
585                 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
586                 conf_usage(progname);
587                 exit(1);
588         }
589         conf_parse(av[optind]);
590         //zconfdump(stdout);
591
592         switch (input_mode) {
593         case defconfig:
594                 if (conf_read(defconfig_file)) {
595                         fprintf(stderr,
596                                 "***\n"
597                                   "*** Can't find default configuration \"%s\"!\n"
598                                   "***\n",
599                                 defconfig_file);
600                         exit(1);
601                 }
602                 break;
603         case savedefconfig:
604         case syncconfig:
605         case oldaskconfig:
606         case oldconfig:
607         case listnewconfig:
608         case helpnewconfig:
609         case olddefconfig:
610         case yes2modconfig:
611         case mod2yesconfig:
612                 conf_read(NULL);
613                 break;
614         case allnoconfig:
615         case allyesconfig:
616         case allmodconfig:
617         case alldefconfig:
618         case randconfig:
619                 name = getenv("KCONFIG_ALLCONFIG");
620                 if (!name)
621                         break;
622                 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
623                         if (conf_read_simple(name, S_DEF_USER)) {
624                                 fprintf(stderr,
625                                         "*** Can't read seed configuration \"%s\"!\n",
626                                         name);
627                                 exit(1);
628                         }
629                         break;
630                 }
631                 switch (input_mode) {
632                 case allnoconfig:       name = "allno.config"; break;
633                 case allyesconfig:      name = "allyes.config"; break;
634                 case allmodconfig:      name = "allmod.config"; break;
635                 case alldefconfig:      name = "alldef.config"; break;
636                 case randconfig:        name = "allrandom.config"; break;
637                 default: break;
638                 }
639                 if (conf_read_simple(name, S_DEF_USER) &&
640                     conf_read_simple("all.config", S_DEF_USER)) {
641                         fprintf(stderr,
642                                 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
643                                 name);
644                         exit(1);
645                 }
646                 break;
647         default:
648                 break;
649         }
650
651         if (sync_kconfig) {
652                 name = getenv("KCONFIG_NOSILENTUPDATE");
653                 if (name && *name) {
654                         if (conf_get_changed()) {
655                                 fprintf(stderr,
656                                         "\n*** The configuration requires explicit update.\n\n");
657                                 return 1;
658                         }
659                         no_conf_write = 1;
660                 }
661         }
662
663         switch (input_mode) {
664         case allnoconfig:
665                 conf_set_all_new_symbols(def_no);
666                 break;
667         case allyesconfig:
668                 conf_set_all_new_symbols(def_yes);
669                 break;
670         case allmodconfig:
671                 conf_set_all_new_symbols(def_mod);
672                 break;
673         case alldefconfig:
674                 conf_set_all_new_symbols(def_default);
675                 break;
676         case randconfig:
677                 /* Really nothing to do in this loop */
678                 while (conf_set_all_new_symbols(def_random)) ;
679                 break;
680         case defconfig:
681                 conf_set_all_new_symbols(def_default);
682                 break;
683         case savedefconfig:
684                 break;
685         case yes2modconfig:
686                 conf_rewrite_mod_or_yes(def_y2m);
687                 break;
688         case mod2yesconfig:
689                 conf_rewrite_mod_or_yes(def_m2y);
690                 break;
691         case oldaskconfig:
692                 rootEntry = &rootmenu;
693                 conf(&rootmenu);
694                 input_mode = oldconfig;
695                 /* fall through */
696         case oldconfig:
697         case listnewconfig:
698         case helpnewconfig:
699         case syncconfig:
700                 /* Update until a loop caused no more changes */
701                 do {
702                         conf_cnt = 0;
703                         check_conf(&rootmenu);
704                 } while (conf_cnt);
705                 break;
706         case olddefconfig:
707         default:
708                 break;
709         }
710
711         if (input_mode == savedefconfig) {
712                 if (conf_write_defconfig(defconfig_file)) {
713                         fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
714                                 defconfig_file);
715                         return 1;
716                 }
717         } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
718                 if (!no_conf_write && conf_write(NULL)) {
719                         fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
720                         exit(1);
721                 }
722
723                 /*
724                  * Create auto.conf if it does not exist.
725                  * This prevents GNU Make 4.1 or older from emitting
726                  * "include/config/auto.conf: No such file or directory"
727                  * in the top-level Makefile
728                  *
729                  * syncconfig always creates or updates auto.conf because it is
730                  * used during the build.
731                  */
732                 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
733                         fprintf(stderr,
734                                 "\n*** Error during sync of the configuration.\n\n");
735                         return 1;
736                 }
737         }
738         return 0;
739 }