kconfig: use long options in conf
[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
f0778c8c
AR
20/* Return codes */
21#define EUNSETOPT 2 /* if -B and -b are used and unset config
22 * options were found */
23
1da177e4
LT
24static void conf(struct menu *menu);
25static void check_conf(struct menu *menu);
26
4062f1a4
SR
27enum input_mode {
28 oldaskconfig,
29 silentoldconfig,
30 oldconfig,
31 allnoconfig,
32 allyesconfig,
33 allmodconfig,
34 randconfig,
35 defconfig,
36 nonint_oldconfig,
37 loose_nonint_oldconfig,
38} input_mode = oldaskconfig;
39
1da177e4
LT
40char *defconfig_file;
41
42static int indent = 1;
43static int valid_stdin = 1;
204c96f6 44static int sync_kconfig;
1da177e4 45static int conf_cnt;
48b9d03c 46static char line[128];
1da177e4 47static struct menu *rootEntry;
f0778c8c 48static int unset_variables;
1da177e4 49
66c4bd80 50static void print_help(struct menu *menu)
03d29122 51{
66c4bd80
CR
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);
03d29122
SR
58}
59
48b9d03c 60static void strip(char *str)
1da177e4 61{
48b9d03c 62 char *p = str;
1da177e4
LT
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
77static void check_stdin(void)
78{
204c96f6 79 if (!valid_stdin) {
3b9fa093
ACM
80 printf(_("aborted!\n\n"));
81 printf(_("Console input/output is redirected. "));
82 printf(_("Run 'make oldconfig' to update configuration.\n\n"));
1da177e4
LT
83 exit(1);
84 }
85}
86
f82f3f94 87static int conf_askvalue(struct symbol *sym, const char *def)
1da177e4
LT
88{
89 enum symbol_type type = sym_get_type(sym);
1da177e4
LT
90
91 if (!sym_has_value(sym))
534a450c 92 printf(_("(NEW) "));
1da177e4
LT
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;
f82f3f94 101 return 0;
1da177e4
LT
102 }
103
104 switch (input_mode) {
4062f1a4
SR
105 case oldconfig:
106 case silentoldconfig:
1da177e4
LT
107 if (sym_has_value(sym)) {
108 printf("%s\n", def);
f82f3f94 109 return 0;
1da177e4
LT
110 }
111 check_stdin();
4062f1a4 112 case oldaskconfig:
1da177e4 113 fflush(stdout);
59c6a3f4 114 fgets(line, 128, stdin);
f82f3f94 115 return 1;
1da177e4
LT
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);
f82f3f94 125 return 1;
1da177e4
LT
126 default:
127 ;
128 }
1da177e4 129 printf("%s", line);
f82f3f94 130 return 1;
1da177e4
LT
131}
132
4356f489 133static int conf_string(struct menu *menu)
1da177e4
LT
134{
135 struct symbol *sym = menu->sym;
03d29122 136 const char *def;
1da177e4
LT
137
138 while (1) {
534a450c 139 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
1da177e4
LT
140 printf("(%s) ", sym->name);
141 def = sym_get_string_value(sym);
142 if (sym_get_string_value(sym))
143 printf("[%s] ", def);
f82f3f94
RZ
144 if (!conf_askvalue(sym, def))
145 return 0;
1da177e4
LT
146 switch (line[0]) {
147 case '\n':
148 break;
149 case '?':
150 /* print help */
151 if (line[1] == '\n') {
66c4bd80 152 print_help(menu);
1da177e4
LT
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
165static int conf_sym(struct menu *menu)
166{
167 struct symbol *sym = menu->sym;
1da177e4 168 tristate oldval, newval;
1da177e4
LT
169
170 while (1) {
534a450c 171 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
1da177e4
LT
172 if (sym->name)
173 printf("(%s) ", sym->name);
1da177e4
LT
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");
03d29122 193 if (menu_has_help(menu))
1da177e4
LT
194 printf("/?");
195 printf("] ");
f82f3f94
RZ
196 if (!conf_askvalue(sym, sym_get_string_value(sym)))
197 return 0;
1da177e4
LT
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;
229help:
66c4bd80 230 print_help(menu);
1da177e4
LT
231 }
232}
233
234static int conf_choice(struct menu *menu)
235{
236 struct symbol *sym, *def_sym;
237 struct menu *child;
1da177e4
LT
238 bool is_new;
239
240 sym = menu->sym;
1da177e4
LT
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:
534a450c 258 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
1da177e4
LT
259 return 0;
260 case yes:
261 break;
262 }
263 }
264
265 while (1) {
266 int cnt, def;
267
534a450c 268 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
1da177e4
LT
269 def_sym = sym_get_choice_value(sym);
270 cnt = def = 0;
40aee729 271 line[0] = 0;
1da177e4
LT
272 for (child = menu->list; child; child = child->next) {
273 if (!menu_is_visible(child))
274 continue;
275 if (!child->sym) {
534a450c 276 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
1da177e4
LT
277 continue;
278 }
279 cnt++;
280 if (child->sym == def_sym) {
281 def = cnt;
282 printf("%*c", indent, '>');
283 } else
284 printf("%*c", indent, ' ');
534a450c 285 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
1da177e4
LT
286 if (child->sym->name)
287 printf(" (%s)", child->sym->name);
288 if (!sym_has_value(child->sym))
534a450c 289 printf(_(" (NEW)"));
1da177e4
LT
290 printf("\n");
291 }
534a450c 292 printf(_("%*schoice"), indent - 1, "");
1da177e4
LT
293 if (cnt == 1) {
294 printf("[1]: 1\n");
295 goto conf_childs;
296 }
297 printf("[1-%d", cnt);
03d29122 298 if (menu_has_help(menu))
1da177e4
LT
299 printf("?");
300 printf("]: ");
301 switch (input_mode) {
4062f1a4
SR
302 case oldconfig:
303 case silentoldconfig:
1da177e4
LT
304 if (!is_new) {
305 cnt = def;
306 printf("%d\n", cnt);
307 break;
308 }
309 check_stdin();
4062f1a4 310 case oldaskconfig:
1da177e4 311 fflush(stdout);
59c6a3f4 312 fgets(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;
338 if (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
SR
368 if ((input_mode == silentoldconfig ||
369 input_mode == nonint_oldconfig ||
370 input_mode == loose_nonint_oldconfig) &&
f0778c8c 371 rootEntry != menu) {
1da177e4
LT
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, '*',
534a450c 380 indent, '*', _(prompt),
1da177e4
LT
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
408conf_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
417static 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;
3f23ca2b
RZ
426 if (sym && !sym_has_value(sym)) {
427 if (sym_is_changable(sym) ||
428 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
4062f1a4
SR
429 if (input_mode == nonint_oldconfig ||
430 input_mode == loose_nonint_oldconfig) {
431 if (input_mode == nonint_oldconfig &&
f0778c8c
AR
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 }
1da177e4 446 }
1da177e4
LT
447 }
448
449 for (child = menu->list; child; child = child->next)
450 check_conf(child);
451}
452
4062f1a4
SR
453static 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 {"loose_nonint_oldconfig", no_argument, NULL, loose_nonint_oldconfig},
464 {NULL, 0, NULL, 0}
465};
466
1da177e4
LT
467int main(int ac, char **av)
468{
2f4b489b 469 int opt;
1da177e4
LT
470 const char *name;
471 struct stat tmpstat;
472
534a450c
EG
473 setlocale(LC_ALL, "");
474 bindtextdomain(PACKAGE, LOCALEDIR);
475 textdomain(PACKAGE);
476
4062f1a4
SR
477 while ((opt = getopt_long_only(ac, av, "", long_opts, NULL)) != -1) {
478 input_mode = (enum input_mode)opt;
2f4b489b 479 switch (opt) {
4062f1a4 480 case silentoldconfig:
204c96f6 481 sync_kconfig = 1;
1da177e4 482 break;
4062f1a4 483 case defconfig:
2f4b489b 484 defconfig_file = optarg;
1da177e4 485 break;
4062f1a4 486 case randconfig:
b0fe5510
IM
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);
1da177e4 499 break;
b0fe5510 500 }
4062f1a4 501 case '?':
534a450c 502 fprintf(stderr, _("See README for usage info\n"));
2f4b489b 503 exit(1);
4062f1a4 504 break;
1da177e4
LT
505 }
506 }
2f4b489b 507 if (ac == optind) {
3b9fa093 508 printf(_("%s: Kconfig file missing\n"), av[0]);
250725aa 509 exit(1);
1da177e4 510 }
2f4b489b 511 name = av[optind];
1da177e4
LT
512 conf_parse(name);
513 //zconfdump(stdout);
204c96f6 514 if (sync_kconfig) {
284026cd
MH
515 name = conf_get_configname();
516 if (stat(name, &tmpstat)) {
204c96f6 517 fprintf(stderr, _("***\n"
518 "*** You have not yet configured your kernel!\n"
284026cd 519 "*** (missing kernel config file \"%s\")\n"
204c96f6 520 "***\n"
521 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
522 "*** \"make menuconfig\" or \"make xconfig\").\n"
284026cd 523 "***\n"), name);
204c96f6 524 exit(1);
525 }
526 }
527
1da177e4 528 switch (input_mode) {
4062f1a4 529 case defconfig:
1da177e4
LT
530 if (!defconfig_file)
531 defconfig_file = conf_get_default_confname();
532 if (conf_read(defconfig_file)) {
534a450c 533 printf(_("***\n"
1da177e4 534 "*** Can't find default configuration \"%s\"!\n"
534a450c 535 "***\n"), defconfig_file);
1da177e4
LT
536 exit(1);
537 }
538 break;
4062f1a4
SR
539 case silentoldconfig:
540 case oldaskconfig:
541 case oldconfig:
542 case nonint_oldconfig:
543 case loose_nonint_oldconfig:
1da177e4
LT
544 conf_read(NULL);
545 break;
4062f1a4
SR
546 case allnoconfig:
547 case allyesconfig:
548 case allmodconfig:
549 case randconfig:
90389160
RZ
550 name = getenv("KCONFIG_ALLCONFIG");
551 if (name && !stat(name, &tmpstat)) {
669bfad9 552 conf_read_simple(name, S_DEF_USER);
90389160
RZ
553 break;
554 }
555 switch (input_mode) {
4062f1a4
SR
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;
90389160
RZ
560 default: break;
561 }
562 if (!stat(name, &tmpstat))
669bfad9 563 conf_read_simple(name, S_DEF_USER);
90389160 564 else if (!stat("all.config", &tmpstat))
669bfad9 565 conf_read_simple("all.config", S_DEF_USER);
90389160 566 break;
1da177e4
LT
567 default:
568 break;
569 }
204c96f6 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
f443d2ec 583 switch (input_mode) {
4062f1a4 584 case allnoconfig:
f443d2ec
SR
585 conf_set_all_new_symbols(def_no);
586 break;
4062f1a4 587 case allyesconfig:
f443d2ec
SR
588 conf_set_all_new_symbols(def_yes);
589 break;
4062f1a4 590 case allmodconfig:
f443d2ec
SR
591 conf_set_all_new_symbols(def_mod);
592 break;
4062f1a4 593 case randconfig:
f443d2ec
SR
594 conf_set_all_new_symbols(def_random);
595 break;
4062f1a4 596 case defconfig:
09748e17
SR
597 conf_set_all_new_symbols(def_default);
598 break;
4062f1a4
SR
599 case oldconfig:
600 case oldaskconfig:
204c96f6 601 rootEntry = &rootmenu;
602 conf(&rootmenu);
4062f1a4 603 input_mode = silentoldconfig;
204c96f6 604 /* fall through */
4062f1a4
SR
605 case nonint_oldconfig:
606 case loose_nonint_oldconfig:
607 case silentoldconfig:
204c96f6 608 /* Update until a loop caused no more changes */
609 do {
610 conf_cnt = 0;
611 check_conf(&rootmenu);
f0778c8c 612 } while (conf_cnt &&
4062f1a4
SR
613 (input_mode != nonint_oldconfig &&
614 input_mode != loose_nonint_oldconfig));
f443d2ec
SR
615 break;
616 }
1da177e4 617
204c96f6 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 }
4062f1a4 630 } else if (!unset_variables || input_mode != nonint_oldconfig) {
204c96f6 631 if (conf_write(NULL)) {
632 fprintf(stderr, _("\n*** Error during writing of the kernel configuration.\n\n"));
633 exit(1);
634 }
c955ccaf 635 }
f0778c8c 636 return unset_variables ? EUNSETOPT : 0;
1da177e4 637}
This page took 0.433378 seconds and 5 git commands to generate.