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