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