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