kconfig: annotate non-trivial fall-trough
[deliverable/linux.git] / scripts / kconfig / conf.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
4 */
5
534a450c 6#include <locale.h>
1da177e4 7#include <ctype.h>
9dfb563b 8#include <stdio.h>
75ff4309 9#include <stdlib.h>
1da177e4 10#include <string.h>
1da177e4 11#include <time.h>
75ff4309 12#include <unistd.h>
4062f1a4 13#include <getopt.h>
1da177e4 14#include <sys/stat.h>
b0fe5510 15#include <sys/time.h>
1da177e4
LT
16
17#define LKC_DIRECT_LINK
18#include "lkc.h"
19
20static void conf(struct menu *menu);
21static void check_conf(struct menu *menu);
22
4062f1a4
SR
23enum input_mode {
24 oldaskconfig,
25 silentoldconfig,
26 oldconfig,
27 allnoconfig,
28 allyesconfig,
29 allmodconfig,
0748cb3e 30 alldefconfig,
4062f1a4
SR
31 randconfig,
32 defconfig,
7cf3d73b 33 savedefconfig,
861b4ea4 34 listnewconfig,
ef61ca88 35 oldnoconfig,
4062f1a4
SR
36} input_mode = oldaskconfig;
37
1da177e4
LT
38char *defconfig_file;
39
40static int indent = 1;
41static int valid_stdin = 1;
204c96f6 42static int sync_kconfig;
1da177e4 43static int conf_cnt;
48b9d03c 44static char line[128];
1da177e4
LT
45static struct menu *rootEntry;
46
66c4bd80 47static void print_help(struct menu *menu)
03d29122 48{
66c4bd80
CR
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);
03d29122
SR
55}
56
48b9d03c 57static void strip(char *str)
1da177e4 58{
48b9d03c 59 char *p = str;
1da177e4
LT
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
74static void check_stdin(void)
75{
204c96f6 76 if (!valid_stdin) {
3b9fa093
ACM
77 printf(_("aborted!\n\n"));
78 printf(_("Console input/output is redirected. "));
79 printf(_("Run 'make oldconfig' to update configuration.\n\n"));
1da177e4
LT
80 exit(1);
81 }
82}
83
f82f3f94 84static int conf_askvalue(struct symbol *sym, const char *def)
1da177e4
LT
85{
86 enum symbol_type type = sym_get_type(sym);
1da177e4
LT
87
88 if (!sym_has_value(sym))
534a450c 89 printf(_("(NEW) "));
1da177e4
LT
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;
f82f3f94 98 return 0;
1da177e4
LT
99 }
100
101 switch (input_mode) {
4062f1a4
SR
102 case oldconfig:
103 case silentoldconfig:
1da177e4
LT
104 if (sym_has_value(sym)) {
105 printf("%s\n", def);
f82f3f94 106 return 0;
1da177e4
LT
107 }
108 check_stdin();
d8fc3200 109 /* fall through */
4062f1a4 110 case oldaskconfig:
1da177e4 111 fflush(stdout);
4418a2b9 112 xfgets(line, 128, stdin);
f82f3f94 113 return 1;
1da177e4
LT
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);
f82f3f94 123 return 1;
1da177e4
LT
124 default:
125 ;
126 }
1da177e4 127 printf("%s", line);
f82f3f94 128 return 1;
1da177e4
LT
129}
130
4356f489 131static int conf_string(struct menu *menu)
1da177e4
LT
132{
133 struct symbol *sym = menu->sym;
03d29122 134 const char *def;
1da177e4
LT
135
136 while (1) {
534a450c 137 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
1da177e4
LT
138 printf("(%s) ", sym->name);
139 def = sym_get_string_value(sym);
140 if (sym_get_string_value(sym))
141 printf("[%s] ", def);
f82f3f94
RZ
142 if (!conf_askvalue(sym, def))
143 return 0;
1da177e4
LT
144 switch (line[0]) {
145 case '\n':
146 break;
147 case '?':
148 /* print help */
149 if (line[1] == '\n') {
66c4bd80 150 print_help(menu);
1da177e4
LT
151 def = NULL;
152 break;
153 }
d8fc3200 154 /* fall through */
1da177e4
LT
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
164static int conf_sym(struct menu *menu)
165{
166 struct symbol *sym = menu->sym;
1da177e4 167 tristate oldval, newval;
1da177e4
LT
168
169 while (1) {
534a450c 170 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
1da177e4
LT
171 if (sym->name)
172 printf("(%s) ", sym->name);
1da177e4
LT
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");
03d29122 192 if (menu_has_help(menu))
1da177e4
LT
193 printf("/?");
194 printf("] ");
f82f3f94
RZ
195 if (!conf_askvalue(sym, sym_get_string_value(sym)))
196 return 0;
1da177e4
LT
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;
228help:
66c4bd80 229 print_help(menu);
1da177e4
LT
230 }
231}
232
233static int conf_choice(struct menu *menu)
234{
235 struct symbol *sym, *def_sym;
236 struct menu *child;
1da177e4
LT
237 bool is_new;
238
239 sym = menu->sym;
1da177e4
LT
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:
534a450c 257 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
1da177e4
LT
258 return 0;
259 case yes:
260 break;
261 }
262 }
263
264 while (1) {
265 int cnt, def;
266
534a450c 267 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
1da177e4
LT
268 def_sym = sym_get_choice_value(sym);
269 cnt = def = 0;
40aee729 270 line[0] = 0;
1da177e4
LT
271 for (child = menu->list; child; child = child->next) {
272 if (!menu_is_visible(child))
273 continue;
274 if (!child->sym) {
534a450c 275 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
1da177e4
LT
276 continue;
277 }
278 cnt++;
279 if (child->sym == def_sym) {
280 def = cnt;
281 printf("%*c", indent, '>');
282 } else
283 printf("%*c", indent, ' ');
534a450c 284 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
1da177e4
LT
285 if (child->sym->name)
286 printf(" (%s)", child->sym->name);
287 if (!sym_has_value(child->sym))
534a450c 288 printf(_(" (NEW)"));
1da177e4
LT
289 printf("\n");
290 }
534a450c 291 printf(_("%*schoice"), indent - 1, "");
1da177e4
LT
292 if (cnt == 1) {
293 printf("[1]: 1\n");
294 goto conf_childs;
295 }
296 printf("[1-%d", cnt);
03d29122 297 if (menu_has_help(menu))
1da177e4
LT
298 printf("?");
299 printf("]: ");
300 switch (input_mode) {
4062f1a4
SR
301 case oldconfig:
302 case silentoldconfig:
1da177e4
LT
303 if (!is_new) {
304 cnt = def;
305 printf("%d\n", cnt);
306 break;
307 }
308 check_stdin();
d8fc3200 309 /* fall through */
4062f1a4 310 case oldaskconfig:
1da177e4 311 fflush(stdout);
4418a2b9 312 xfgets(line, 128, stdin);
1da177e4
LT
313 strip(line);
314 if (line[0] == '?') {
66c4bd80 315 print_help(menu);
1da177e4
LT
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;
f443d2ec
SR
325 default:
326 break;
1da177e4
LT
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;
3ba41621 338 if (line[0] && line[strlen(line) - 1] == '?') {
66c4bd80 339 print_help(child);
1da177e4
LT
340 continue;
341 }
342 sym_set_choice_value(sym, child->sym);
f5eaa323 343 for (child = child->list; child; child = child->next) {
1da177e4 344 indent += 2;
f5eaa323 345 conf(child);
1da177e4
LT
346 indent -= 2;
347 }
348 return 1;
349 }
350}
351
352static 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:
4062f1a4 368 if ((input_mode == silentoldconfig ||
861b4ea4 369 input_mode == listnewconfig ||
ef61ca88 370 input_mode == oldnoconfig) &&
f0778c8c 371 rootEntry != menu) {
1da177e4
LT
372 check_conf(menu);
373 return;
374 }
d8fc3200 375 /* fall through */
1da177e4
LT
376 case P_COMMENT:
377 prompt = menu_get_prompt(menu);
378 if (prompt)
379 printf("%*c\n%*c %s\n%*c\n",
380 indent, '*',
534a450c 381 indent, '*', _(prompt),
1da177e4
LT
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
409conf_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
418static 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;
3f23ca2b
RZ
427 if (sym && !sym_has_value(sym)) {
428 if (sym_is_changable(sym) ||
429 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
861b4ea4
SR
430 if (input_mode == listnewconfig) {
431 if (sym->name && !sym_is_choice_value(sym)) {
ffb5957b 432 printf("%s%s\n", CONFIG_, sym->name);
f0778c8c 433 }
71d80662 434 } else if (input_mode != oldnoconfig) {
f0778c8c
AR
435 if (!conf_cnt++)
436 printf(_("*\n* Restart config...\n*\n"));
437 rootEntry = menu_get_parent_menu(menu);
438 conf(rootEntry);
439 }
1da177e4 440 }
1da177e4
LT
441 }
442
443 for (child = menu->list; child; child = child->next)
444 check_conf(child);
445}
446
4062f1a4
SR
447static 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},
7cf3d73b 452 {"savedefconfig", required_argument, NULL, savedefconfig},
4062f1a4
SR
453 {"allnoconfig", no_argument, NULL, allnoconfig},
454 {"allyesconfig", no_argument, NULL, allyesconfig},
455 {"allmodconfig", no_argument, NULL, allmodconfig},
0748cb3e 456 {"alldefconfig", no_argument, NULL, alldefconfig},
4062f1a4 457 {"randconfig", no_argument, NULL, randconfig},
861b4ea4 458 {"listnewconfig", no_argument, NULL, listnewconfig},
ef61ca88 459 {"oldnoconfig", no_argument, NULL, oldnoconfig},
4062f1a4
SR
460 {NULL, 0, NULL, 0}
461};
462
1da177e4
LT
463int main(int ac, char **av)
464{
2f4b489b 465 int opt;
1da177e4
LT
466 const char *name;
467 struct stat tmpstat;
468
534a450c
EG
469 setlocale(LC_ALL, "");
470 bindtextdomain(PACKAGE, LOCALEDIR);
471 textdomain(PACKAGE);
472
c94d3fb0 473 while ((opt = getopt_long(ac, av, "", long_opts, NULL)) != -1) {
4062f1a4 474 input_mode = (enum input_mode)opt;
2f4b489b 475 switch (opt) {
4062f1a4 476 case silentoldconfig:
204c96f6 477 sync_kconfig = 1;
1da177e4 478 break;
4062f1a4 479 case defconfig:
7cf3d73b 480 case savedefconfig:
2f4b489b 481 defconfig_file = optarg;
1da177e4 482 break;
4062f1a4 483 case randconfig:
b0fe5510
IM
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);
1da177e4 496 break;
b0fe5510 497 }
4062f1a4 498 case '?':
534a450c 499 fprintf(stderr, _("See README for usage info\n"));
2f4b489b 500 exit(1);
4062f1a4 501 break;
1da177e4
LT
502 }
503 }
2f4b489b 504 if (ac == optind) {
3b9fa093 505 printf(_("%s: Kconfig file missing\n"), av[0]);
250725aa 506 exit(1);
1da177e4 507 }
2f4b489b 508 name = av[optind];
1da177e4
LT
509 conf_parse(name);
510 //zconfdump(stdout);
204c96f6 511 if (sync_kconfig) {
284026cd
MH
512 name = conf_get_configname();
513 if (stat(name, &tmpstat)) {
204c96f6 514 fprintf(stderr, _("***\n"
652cf982 515 "*** Configuration file \"%s\" not found!\n"
204c96f6 516 "***\n"
517 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
518 "*** \"make menuconfig\" or \"make xconfig\").\n"
284026cd 519 "***\n"), name);
204c96f6 520 exit(1);
521 }
522 }
523
1da177e4 524 switch (input_mode) {
4062f1a4 525 case defconfig:
1da177e4
LT
526 if (!defconfig_file)
527 defconfig_file = conf_get_default_confname();
528 if (conf_read(defconfig_file)) {
534a450c 529 printf(_("***\n"
1da177e4 530 "*** Can't find default configuration \"%s\"!\n"
534a450c 531 "***\n"), defconfig_file);
1da177e4
LT
532 exit(1);
533 }
534 break;
7cf3d73b 535 case savedefconfig:
4062f1a4
SR
536 case silentoldconfig:
537 case oldaskconfig:
538 case oldconfig:
861b4ea4 539 case listnewconfig:
ef61ca88 540 case oldnoconfig:
1da177e4
LT
541 conf_read(NULL);
542 break;
4062f1a4
SR
543 case allnoconfig:
544 case allyesconfig:
545 case allmodconfig:
0748cb3e 546 case alldefconfig:
4062f1a4 547 case randconfig:
90389160
RZ
548 name = getenv("KCONFIG_ALLCONFIG");
549 if (name && !stat(name, &tmpstat)) {
669bfad9 550 conf_read_simple(name, S_DEF_USER);
90389160
RZ
551 break;
552 }
553 switch (input_mode) {
4062f1a4
SR
554 case allnoconfig: name = "allno.config"; break;
555 case allyesconfig: name = "allyes.config"; break;
556 case allmodconfig: name = "allmod.config"; break;
0748cb3e 557 case alldefconfig: name = "alldef.config"; break;
4062f1a4 558 case randconfig: name = "allrandom.config"; break;
90389160
RZ
559 default: break;
560 }
561 if (!stat(name, &tmpstat))
669bfad9 562 conf_read_simple(name, S_DEF_USER);
90389160 563 else if (!stat("all.config", &tmpstat))
669bfad9 564 conf_read_simple("all.config", S_DEF_USER);
90389160 565 break;
1da177e4
LT
566 default:
567 break;
568 }
204c96f6 569
570 if (sync_kconfig) {
571 if (conf_get_changed()) {
572 name = getenv("KCONFIG_NOSILENTUPDATE");
573 if (name && *name) {
574 fprintf(stderr,
652cf982 575 _("\n*** The configuration requires explicit update.\n\n"));
204c96f6 576 return 1;
577 }
578 }
579 valid_stdin = isatty(0) && isatty(1) && isatty(2);
580 }
581
f443d2ec 582 switch (input_mode) {
4062f1a4 583 case allnoconfig:
f443d2ec
SR
584 conf_set_all_new_symbols(def_no);
585 break;
4062f1a4 586 case allyesconfig:
f443d2ec
SR
587 conf_set_all_new_symbols(def_yes);
588 break;
4062f1a4 589 case allmodconfig:
f443d2ec
SR
590 conf_set_all_new_symbols(def_mod);
591 break;
0748cb3e
SR
592 case alldefconfig:
593 conf_set_all_new_symbols(def_default);
594 break;
4062f1a4 595 case randconfig:
f443d2ec
SR
596 conf_set_all_new_symbols(def_random);
597 break;
4062f1a4 598 case defconfig:
09748e17
SR
599 conf_set_all_new_symbols(def_default);
600 break;
7cf3d73b
SR
601 case savedefconfig:
602 break;
4062f1a4 603 case oldaskconfig:
204c96f6 604 rootEntry = &rootmenu;
605 conf(&rootmenu);
4062f1a4 606 input_mode = silentoldconfig;
204c96f6 607 /* fall through */
14828349 608 case oldconfig:
861b4ea4 609 case listnewconfig:
ef61ca88 610 case oldnoconfig:
4062f1a4 611 case silentoldconfig:
204c96f6 612 /* Update until a loop caused no more changes */
613 do {
614 conf_cnt = 0;
615 check_conf(&rootmenu);
f0778c8c 616 } while (conf_cnt &&
861b4ea4 617 (input_mode != listnewconfig &&
ef61ca88 618 input_mode != oldnoconfig));
f443d2ec
SR
619 break;
620 }
1da177e4 621
204c96f6 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)) {
652cf982 627 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
204c96f6 628 exit(1);
629 }
630 if (conf_write_autoconf()) {
652cf982 631 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
204c96f6 632 return 1;
633 }
7cf3d73b
SR
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 }
861b4ea4 640 } else if (input_mode != listnewconfig) {
204c96f6 641 if (conf_write(NULL)) {
652cf982 642 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
204c96f6 643 exit(1);
644 }
c955ccaf 645 }
861b4ea4 646 return 0;
1da177e4 647}
4418a2b9
JS
648/*
649 * Helper function to facilitate fgets() by Jean Sacren.
650 */
651void 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}
This page took 0.782729 seconds and 5 git commands to generate.