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