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