2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
21 static void conf(struct menu *menu);
22 static void check_conf(struct menu *menu);
38 static enum input_mode input_mode = oldaskconfig;
40 static int indent = 1;
42 static int valid_stdin = 1;
43 static int sync_kconfig;
45 static char line[PATH_MAX];
46 static struct menu *rootEntry;
48 static void print_help(struct menu *menu)
50 struct gstr help = str_new();
52 menu_get_ext_help(menu, &help);
54 printf("\n%s\n", str_get(&help));
58 static void strip(char *str)
67 memmove(str, p, l + 1);
75 static void check_stdin(void)
78 printf(_("aborted!\n\n"));
79 printf(_("Console input/output is redirected. "));
80 printf(_("Run 'make oldconfig' to update configuration.\n\n"));
85 /* Helper function to facilitate fgets() by Jean Sacren. */
86 static void xfgets(char *str, int size, FILE *in)
88 if (!fgets(str, size, in))
89 fprintf(stderr, "\nError in reading or end of file.\n");
92 static int conf_askvalue(struct symbol *sym, const char *def)
94 enum symbol_type type = sym_get_type(sym);
96 if (!sym_has_value(sym))
102 if (!sym_is_changable(sym)) {
109 switch (input_mode) {
111 case silentoldconfig:
112 if (sym_has_value(sym)) {
120 xfgets(line, sizeof(line), stdin);
141 static int conf_string(struct menu *menu)
143 struct symbol *sym = menu->sym;
147 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
148 printf("(%s) ", sym->name);
149 def = sym_get_string_value(sym);
150 if (sym_get_string_value(sym))
151 printf("[%s] ", def);
152 if (!conf_askvalue(sym, def))
159 if (line[1] == '\n') {
166 line[strlen(line)-1] = 0;
169 if (def && sym_set_string_value(sym, def))
174 static int conf_sym(struct menu *menu)
176 struct symbol *sym = menu->sym;
177 tristate oldval, newval;
180 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
182 printf("(%s) ", sym->name);
184 oldval = sym_get_tristate_value(sym);
196 if (oldval != no && sym_tristate_within_range(sym, no))
198 if (oldval != mod && sym_tristate_within_range(sym, mod))
200 if (oldval != yes && sym_tristate_within_range(sym, yes))
203 if (!conf_askvalue(sym, sym_get_string_value(sym)))
211 if (!line[1] || !strcmp(&line[1], "o"))
223 if (!line[1] || !strcmp(&line[1], "es"))
234 if (sym_set_tristate_value(sym, newval))
241 static int conf_choice(struct menu *menu)
243 struct symbol *sym, *def_sym;
248 is_new = !sym_has_value(sym);
249 if (sym_is_changable(sym)) {
252 switch (sym_get_tristate_value(sym)) {
261 switch (sym_get_tristate_value(sym)) {
265 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
275 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
276 def_sym = sym_get_choice_value(sym);
279 for (child = menu->list; child; child = child->next) {
280 if (!menu_is_visible(child))
283 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
287 if (child->sym == def_sym) {
289 printf("%*c", indent, '>');
291 printf("%*c", indent, ' ');
292 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
293 if (child->sym->name)
294 printf(" (%s)", child->sym->name);
295 if (!sym_has_value(child->sym))
299 printf(_("%*schoice"), indent - 1, "");
304 printf("[1-%d?]: ", cnt);
305 switch (input_mode) {
307 case silentoldconfig:
317 xfgets(line, sizeof(line), stdin);
319 if (line[0] == '?') {
325 else if (isdigit(line[0]))
335 for (child = menu->list; child; child = child->next) {
336 if (!child->sym || !menu_is_visible(child))
343 if (line[0] && line[strlen(line) - 1] == '?') {
347 sym_set_choice_value(sym, child->sym);
348 for (child = child->list; child; child = child->next) {
357 static void conf(struct menu *menu)
360 struct property *prop;
363 if (!menu_is_visible(menu))
371 switch (prop->type) {
373 if ((input_mode == silentoldconfig ||
374 input_mode == listnewconfig ||
375 input_mode == olddefconfig) &&
382 prompt = menu_get_prompt(menu);
384 printf("%*c\n%*c %s\n%*c\n",
386 indent, '*', _(prompt),
396 if (sym_is_choice(sym)) {
398 if (sym->curr.tri != mod)
417 for (child = menu->list; child; child = child->next)
423 static void check_conf(struct menu *menu)
428 if (!menu_is_visible(menu))
432 if (sym && !sym_has_value(sym)) {
433 if (sym_is_changable(sym) ||
434 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
435 if (input_mode == listnewconfig) {
436 if (sym->name && !sym_is_choice_value(sym)) {
437 printf("%s%s\n", CONFIG_, sym->name);
439 } else if (input_mode != olddefconfig) {
441 printf(_("*\n* Restart config...\n*\n"));
442 rootEntry = menu_get_parent_menu(menu);
448 for (child = menu->list; child; child = child->next)
452 static struct option long_opts[] = {
453 {"oldaskconfig", no_argument, NULL, oldaskconfig},
454 {"oldconfig", no_argument, NULL, oldconfig},
455 {"silentoldconfig", no_argument, NULL, silentoldconfig},
456 {"defconfig", optional_argument, NULL, defconfig},
457 {"savedefconfig", required_argument, NULL, savedefconfig},
458 {"allnoconfig", no_argument, NULL, allnoconfig},
459 {"allyesconfig", no_argument, NULL, allyesconfig},
460 {"allmodconfig", no_argument, NULL, allmodconfig},
461 {"alldefconfig", no_argument, NULL, alldefconfig},
462 {"randconfig", no_argument, NULL, randconfig},
463 {"listnewconfig", no_argument, NULL, listnewconfig},
464 {"olddefconfig", no_argument, NULL, olddefconfig},
466 * oldnoconfig is an alias of olddefconfig, because people already
467 * are dependent on its behavior(sets new symbols to their default
468 * value but not 'n') with the counter-intuitive name.
470 {"oldnoconfig", no_argument, NULL, olddefconfig},
474 static void conf_usage(const char *progname)
477 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
478 printf("[option] is _one_ of the following:\n");
479 printf(" --listnewconfig List new options\n");
480 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
481 printf(" --oldconfig Update a configuration using a provided .config as base\n");
482 printf(" --silentoldconfig Similar to oldconfig but generates configuration in\n"
483 " include/{generated/,config/} (oldconfig used to be more verbose)\n");
484 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
485 printf(" --oldnoconfig An alias of olddefconfig\n");
486 printf(" --defconfig <file> New config with default defined in <file>\n");
487 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
488 printf(" --allnoconfig New config where all options are answered with no\n");
489 printf(" --allyesconfig New config where all options are answered with yes\n");
490 printf(" --allmodconfig New config where all options are answered with mod\n");
491 printf(" --alldefconfig New config with all symbols set to default\n");
492 printf(" --randconfig New config with random answer to all options\n");
495 int main(int ac, char **av)
497 const char *progname = av[0];
499 const char *name, *defconfig_file = NULL /* gcc uninit */;
502 setlocale(LC_ALL, "");
503 bindtextdomain(PACKAGE, LOCALEDIR);
506 tty_stdio = isatty(0) && isatty(1) && isatty(2);
508 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
510 conf_set_message_callback(NULL);
513 input_mode = (enum input_mode)opt;
515 case silentoldconfig:
520 defconfig_file = optarg;
529 * Use microseconds derived seed,
530 * compensate for systems where it may be zero
532 gettimeofday(&now, NULL);
533 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
535 seed_env = getenv("KCONFIG_SEED");
536 if( seed_env && *seed_env ) {
538 int tmp = (int)strtol(seed_env, &endp, 0);
543 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
557 conf_usage(progname);
563 printf(_("%s: Kconfig file missing\n"), av[0]);
564 conf_usage(progname);
571 name = conf_get_configname();
572 if (stat(name, &tmpstat)) {
573 fprintf(stderr, _("***\n"
574 "*** Configuration file \"%s\" not found!\n"
576 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
577 "*** \"make menuconfig\" or \"make xconfig\").\n"
583 switch (input_mode) {
586 defconfig_file = conf_get_default_confname();
587 if (conf_read(defconfig_file)) {
589 "*** Can't find default configuration \"%s\"!\n"
590 "***\n"), defconfig_file);
595 case silentoldconfig:
607 name = getenv("KCONFIG_ALLCONFIG");
610 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
611 if (conf_read_simple(name, S_DEF_USER)) {
613 _("*** Can't read seed configuration \"%s\"!\n"),
619 switch (input_mode) {
620 case allnoconfig: name = "allno.config"; break;
621 case allyesconfig: name = "allyes.config"; break;
622 case allmodconfig: name = "allmod.config"; break;
623 case alldefconfig: name = "alldef.config"; break;
624 case randconfig: name = "allrandom.config"; break;
627 if (conf_read_simple(name, S_DEF_USER) &&
628 conf_read_simple("all.config", S_DEF_USER)) {
630 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
640 if (conf_get_changed()) {
641 name = getenv("KCONFIG_NOSILENTUPDATE");
644 _("\n*** The configuration requires explicit update.\n\n"));
648 valid_stdin = tty_stdio;
651 switch (input_mode) {
653 conf_set_all_new_symbols(def_no);
656 conf_set_all_new_symbols(def_yes);
659 conf_set_all_new_symbols(def_mod);
662 conf_set_all_new_symbols(def_default);
665 /* Really nothing to do in this loop */
666 while (conf_set_all_new_symbols(def_random)) ;
669 conf_set_all_new_symbols(def_default);
674 rootEntry = &rootmenu;
676 input_mode = silentoldconfig;
681 case silentoldconfig:
682 /* Update until a loop caused no more changes */
685 check_conf(&rootmenu);
687 (input_mode != listnewconfig &&
688 input_mode != olddefconfig));
693 /* silentoldconfig is used during the build so we shall update autoconf.
694 * All other commands are only used to generate a config.
696 if (conf_get_changed() && conf_write(NULL)) {
697 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
700 if (conf_write_autoconf()) {
701 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
704 } else if (input_mode == savedefconfig) {
705 if (conf_write_defconfig(defconfig_file)) {
706 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
710 } else if (input_mode != listnewconfig) {
711 if (conf_write(NULL)) {
712 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));