1f86fca6b07f415c36025846913e29b4077799bf
[cascardo/linux.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 #define LKC_DIRECT_LINK
18 #include "lkc.h"
19
20 /* Return codes */
21 #define EUNSETOPT       2       /* if -B and -b are used and unset config
22                                  * options were found */
23
24 static void conf(struct menu *menu);
25 static void check_conf(struct menu *menu);
26
27 enum input_mode {
28         oldaskconfig,
29         silentoldconfig,
30         oldconfig,
31         allnoconfig,
32         allyesconfig,
33         allmodconfig,
34         randconfig,
35         defconfig,
36         nonint_oldconfig,
37         oldnoconfig,
38 } input_mode = oldaskconfig;
39
40 char *defconfig_file;
41
42 static int indent = 1;
43 static int valid_stdin = 1;
44 static int sync_kconfig;
45 static int conf_cnt;
46 static char line[128];
47 static struct menu *rootEntry;
48 static int unset_variables;
49
50 static void print_help(struct menu *menu)
51 {
52         struct gstr help = str_new();
53
54         menu_get_ext_help(menu, &help);
55
56         printf("\n%s\n", str_get(&help));
57         str_free(&help);
58 }
59
60 static void strip(char *str)
61 {
62         char *p = str;
63         int l;
64
65         while ((isspace(*p)))
66                 p++;
67         l = strlen(p);
68         if (p != str)
69                 memmove(str, p, l + 1);
70         if (!l)
71                 return;
72         p = str + l - 1;
73         while ((isspace(*p)))
74                 *p-- = 0;
75 }
76
77 static void check_stdin(void)
78 {
79         if (!valid_stdin) {
80                 printf(_("aborted!\n\n"));
81                 printf(_("Console input/output is redirected. "));
82                 printf(_("Run 'make oldconfig' to update configuration.\n\n"));
83                 exit(1);
84         }
85 }
86
87 static int conf_askvalue(struct symbol *sym, const char *def)
88 {
89         enum symbol_type type = sym_get_type(sym);
90
91         if (!sym_has_value(sym))
92                 printf(_("(NEW) "));
93
94         line[0] = '\n';
95         line[1] = 0;
96
97         if (!sym_is_changable(sym)) {
98                 printf("%s\n", def);
99                 line[0] = '\n';
100                 line[1] = 0;
101                 return 0;
102         }
103
104         switch (input_mode) {
105         case oldconfig:
106         case silentoldconfig:
107                 if (sym_has_value(sym)) {
108                         printf("%s\n", def);
109                         return 0;
110                 }
111                 check_stdin();
112         case oldaskconfig:
113                 fflush(stdout);
114                 fgets(line, 128, stdin);
115                 return 1;
116         default:
117                 break;
118         }
119
120         switch (type) {
121         case S_INT:
122         case S_HEX:
123         case S_STRING:
124                 printf("%s\n", def);
125                 return 1;
126         default:
127                 ;
128         }
129         printf("%s", line);
130         return 1;
131 }
132
133 static int conf_string(struct menu *menu)
134 {
135         struct symbol *sym = menu->sym;
136         const char *def;
137
138         while (1) {
139                 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
140                 printf("(%s) ", sym->name);
141                 def = sym_get_string_value(sym);
142                 if (sym_get_string_value(sym))
143                         printf("[%s] ", def);
144                 if (!conf_askvalue(sym, def))
145                         return 0;
146                 switch (line[0]) {
147                 case '\n':
148                         break;
149                 case '?':
150                         /* print help */
151                         if (line[1] == '\n') {
152                                 print_help(menu);
153                                 def = NULL;
154                                 break;
155                         }
156                 default:
157                         line[strlen(line)-1] = 0;
158                         def = line;
159                 }
160                 if (def && sym_set_string_value(sym, def))
161                         return 0;
162         }
163 }
164
165 static int conf_sym(struct menu *menu)
166 {
167         struct symbol *sym = menu->sym;
168         tristate oldval, newval;
169
170         while (1) {
171                 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
172                 if (sym->name)
173                         printf("(%s) ", sym->name);
174                 putchar('[');
175                 oldval = sym_get_tristate_value(sym);
176                 switch (oldval) {
177                 case no:
178                         putchar('N');
179                         break;
180                 case mod:
181                         putchar('M');
182                         break;
183                 case yes:
184                         putchar('Y');
185                         break;
186                 }
187                 if (oldval != no && sym_tristate_within_range(sym, no))
188                         printf("/n");
189                 if (oldval != mod && sym_tristate_within_range(sym, mod))
190                         printf("/m");
191                 if (oldval != yes && sym_tristate_within_range(sym, yes))
192                         printf("/y");
193                 if (menu_has_help(menu))
194                         printf("/?");
195                 printf("] ");
196                 if (!conf_askvalue(sym, sym_get_string_value(sym)))
197                         return 0;
198                 strip(line);
199
200                 switch (line[0]) {
201                 case 'n':
202                 case 'N':
203                         newval = no;
204                         if (!line[1] || !strcmp(&line[1], "o"))
205                                 break;
206                         continue;
207                 case 'm':
208                 case 'M':
209                         newval = mod;
210                         if (!line[1])
211                                 break;
212                         continue;
213                 case 'y':
214                 case 'Y':
215                         newval = yes;
216                         if (!line[1] || !strcmp(&line[1], "es"))
217                                 break;
218                         continue;
219                 case 0:
220                         newval = oldval;
221                         break;
222                 case '?':
223                         goto help;
224                 default:
225                         continue;
226                 }
227                 if (sym_set_tristate_value(sym, newval))
228                         return 0;
229 help:
230                 print_help(menu);
231         }
232 }
233
234 static int conf_choice(struct menu *menu)
235 {
236         struct symbol *sym, *def_sym;
237         struct menu *child;
238         bool is_new;
239
240         sym = menu->sym;
241         is_new = !sym_has_value(sym);
242         if (sym_is_changable(sym)) {
243                 conf_sym(menu);
244                 sym_calc_value(sym);
245                 switch (sym_get_tristate_value(sym)) {
246                 case no:
247                         return 1;
248                 case mod:
249                         return 0;
250                 case yes:
251                         break;
252                 }
253         } else {
254                 switch (sym_get_tristate_value(sym)) {
255                 case no:
256                         return 1;
257                 case mod:
258                         printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
259                         return 0;
260                 case yes:
261                         break;
262                 }
263         }
264
265         while (1) {
266                 int cnt, def;
267
268                 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
269                 def_sym = sym_get_choice_value(sym);
270                 cnt = def = 0;
271                 line[0] = 0;
272                 for (child = menu->list; child; child = child->next) {
273                         if (!menu_is_visible(child))
274                                 continue;
275                         if (!child->sym) {
276                                 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
277                                 continue;
278                         }
279                         cnt++;
280                         if (child->sym == def_sym) {
281                                 def = cnt;
282                                 printf("%*c", indent, '>');
283                         } else
284                                 printf("%*c", indent, ' ');
285                         printf(" %d. %s", cnt, _(menu_get_prompt(child)));
286                         if (child->sym->name)
287                                 printf(" (%s)", child->sym->name);
288                         if (!sym_has_value(child->sym))
289                                 printf(_(" (NEW)"));
290                         printf("\n");
291                 }
292                 printf(_("%*schoice"), indent - 1, "");
293                 if (cnt == 1) {
294                         printf("[1]: 1\n");
295                         goto conf_childs;
296                 }
297                 printf("[1-%d", cnt);
298                 if (menu_has_help(menu))
299                         printf("?");
300                 printf("]: ");
301                 switch (input_mode) {
302                 case oldconfig:
303                 case silentoldconfig:
304                         if (!is_new) {
305                                 cnt = def;
306                                 printf("%d\n", cnt);
307                                 break;
308                         }
309                         check_stdin();
310                 case oldaskconfig:
311                         fflush(stdout);
312                         fgets(line, 128, stdin);
313                         strip(line);
314                         if (line[0] == '?') {
315                                 print_help(menu);
316                                 continue;
317                         }
318                         if (!line[0])
319                                 cnt = def;
320                         else if (isdigit(line[0]))
321                                 cnt = atoi(line);
322                         else
323                                 continue;
324                         break;
325                 default:
326                         break;
327                 }
328
329         conf_childs:
330                 for (child = menu->list; child; child = child->next) {
331                         if (!child->sym || !menu_is_visible(child))
332                                 continue;
333                         if (!--cnt)
334                                 break;
335                 }
336                 if (!child)
337                         continue;
338                 if (line[strlen(line) - 1] == '?') {
339                         print_help(child);
340                         continue;
341                 }
342                 sym_set_choice_value(sym, child->sym);
343                 for (child = child->list; child; child = child->next) {
344                         indent += 2;
345                         conf(child);
346                         indent -= 2;
347                 }
348                 return 1;
349         }
350 }
351
352 static void conf(struct menu *menu)
353 {
354         struct symbol *sym;
355         struct property *prop;
356         struct menu *child;
357
358         if (!menu_is_visible(menu))
359                 return;
360
361         sym = menu->sym;
362         prop = menu->prompt;
363         if (prop) {
364                 const char *prompt;
365
366                 switch (prop->type) {
367                 case P_MENU:
368                         if ((input_mode == silentoldconfig ||
369                              input_mode == nonint_oldconfig ||
370                              input_mode == oldnoconfig) &&
371                             rootEntry != menu) {
372                                 check_conf(menu);
373                                 return;
374                         }
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 == nonint_oldconfig ||
430                             input_mode == oldnoconfig) {
431                                 if (input_mode == nonint_oldconfig &&
432                                     sym->name && !sym_is_choice_value(sym)) {
433                                         if (!unset_variables)
434                                                 fprintf(stderr, "The following"
435                                                   " variables are not set:\n");
436                                         fprintf(stderr, "CONFIG_%s\n",
437                                                         sym->name);
438                                         unset_variables++;
439                                 }
440                         } else {
441                                 if (!conf_cnt++)
442                                         printf(_("*\n* Restart config...\n*\n"));
443                                 rootEntry = menu_get_parent_menu(menu);
444                                 conf(rootEntry);
445                         }
446                 }
447         }
448
449         for (child = menu->list; child; child = child->next)
450                 check_conf(child);
451 }
452
453 static struct option long_opts[] = {
454         {"oldaskconfig",    no_argument,       NULL, oldaskconfig},
455         {"oldconfig",       no_argument,       NULL, oldconfig},
456         {"silentoldconfig", no_argument,       NULL, silentoldconfig},
457         {"defconfig",       optional_argument, NULL, defconfig},
458         {"allnoconfig",     no_argument,       NULL, allnoconfig},
459         {"allyesconfig",    no_argument,       NULL, allyesconfig},
460         {"allmodconfig",    no_argument,       NULL, allmodconfig},
461         {"randconfig",      no_argument,       NULL, randconfig},
462         {"nonint_oldconfig",       no_argument, NULL, nonint_oldconfig},
463         {"oldnoconfig",     no_argument,       NULL, oldnoconfig},
464         {NULL, 0, NULL, 0}
465 };
466
467 int main(int ac, char **av)
468 {
469         int opt;
470         const char *name;
471         struct stat tmpstat;
472
473         setlocale(LC_ALL, "");
474         bindtextdomain(PACKAGE, LOCALEDIR);
475         textdomain(PACKAGE);
476
477         while ((opt = getopt_long_only(ac, av, "", long_opts, NULL)) != -1) {
478                 input_mode = (enum input_mode)opt;
479                 switch (opt) {
480                 case silentoldconfig:
481                         sync_kconfig = 1;
482                         break;
483                 case defconfig:
484                         defconfig_file = optarg;
485                         break;
486                 case randconfig:
487                 {
488                         struct timeval now;
489                         unsigned int seed;
490
491                         /*
492                          * Use microseconds derived seed,
493                          * compensate for systems where it may be zero
494                          */
495                         gettimeofday(&now, NULL);
496
497                         seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
498                         srand(seed);
499                         break;
500                 }
501                 case '?':
502                         fprintf(stderr, _("See README for usage info\n"));
503                         exit(1);
504                         break;
505                 }
506         }
507         if (ac == optind) {
508                 printf(_("%s: Kconfig file missing\n"), av[0]);
509                 exit(1);
510         }
511         name = av[optind];
512         conf_parse(name);
513         //zconfdump(stdout);
514         if (sync_kconfig) {
515                 name = conf_get_configname();
516                 if (stat(name, &tmpstat)) {
517                         fprintf(stderr, _("***\n"
518                                 "*** You have not yet configured your kernel!\n"
519                                 "*** (missing kernel config file \"%s\")\n"
520                                 "***\n"
521                                 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
522                                 "*** \"make menuconfig\" or \"make xconfig\").\n"
523                                 "***\n"), name);
524                         exit(1);
525                 }
526         }
527
528         switch (input_mode) {
529         case defconfig:
530                 if (!defconfig_file)
531                         defconfig_file = conf_get_default_confname();
532                 if (conf_read(defconfig_file)) {
533                         printf(_("***\n"
534                                 "*** Can't find default configuration \"%s\"!\n"
535                                 "***\n"), defconfig_file);
536                         exit(1);
537                 }
538                 break;
539         case silentoldconfig:
540         case oldaskconfig:
541         case oldconfig:
542         case nonint_oldconfig:
543         case oldnoconfig:
544                 conf_read(NULL);
545                 break;
546         case allnoconfig:
547         case allyesconfig:
548         case allmodconfig:
549         case randconfig:
550                 name = getenv("KCONFIG_ALLCONFIG");
551                 if (name && !stat(name, &tmpstat)) {
552                         conf_read_simple(name, S_DEF_USER);
553                         break;
554                 }
555                 switch (input_mode) {
556                 case allnoconfig:       name = "allno.config"; break;
557                 case allyesconfig:      name = "allyes.config"; break;
558                 case allmodconfig:      name = "allmod.config"; break;
559                 case randconfig:        name = "allrandom.config"; break;
560                 default: break;
561                 }
562                 if (!stat(name, &tmpstat))
563                         conf_read_simple(name, S_DEF_USER);
564                 else if (!stat("all.config", &tmpstat))
565                         conf_read_simple("all.config", S_DEF_USER);
566                 break;
567         default:
568                 break;
569         }
570
571         if (sync_kconfig) {
572                 if (conf_get_changed()) {
573                         name = getenv("KCONFIG_NOSILENTUPDATE");
574                         if (name && *name) {
575                                 fprintf(stderr,
576                                         _("\n*** Kernel configuration requires explicit update.\n\n"));
577                                 return 1;
578                         }
579                 }
580                 valid_stdin = isatty(0) && isatty(1) && isatty(2);
581         }
582
583         switch (input_mode) {
584         case allnoconfig:
585                 conf_set_all_new_symbols(def_no);
586                 break;
587         case allyesconfig:
588                 conf_set_all_new_symbols(def_yes);
589                 break;
590         case allmodconfig:
591                 conf_set_all_new_symbols(def_mod);
592                 break;
593         case randconfig:
594                 conf_set_all_new_symbols(def_random);
595                 break;
596         case defconfig:
597                 conf_set_all_new_symbols(def_default);
598                 break;
599         case oldconfig:
600         case oldaskconfig:
601                 rootEntry = &rootmenu;
602                 conf(&rootmenu);
603                 input_mode = silentoldconfig;
604                 /* fall through */
605         case nonint_oldconfig:
606         case oldnoconfig:
607         case silentoldconfig:
608                 /* Update until a loop caused no more changes */
609                 do {
610                         conf_cnt = 0;
611                         check_conf(&rootmenu);
612                 } while (conf_cnt &&
613                          (input_mode != nonint_oldconfig &&
614                           input_mode != oldnoconfig));
615                 break;
616         }
617
618         if (sync_kconfig) {
619                 /* silentoldconfig is used during the build so we shall update autoconf.
620                  * All other commands are only used to generate a config.
621                  */
622                 if (conf_get_changed() && conf_write(NULL)) {
623                         fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
624                         exit(1);
625                 }
626                 if (conf_write_autoconf()) {
627                         fprintf(stderr, _("\n*** Error during update of the kernel configuration.\n\n"));
628                         return 1;
629                 }
630         } else if (!unset_variables || input_mode != nonint_oldconfig) {
631                 if (conf_write(NULL)) {
632                         fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
633                         exit(1);
634                 }
635         }
636         return unset_variables ? EUNSETOPT : 0;
637 }