kconfig: nuke LKC_DIRECT_LINK cruft
[pandora-kernel.git] / scripts / kconfig / 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
17 #include "lkc.h"
18
19 static void conf(struct menu *menu);
20 static void check_conf(struct menu *menu);
21
22 enum input_mode {
23         oldaskconfig,
24         silentoldconfig,
25         oldconfig,
26         allnoconfig,
27         allyesconfig,
28         allmodconfig,
29         alldefconfig,
30         randconfig,
31         defconfig,
32         savedefconfig,
33         listnewconfig,
34         oldnoconfig,
35 } input_mode = oldaskconfig;
36
37 char *defconfig_file;
38
39 static int indent = 1;
40 static int valid_stdin = 1;
41 static int sync_kconfig;
42 static int conf_cnt;
43 static char line[128];
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 oldconfig' 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         case silentoldconfig:
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, 128, stdin);
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                 case silentoldconfig:
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, 128, 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 == silentoldconfig ||
368                              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         {"oldaskconfig",    no_argument,       NULL, oldaskconfig},
448         {"oldconfig",       no_argument,       NULL, oldconfig},
449         {"silentoldconfig", no_argument,       NULL, silentoldconfig},
450         {"defconfig",       optional_argument, NULL, defconfig},
451         {"savedefconfig",   required_argument, NULL, savedefconfig},
452         {"allnoconfig",     no_argument,       NULL, allnoconfig},
453         {"allyesconfig",    no_argument,       NULL, allyesconfig},
454         {"allmodconfig",    no_argument,       NULL, allmodconfig},
455         {"alldefconfig",    no_argument,       NULL, alldefconfig},
456         {"randconfig",      no_argument,       NULL, randconfig},
457         {"listnewconfig",   no_argument,       NULL, listnewconfig},
458         {"oldnoconfig",     no_argument,       NULL, oldnoconfig},
459         {NULL, 0, NULL, 0}
460 };
461
462 int main(int ac, char **av)
463 {
464         int opt;
465         const char *name;
466         struct stat tmpstat;
467
468         setlocale(LC_ALL, "");
469         bindtextdomain(PACKAGE, LOCALEDIR);
470         textdomain(PACKAGE);
471
472         while ((opt = getopt_long(ac, av, "", long_opts, NULL)) != -1) {
473                 input_mode = (enum input_mode)opt;
474                 switch (opt) {
475                 case silentoldconfig:
476                         sync_kconfig = 1;
477                         break;
478                 case defconfig:
479                 case savedefconfig:
480                         defconfig_file = optarg;
481                         break;
482                 case randconfig:
483                 {
484                         struct timeval now;
485                         unsigned int seed;
486
487                         /*
488                          * Use microseconds derived seed,
489                          * compensate for systems where it may be zero
490                          */
491                         gettimeofday(&now, NULL);
492
493                         seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
494                         srand(seed);
495                         break;
496                 }
497                 case '?':
498                         fprintf(stderr, _("See README for usage info\n"));
499                         exit(1);
500                         break;
501                 }
502         }
503         if (ac == optind) {
504                 printf(_("%s: Kconfig file missing\n"), av[0]);
505                 exit(1);
506         }
507         name = av[optind];
508         conf_parse(name);
509         //zconfdump(stdout);
510         if (sync_kconfig) {
511                 name = conf_get_configname();
512                 if (stat(name, &tmpstat)) {
513                         fprintf(stderr, _("***\n"
514                                 "*** Configuration file \"%s\" not found!\n"
515                                 "***\n"
516                                 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
517                                 "*** \"make menuconfig\" or \"make xconfig\").\n"
518                                 "***\n"), name);
519                         exit(1);
520                 }
521         }
522
523         switch (input_mode) {
524         case defconfig:
525                 if (!defconfig_file)
526                         defconfig_file = conf_get_default_confname();
527                 if (conf_read(defconfig_file)) {
528                         printf(_("***\n"
529                                 "*** Can't find default configuration \"%s\"!\n"
530                                 "***\n"), defconfig_file);
531                         exit(1);
532                 }
533                 break;
534         case savedefconfig:
535         case silentoldconfig:
536         case oldaskconfig:
537         case oldconfig:
538         case listnewconfig:
539         case oldnoconfig:
540                 conf_read(NULL);
541                 break;
542         case allnoconfig:
543         case allyesconfig:
544         case allmodconfig:
545         case alldefconfig:
546         case randconfig:
547                 name = getenv("KCONFIG_ALLCONFIG");
548                 if (name && !stat(name, &tmpstat)) {
549                         conf_read_simple(name, S_DEF_USER);
550                         break;
551                 }
552                 switch (input_mode) {
553                 case allnoconfig:       name = "allno.config"; break;
554                 case allyesconfig:      name = "allyes.config"; break;
555                 case allmodconfig:      name = "allmod.config"; break;
556                 case alldefconfig:      name = "alldef.config"; break;
557                 case randconfig:        name = "allrandom.config"; break;
558                 default: break;
559                 }
560                 if (!stat(name, &tmpstat))
561                         conf_read_simple(name, S_DEF_USER);
562                 else if (!stat("all.config", &tmpstat))
563                         conf_read_simple("all.config", S_DEF_USER);
564                 break;
565         default:
566                 break;
567         }
568
569         if (sync_kconfig) {
570                 if (conf_get_changed()) {
571                         name = getenv("KCONFIG_NOSILENTUPDATE");
572                         if (name && *name) {
573                                 fprintf(stderr,
574                                         _("\n*** The configuration requires explicit update.\n\n"));
575                                 return 1;
576                         }
577                 }
578                 valid_stdin = isatty(0) && isatty(1) && isatty(2);
579         }
580
581         switch (input_mode) {
582         case allnoconfig:
583                 conf_set_all_new_symbols(def_no);
584                 break;
585         case allyesconfig:
586                 conf_set_all_new_symbols(def_yes);
587                 break;
588         case allmodconfig:
589                 conf_set_all_new_symbols(def_mod);
590                 break;
591         case alldefconfig:
592                 conf_set_all_new_symbols(def_default);
593                 break;
594         case randconfig:
595                 conf_set_all_new_symbols(def_random);
596                 break;
597         case defconfig:
598                 conf_set_all_new_symbols(def_default);
599                 break;
600         case savedefconfig:
601                 break;
602         case oldaskconfig:
603                 rootEntry = &rootmenu;
604                 conf(&rootmenu);
605                 input_mode = silentoldconfig;
606                 /* fall through */
607         case oldconfig:
608         case listnewconfig:
609         case oldnoconfig:
610         case silentoldconfig:
611                 /* Update until a loop caused no more changes */
612                 do {
613                         conf_cnt = 0;
614                         check_conf(&rootmenu);
615                 } while (conf_cnt &&
616                          (input_mode != listnewconfig &&
617                           input_mode != oldnoconfig));
618                 break;
619         }
620
621         if (sync_kconfig) {
622                 /* silentoldconfig is used during the build so we shall update autoconf.
623                  * All other commands are only used to generate a config.
624                  */
625                 if (conf_get_changed() && conf_write(NULL)) {
626                         fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
627                         exit(1);
628                 }
629                 if (conf_write_autoconf()) {
630                         fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
631                         return 1;
632                 }
633         } else if (input_mode == savedefconfig) {
634                 if (conf_write_defconfig(defconfig_file)) {
635                         fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
636                                 defconfig_file);
637                         return 1;
638                 }
639         } else if (input_mode != listnewconfig) {
640                 if (conf_write(NULL)) {
641                         fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
642                         exit(1);
643                 }
644         }
645         return 0;
646 }
647 /*
648  * Helper function to facilitate fgets() by Jean Sacren.
649  */
650 void xfgets(str, size, in)
651         char *str;
652         int size;
653         FILE *in;
654 {
655         if (fgets(str, size, in) == NULL)
656                 fprintf(stderr, "\nError in reading or end of file.\n");
657 }