kconfig: annotate non-trivial fall-trough
[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 #define LKC_DIRECT_LINK
18 #include "lkc.h"
19
20 static void conf(struct menu *menu);
21 static void check_conf(struct menu *menu);
22
23 enum input_mode {
24         oldaskconfig,
25         silentoldconfig,
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 char *defconfig_file;
39
40 static int indent = 1;
41 static int valid_stdin = 1;
42 static int sync_kconfig;
43 static int conf_cnt;
44 static char line[128];
45 static struct menu *rootEntry;
46
47 static void print_help(struct menu *menu)
48 {
49         struct gstr help = str_new();
50
51         menu_get_ext_help(menu, &help);
52
53         printf("\n%s\n", str_get(&help));
54         str_free(&help);
55 }
56
57 static void strip(char *str)
58 {
59         char *p = str;
60         int l;
61
62         while ((isspace(*p)))
63                 p++;
64         l = strlen(p);
65         if (p != str)
66                 memmove(str, p, l + 1);
67         if (!l)
68                 return;
69         p = str + l - 1;
70         while ((isspace(*p)))
71                 *p-- = 0;
72 }
73
74 static void check_stdin(void)
75 {
76         if (!valid_stdin) {
77                 printf(_("aborted!\n\n"));
78                 printf(_("Console input/output is redirected. "));
79                 printf(_("Run 'make oldconfig' to update configuration.\n\n"));
80                 exit(1);
81         }
82 }
83
84 static int conf_askvalue(struct symbol *sym, const char *def)
85 {
86         enum symbol_type type = sym_get_type(sym);
87
88         if (!sym_has_value(sym))
89                 printf(_("(NEW) "));
90
91         line[0] = '\n';
92         line[1] = 0;
93
94         if (!sym_is_changable(sym)) {
95                 printf("%s\n", def);
96                 line[0] = '\n';
97                 line[1] = 0;
98                 return 0;
99         }
100
101         switch (input_mode) {
102         case oldconfig:
103         case silentoldconfig:
104                 if (sym_has_value(sym)) {
105                         printf("%s\n", def);
106                         return 0;
107                 }
108                 check_stdin();
109                 /* fall through */
110         case oldaskconfig:
111                 fflush(stdout);
112                 xfgets(line, 128, stdin);
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                 case silentoldconfig:
303                         if (!is_new) {
304                                 cnt = def;
305                                 printf("%d\n", cnt);
306                                 break;
307                         }
308                         check_stdin();
309                         /* fall through */
310                 case oldaskconfig:
311                         fflush(stdout);
312                         xfgets(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[0] && 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 == listnewconfig ||
370                              input_mode == oldnoconfig) &&
371                             rootEntry != menu) {
372                                 check_conf(menu);
373                                 return;
374                         }
375                         /* fall through */
376                 case P_COMMENT:
377                         prompt = menu_get_prompt(menu);
378                         if (prompt)
379                                 printf("%*c\n%*c %s\n%*c\n",
380                                         indent, '*',
381                                         indent, '*', _(prompt),
382                                         indent, '*');
383                 default:
384                         ;
385                 }
386         }
387
388         if (!sym)
389                 goto conf_childs;
390
391         if (sym_is_choice(sym)) {
392                 conf_choice(menu);
393                 if (sym->curr.tri != mod)
394                         return;
395                 goto conf_childs;
396         }
397
398         switch (sym->type) {
399         case S_INT:
400         case S_HEX:
401         case S_STRING:
402                 conf_string(menu);
403                 break;
404         default:
405                 conf_sym(menu);
406                 break;
407         }
408
409 conf_childs:
410         if (sym)
411                 indent += 2;
412         for (child = menu->list; child; child = child->next)
413                 conf(child);
414         if (sym)
415                 indent -= 2;
416 }
417
418 static void check_conf(struct menu *menu)
419 {
420         struct symbol *sym;
421         struct menu *child;
422
423         if (!menu_is_visible(menu))
424                 return;
425
426         sym = menu->sym;
427         if (sym && !sym_has_value(sym)) {
428                 if (sym_is_changable(sym) ||
429                     (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
430                         if (input_mode == listnewconfig) {
431                                 if (sym->name && !sym_is_choice_value(sym)) {
432                                         printf("%s%s\n", CONFIG_, sym->name);
433                                 }
434                         } else if (input_mode != oldnoconfig) {
435                                 if (!conf_cnt++)
436                                         printf(_("*\n* Restart config...\n*\n"));
437                                 rootEntry = menu_get_parent_menu(menu);
438                                 conf(rootEntry);
439                         }
440                 }
441         }
442
443         for (child = menu->list; child; child = child->next)
444                 check_conf(child);
445 }
446
447 static struct option long_opts[] = {
448         {"oldaskconfig",    no_argument,       NULL, oldaskconfig},
449         {"oldconfig",       no_argument,       NULL, oldconfig},
450         {"silentoldconfig", no_argument,       NULL, silentoldconfig},
451         {"defconfig",       optional_argument, NULL, defconfig},
452         {"savedefconfig",   required_argument, NULL, savedefconfig},
453         {"allnoconfig",     no_argument,       NULL, allnoconfig},
454         {"allyesconfig",    no_argument,       NULL, allyesconfig},
455         {"allmodconfig",    no_argument,       NULL, allmodconfig},
456         {"alldefconfig",    no_argument,       NULL, alldefconfig},
457         {"randconfig",      no_argument,       NULL, randconfig},
458         {"listnewconfig",   no_argument,       NULL, listnewconfig},
459         {"oldnoconfig",     no_argument,       NULL, oldnoconfig},
460         {NULL, 0, NULL, 0}
461 };
462
463 int main(int ac, char **av)
464 {
465         int opt;
466         const char *name;
467         struct stat tmpstat;
468
469         setlocale(LC_ALL, "");
470         bindtextdomain(PACKAGE, LOCALEDIR);
471         textdomain(PACKAGE);
472
473         while ((opt = getopt_long(ac, av, "", long_opts, NULL)) != -1) {
474                 input_mode = (enum input_mode)opt;
475                 switch (opt) {
476                 case silentoldconfig:
477                         sync_kconfig = 1;
478                         break;
479                 case defconfig:
480                 case savedefconfig:
481                         defconfig_file = optarg;
482                         break;
483                 case randconfig:
484                 {
485                         struct timeval now;
486                         unsigned int seed;
487
488                         /*
489                          * Use microseconds derived seed,
490                          * compensate for systems where it may be zero
491                          */
492                         gettimeofday(&now, NULL);
493
494                         seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
495                         srand(seed);
496                         break;
497                 }
498                 case '?':
499                         fprintf(stderr, _("See README for usage info\n"));
500                         exit(1);
501                         break;
502                 }
503         }
504         if (ac == optind) {
505                 printf(_("%s: Kconfig file missing\n"), av[0]);
506                 exit(1);
507         }
508         name = av[optind];
509         conf_parse(name);
510         //zconfdump(stdout);
511         if (sync_kconfig) {
512                 name = conf_get_configname();
513                 if (stat(name, &tmpstat)) {
514                         fprintf(stderr, _("***\n"
515                                 "*** Configuration file \"%s\" not found!\n"
516                                 "***\n"
517                                 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
518                                 "*** \"make menuconfig\" or \"make xconfig\").\n"
519                                 "***\n"), name);
520                         exit(1);
521                 }
522         }
523
524         switch (input_mode) {
525         case defconfig:
526                 if (!defconfig_file)
527                         defconfig_file = conf_get_default_confname();
528                 if (conf_read(defconfig_file)) {
529                         printf(_("***\n"
530                                 "*** Can't find default configuration \"%s\"!\n"
531                                 "***\n"), defconfig_file);
532                         exit(1);
533                 }
534                 break;
535         case savedefconfig:
536         case silentoldconfig:
537         case oldaskconfig:
538         case oldconfig:
539         case listnewconfig:
540         case oldnoconfig:
541                 conf_read(NULL);
542                 break;
543         case allnoconfig:
544         case allyesconfig:
545         case allmodconfig:
546         case alldefconfig:
547         case randconfig:
548                 name = getenv("KCONFIG_ALLCONFIG");
549                 if (name && !stat(name, &tmpstat)) {
550                         conf_read_simple(name, S_DEF_USER);
551                         break;
552                 }
553                 switch (input_mode) {
554                 case allnoconfig:       name = "allno.config"; break;
555                 case allyesconfig:      name = "allyes.config"; break;
556                 case allmodconfig:      name = "allmod.config"; break;
557                 case alldefconfig:      name = "alldef.config"; break;
558                 case randconfig:        name = "allrandom.config"; break;
559                 default: break;
560                 }
561                 if (!stat(name, &tmpstat))
562                         conf_read_simple(name, S_DEF_USER);
563                 else if (!stat("all.config", &tmpstat))
564                         conf_read_simple("all.config", S_DEF_USER);
565                 break;
566         default:
567                 break;
568         }
569
570         if (sync_kconfig) {
571                 if (conf_get_changed()) {
572                         name = getenv("KCONFIG_NOSILENTUPDATE");
573                         if (name && *name) {
574                                 fprintf(stderr,
575                                         _("\n*** The configuration requires explicit update.\n\n"));
576                                 return 1;
577                         }
578                 }
579                 valid_stdin = isatty(0) && isatty(1) && isatty(2);
580         }
581
582         switch (input_mode) {
583         case allnoconfig:
584                 conf_set_all_new_symbols(def_no);
585                 break;
586         case allyesconfig:
587                 conf_set_all_new_symbols(def_yes);
588                 break;
589         case allmodconfig:
590                 conf_set_all_new_symbols(def_mod);
591                 break;
592         case alldefconfig:
593                 conf_set_all_new_symbols(def_default);
594                 break;
595         case randconfig:
596                 conf_set_all_new_symbols(def_random);
597                 break;
598         case defconfig:
599                 conf_set_all_new_symbols(def_default);
600                 break;
601         case savedefconfig:
602                 break;
603         case oldaskconfig:
604                 rootEntry = &rootmenu;
605                 conf(&rootmenu);
606                 input_mode = silentoldconfig;
607                 /* fall through */
608         case oldconfig:
609         case listnewconfig:
610         case oldnoconfig:
611         case silentoldconfig:
612                 /* Update until a loop caused no more changes */
613                 do {
614                         conf_cnt = 0;
615                         check_conf(&rootmenu);
616                 } while (conf_cnt &&
617                          (input_mode != listnewconfig &&
618                           input_mode != oldnoconfig));
619                 break;
620         }
621
622         if (sync_kconfig) {
623                 /* silentoldconfig is used during the build so we shall update autoconf.
624                  * All other commands are only used to generate a config.
625                  */
626                 if (conf_get_changed() && conf_write(NULL)) {
627                         fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
628                         exit(1);
629                 }
630                 if (conf_write_autoconf()) {
631                         fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
632                         return 1;
633                 }
634         } else if (input_mode == savedefconfig) {
635                 if (conf_write_defconfig(defconfig_file)) {
636                         fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
637                                 defconfig_file);
638                         return 1;
639                 }
640         } else if (input_mode != listnewconfig) {
641                 if (conf_write(NULL)) {
642                         fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
643                         exit(1);
644                 }
645         }
646         return 0;
647 }
648 /*
649  * Helper function to facilitate fgets() by Jean Sacren.
650  */
651 void xfgets(str, size, in)
652         char *str;
653         int size;
654         FILE *in;
655 {
656         if (fgets(str, size, in) == NULL)
657                 fprintf(stderr, "\nError in reading or end of file.\n");
658 }