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