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