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