2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
20 static void conf(struct menu *menu);
21 static void check_conf(struct menu *menu);
22 static void xfgets(char *str, int size, FILE *in);
37 } input_mode = oldaskconfig;
39 static int indent = 1;
41 static int valid_stdin = 1;
42 static int sync_kconfig;
44 static char line[128];
45 static struct menu *rootEntry;
47 static void print_help(struct menu *menu)
49 struct gstr help = str_new();
51 menu_get_ext_help(menu, &help);
53 printf("\n%s\n", str_get(&help));
57 static void strip(char *str)
66 memmove(str, p, l + 1);
74 static void check_stdin(void)
77 printf(_("aborted!\n\n"));
78 printf(_("Console input/output is redirected. "));
79 printf(_("Run 'make oldconfig' to update configuration.\n\n"));
84 static int conf_askvalue(struct symbol *sym, const char *def)
86 enum symbol_type type = sym_get_type(sym);
88 if (!sym_has_value(sym))
94 if (!sym_is_changable(sym)) {
101 switch (input_mode) {
103 case silentoldconfig:
104 if (sym_has_value(sym)) {
112 xfgets(line, 128, stdin);
133 static int conf_string(struct menu *menu)
135 struct symbol *sym = menu->sym;
139 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
140 printf("(%s) ", sym->name);
141 def = sym_get_string_value(sym);
142 if (sym_get_string_value(sym))
143 printf("[%s] ", def);
144 if (!conf_askvalue(sym, def))
151 if (line[1] == '\n') {
158 line[strlen(line)-1] = 0;
161 if (def && sym_set_string_value(sym, def))
166 static int conf_sym(struct menu *menu)
168 struct symbol *sym = menu->sym;
169 tristate oldval, newval;
172 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
174 printf("(%s) ", sym->name);
176 oldval = sym_get_tristate_value(sym);
188 if (oldval != no && sym_tristate_within_range(sym, no))
190 if (oldval != mod && sym_tristate_within_range(sym, mod))
192 if (oldval != yes && sym_tristate_within_range(sym, yes))
194 if (menu_has_help(menu))
197 if (!conf_askvalue(sym, sym_get_string_value(sym)))
205 if (!line[1] || !strcmp(&line[1], "o"))
217 if (!line[1] || !strcmp(&line[1], "es"))
228 if (sym_set_tristate_value(sym, newval))
235 static int conf_choice(struct menu *menu)
237 struct symbol *sym, *def_sym;
242 is_new = !sym_has_value(sym);
243 if (sym_is_changable(sym)) {
246 switch (sym_get_tristate_value(sym)) {
255 switch (sym_get_tristate_value(sym)) {
259 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
269 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
270 def_sym = sym_get_choice_value(sym);
273 for (child = menu->list; child; child = child->next) {
274 if (!menu_is_visible(child))
277 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
281 if (child->sym == def_sym) {
283 printf("%*c", indent, '>');
285 printf("%*c", indent, ' ');
286 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
287 if (child->sym->name)
288 printf(" (%s)", child->sym->name);
289 if (!sym_has_value(child->sym))
293 printf(_("%*schoice"), indent - 1, "");
298 printf("[1-%d", cnt);
299 if (menu_has_help(menu))
302 switch (input_mode) {
304 case silentoldconfig:
314 xfgets(line, 128, stdin);
316 if (line[0] == '?') {
322 else if (isdigit(line[0]))
332 for (child = menu->list; child; child = child->next) {
333 if (!child->sym || !menu_is_visible(child))
340 if (line[0] && line[strlen(line) - 1] == '?') {
344 sym_set_choice_value(sym, child->sym);
345 for (child = child->list; child; child = child->next) {
354 static void conf(struct menu *menu)
357 struct property *prop;
360 if (!menu_is_visible(menu))
368 switch (prop->type) {
370 if ((input_mode == silentoldconfig ||
371 input_mode == listnewconfig ||
372 input_mode == olddefconfig) &&
379 prompt = menu_get_prompt(menu);
381 printf("%*c\n%*c %s\n%*c\n",
383 indent, '*', _(prompt),
393 if (sym_is_choice(sym)) {
395 if (sym->curr.tri != mod)
414 for (child = menu->list; child; child = child->next)
420 static void check_conf(struct menu *menu)
425 if (!menu_is_visible(menu))
429 if (sym && !sym_has_value(sym)) {
430 if (sym_is_changable(sym) ||
431 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
432 if (input_mode == listnewconfig) {
433 if (sym->name && !sym_is_choice_value(sym)) {
434 printf("%s%s\n", CONFIG_, sym->name);
436 } else if (input_mode != olddefconfig) {
438 printf(_("*\n* Restart config...\n*\n"));
439 rootEntry = menu_get_parent_menu(menu);
445 for (child = menu->list; child; child = child->next)
449 static struct option long_opts[] = {
450 {"oldaskconfig", no_argument, NULL, oldaskconfig},
451 {"oldconfig", no_argument, NULL, oldconfig},
452 {"silentoldconfig", no_argument, NULL, silentoldconfig},
453 {"defconfig", optional_argument, NULL, defconfig},
454 {"savedefconfig", required_argument, NULL, savedefconfig},
455 {"allnoconfig", no_argument, NULL, allnoconfig},
456 {"allyesconfig", no_argument, NULL, allyesconfig},
457 {"allmodconfig", no_argument, NULL, allmodconfig},
458 {"alldefconfig", no_argument, NULL, alldefconfig},
459 {"randconfig", no_argument, NULL, randconfig},
460 {"listnewconfig", no_argument, NULL, listnewconfig},
461 {"olddefconfig", no_argument, NULL, olddefconfig},
463 * oldnoconfig is an alias of olddefconfig, because people already
464 * are dependent on its behavior(sets new symbols to their default
465 * value but not 'n') with the counter-intuitive name.
467 {"oldnoconfig", no_argument, NULL, olddefconfig},
471 static void conf_usage(const char *progname)
474 printf("Usage: %s [option] <kconfig-file>\n", progname);
475 printf("[option] is _one_ of the following:\n");
476 printf(" --listnewconfig List new options\n");
477 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
478 printf(" --oldconfig Update a configuration using a provided .config as base\n");
479 printf(" --silentoldconfig Same as oldconfig, but quietly, additionally update deps\n");
480 printf(" --olddefconfig Same as silentoldconfig but sets new symbols to their default value\n");
481 printf(" --oldnoconfig An alias of olddefconfig\n");
482 printf(" --defconfig <file> New config with default defined in <file>\n");
483 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
484 printf(" --allnoconfig New config where all options are answered with no\n");
485 printf(" --allyesconfig New config where all options are answered with yes\n");
486 printf(" --allmodconfig New config where all options are answered with mod\n");
487 printf(" --alldefconfig New config with all symbols set to default\n");
488 printf(" --randconfig New config with random answer to all options\n");
491 int main(int ac, char **av)
493 const char *progname = av[0];
495 const char *name, *defconfig_file = NULL /* gcc uninit */;
498 setlocale(LC_ALL, "");
499 bindtextdomain(PACKAGE, LOCALEDIR);
502 tty_stdio = isatty(0) && isatty(1) && isatty(2);
504 while ((opt = getopt_long(ac, av, "", long_opts, NULL)) != -1) {
505 input_mode = (enum input_mode)opt;
507 case silentoldconfig:
512 defconfig_file = optarg;
521 * Use microseconds derived seed,
522 * compensate for systems where it may be zero
524 gettimeofday(&now, NULL);
525 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
527 seed_env = getenv("KCONFIG_SEED");
528 if( seed_env && *seed_env ) {
530 int tmp = (int)strtol(seed_env, &endp, 0);
535 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
549 conf_usage(progname);
555 printf(_("%s: Kconfig file missing\n"), av[0]);
556 conf_usage(progname);
563 name = conf_get_configname();
564 if (stat(name, &tmpstat)) {
565 fprintf(stderr, _("***\n"
566 "*** Configuration file \"%s\" not found!\n"
568 "*** Please run some configurator (e.g. \"make oldconfig\" or\n"
569 "*** \"make menuconfig\" or \"make xconfig\").\n"
575 switch (input_mode) {
578 defconfig_file = conf_get_default_confname();
579 if (conf_read(defconfig_file)) {
581 "*** Can't find default configuration \"%s\"!\n"
582 "***\n"), defconfig_file);
587 case silentoldconfig:
599 name = getenv("KCONFIG_ALLCONFIG");
602 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
603 if (conf_read_simple(name, S_DEF_USER)) {
605 _("*** Can't read seed configuration \"%s\"!\n"),
611 switch (input_mode) {
612 case allnoconfig: name = "allno.config"; break;
613 case allyesconfig: name = "allyes.config"; break;
614 case allmodconfig: name = "allmod.config"; break;
615 case alldefconfig: name = "alldef.config"; break;
616 case randconfig: name = "allrandom.config"; break;
619 if (conf_read_simple(name, S_DEF_USER) &&
620 conf_read_simple("all.config", S_DEF_USER)) {
622 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
632 if (conf_get_changed()) {
633 name = getenv("KCONFIG_NOSILENTUPDATE");
636 _("\n*** The configuration requires explicit update.\n\n"));
640 valid_stdin = tty_stdio;
643 switch (input_mode) {
645 conf_set_all_new_symbols(def_no);
648 conf_set_all_new_symbols(def_yes);
651 conf_set_all_new_symbols(def_mod);
654 conf_set_all_new_symbols(def_default);
657 /* Really nothing to do in this loop */
658 while (conf_set_all_new_symbols(def_random)) ;
661 conf_set_all_new_symbols(def_default);
666 rootEntry = &rootmenu;
668 input_mode = silentoldconfig;
673 case silentoldconfig:
674 /* Update until a loop caused no more changes */
677 check_conf(&rootmenu);
679 (input_mode != listnewconfig &&
680 input_mode != olddefconfig));
685 /* silentoldconfig is used during the build so we shall update autoconf.
686 * All other commands are only used to generate a config.
688 if (conf_get_changed() && conf_write(NULL)) {
689 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
692 if (conf_write_autoconf()) {
693 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
696 } else if (input_mode == savedefconfig) {
697 if (conf_write_defconfig(defconfig_file)) {
698 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
702 } else if (input_mode != listnewconfig) {
703 if (conf_write(NULL)) {
704 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
712 * Helper function to facilitate fgets() by Jean Sacren.
714 void xfgets(char *str, int size, FILE *in)
716 if (fgets(str, size, in) == NULL)
717 fprintf(stderr, "\nError in reading or end of file.\n");