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))
202 if (menu_has_help(menu))
205 if (!conf_askvalue(sym, sym_get_string_value(sym)))
213 if (!line[1] || !strcmp(&line[1], "o"))
225 if (!line[1] || !strcmp(&line[1], "es"))
236 if (sym_set_tristate_value(sym, newval))
243 static int conf_choice(struct menu *menu)
245 struct symbol *sym, *def_sym;
250 is_new = !sym_has_value(sym);
251 if (sym_is_changable(sym)) {
254 switch (sym_get_tristate_value(sym)) {
263 switch (sym_get_tristate_value(sym)) {
267 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
277 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
278 def_sym = sym_get_choice_value(sym);
281 for (child = menu->list; child; child = child->next) {
282 if (!menu_is_visible(child))
285 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
289 if (child->sym == def_sym) {
291 printf("%*c", indent, '>');
293 printf("%*c", indent, ' ');
294 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
295 if (child->sym->name)
296 printf(" (%s)", child->sym->name);
297 if (!sym_has_value(child->sym))
301 printf(_("%*schoice"), indent - 1, "");
306 printf("[1-%d", cnt);
307 if (menu_has_help(menu))
310 switch (input_mode) {
312 case silentoldconfig:
322 xfgets(line, sizeof(line), stdin);
324 if (line[0] == '?') {
330 else if (isdigit(line[0]))
340 for (child = menu->list; child; child = child->next) {
341 if (!child->sym || !menu_is_visible(child))
348 if (line[0] && line[strlen(line) - 1] == '?') {
352 sym_set_choice_value(sym, child->sym);
353 for (child = child->list; child; child = child->next) {
362 static void conf(struct menu *menu)
365 struct property *prop;
368 if (!menu_is_visible(menu))
376 switch (prop->type) {
378 if ((input_mode == silentoldconfig ||
379 input_mode == listnewconfig ||
380 input_mode == olddefconfig) &&
387 prompt = menu_get_prompt(menu);
389 printf("%*c\n%*c %s\n%*c\n",
391 indent, '*', _(prompt),
401 if (sym_is_choice(sym)) {
403 if (sym->curr.tri != mod)
422 for (child = menu->list; child; child = child->next)
428 static void check_conf(struct menu *menu)
433 if (!menu_is_visible(menu))
437 if (sym && !sym_has_value(sym)) {
438 if (sym_is_changable(sym) ||
439 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
440 if (input_mode == listnewconfig) {
441 if (sym->name && !sym_is_choice_value(sym)) {
442 printf("%s%s\n", CONFIG_, sym->name);
444 } else if (input_mode != olddefconfig) {
446 printf(_("*\n* Restart config...\n*\n"));
447 rootEntry = menu_get_parent_menu(menu);
453 for (child = menu->list; child; child = child->next)
457 static struct option long_opts[] = {
458 {"oldaskconfig", no_argument, NULL, oldaskconfig},
459 {"oldconfig", no_argument, NULL, oldconfig},
460 {"silentoldconfig", no_argument, NULL, silentoldconfig},
461 {"defconfig", optional_argument, NULL, defconfig},
462 {"savedefconfig", required_argument, NULL, savedefconfig},
463 {"allnoconfig", no_argument, NULL, allnoconfig},
464 {"allyesconfig", no_argument, NULL, allyesconfig},
465 {"allmodconfig", no_argument, NULL, allmodconfig},
466 {"alldefconfig", no_argument, NULL, alldefconfig},
467 {"randconfig", no_argument, NULL, randconfig},
468 {"listnewconfig", no_argument, NULL, listnewconfig},
469 {"olddefconfig", no_argument, NULL, olddefconfig},
471 * oldnoconfig is an alias of olddefconfig, because people already
472 * are dependent on its behavior(sets new symbols to their default
473 * value but not 'n') with the counter-intuitive name.
475 {"oldnoconfig", no_argument, NULL, olddefconfig},
479 static void conf_usage(const char *progname)
482 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
483 printf("[option] is _one_ of the following:\n");
484 printf(" --listnewconfig List new options\n");
485 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
486 printf(" --oldconfig Update a configuration using a provided .config as base\n");
487 printf(" --silentoldconfig Similar to oldconfig but generates configuration in\n"
488 " include/{generated/,config/} (oldconfig used to be more verbose)\n");
489 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
490 printf(" --oldnoconfig An alias of olddefconfig\n");
491 printf(" --defconfig <file> New config with default defined in <file>\n");
492 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
493 printf(" --allnoconfig New config where all options are answered with no\n");
494 printf(" --allyesconfig New config where all options are answered with yes\n");
495 printf(" --allmodconfig New config where all options are answered with mod\n");
496 printf(" --alldefconfig New config with all symbols set to default\n");
497 printf(" --randconfig New config with random answer to all options\n");
500 int main(int ac, char **av)
502 const char *progname = av[0];
504 const char *name, *defconfig_file = NULL /* gcc uninit */;
507 setlocale(LC_ALL, "");
508 bindtextdomain(PACKAGE, LOCALEDIR);
511 tty_stdio = isatty(0) && isatty(1) && isatty(2);
513 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
515 conf_set_message_callback(NULL);
518 input_mode = (enum input_mode)opt;
520 case silentoldconfig:
525 defconfig_file = optarg;
534 * Use microseconds derived seed,
535 * compensate for systems where it may be zero
537 gettimeofday(&now, NULL);
538 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
540 seed_env = getenv("KCONFIG_SEED");
541 if( seed_env && *seed_env ) {
543 int tmp = (int)strtol(seed_env, &endp, 0);
548 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
562 conf_usage(progname);
568 printf(_("%s: Kconfig file missing\n"), av[0]);
569 conf_usage(progname);
576 name = conf_get_configname();
577 if (stat(name, &tmpstat)) {
578 fprintf(stderr, _("***\n"
579 "*** Configuration file \"%s\" not found!\n"
581 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
582 "*** \"make menuconfig\" or \"make xconfig\").\n"
588 switch (input_mode) {
591 defconfig_file = conf_get_default_confname();
592 if (conf_read(defconfig_file)) {
594 "*** Can't find default configuration \"%s\"!\n"
595 "***\n"), defconfig_file);
600 case silentoldconfig:
612 name = getenv("KCONFIG_ALLCONFIG");
615 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
616 if (conf_read_simple(name, S_DEF_USER)) {
618 _("*** Can't read seed configuration \"%s\"!\n"),
624 switch (input_mode) {
625 case allnoconfig: name = "allno.config"; break;
626 case allyesconfig: name = "allyes.config"; break;
627 case allmodconfig: name = "allmod.config"; break;
628 case alldefconfig: name = "alldef.config"; break;
629 case randconfig: name = "allrandom.config"; break;
632 if (conf_read_simple(name, S_DEF_USER) &&
633 conf_read_simple("all.config", S_DEF_USER)) {
635 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
645 if (conf_get_changed()) {
646 name = getenv("KCONFIG_NOSILENTUPDATE");
649 _("\n*** The configuration requires explicit update.\n\n"));
653 valid_stdin = tty_stdio;
656 switch (input_mode) {
658 conf_set_all_new_symbols(def_no);
661 conf_set_all_new_symbols(def_yes);
664 conf_set_all_new_symbols(def_mod);
667 conf_set_all_new_symbols(def_default);
670 /* Really nothing to do in this loop */
671 while (conf_set_all_new_symbols(def_random)) ;
674 conf_set_all_new_symbols(def_default);
679 rootEntry = &rootmenu;
681 input_mode = silentoldconfig;
686 case silentoldconfig:
687 /* Update until a loop caused no more changes */
690 check_conf(&rootmenu);
692 (input_mode != listnewconfig &&
693 input_mode != olddefconfig));
698 /* silentoldconfig is used during the build so we shall update autoconf.
699 * All other commands are only used to generate a config.
701 if (conf_get_changed() && conf_write(NULL)) {
702 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
705 if (conf_write_autoconf()) {
706 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
709 } else if (input_mode == savedefconfig) {
710 if (conf_write_defconfig(defconfig_file)) {
711 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
715 } else if (input_mode != listnewconfig) {
716 if (conf_write(NULL)) {
717 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));