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);
36 } input_mode = oldaskconfig;
38 static int indent = 1;
40 static int valid_stdin = 1;
42 static char line[128];
43 static struct menu *rootEntry;
45 static void print_help(struct menu *menu)
47 struct gstr help = str_new();
49 menu_get_ext_help(menu, &help);
51 printf("\n%s\n", str_get(&help));
55 static void strip(char *str)
64 memmove(str, p, l + 1);
72 static void check_stdin(void)
75 printf(_("aborted!\n\n"));
76 printf(_("Console input/output is redirected. "));
77 printf(_("Run 'make config' to update configuration.\n\n"));
82 static int conf_askvalue(struct symbol *sym, const char *def)
84 enum symbol_type type = sym_get_type(sym);
86 if (!sym_has_value(sym))
92 if (!sym_is_changable(sym)) {
101 if (sym_has_value(sym)) {
109 xfgets(line, 128, stdin);
130 static int conf_string(struct menu *menu)
132 struct symbol *sym = menu->sym;
136 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
137 printf("(%s) ", sym->name);
138 def = sym_get_string_value(sym);
139 if (sym_get_string_value(sym))
140 printf("[%s] ", def);
141 if (!conf_askvalue(sym, def))
148 if (line[1] == '\n') {
155 line[strlen(line)-1] = 0;
158 if (def && sym_set_string_value(sym, def))
163 static int conf_sym(struct menu *menu)
165 struct symbol *sym = menu->sym;
166 tristate oldval, newval;
169 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
171 printf("(%s) ", sym->name);
173 oldval = sym_get_tristate_value(sym);
185 if (oldval != no && sym_tristate_within_range(sym, no))
187 if (oldval != mod && sym_tristate_within_range(sym, mod))
189 if (oldval != yes && sym_tristate_within_range(sym, yes))
191 if (menu_has_help(menu))
194 if (!conf_askvalue(sym, sym_get_string_value(sym)))
202 if (!line[1] || !strcmp(&line[1], "o"))
214 if (!line[1] || !strcmp(&line[1], "es"))
225 if (sym_set_tristate_value(sym, newval))
232 static int conf_choice(struct menu *menu)
234 struct symbol *sym, *def_sym;
239 is_new = !sym_has_value(sym);
240 if (sym_is_changable(sym)) {
243 switch (sym_get_tristate_value(sym)) {
252 switch (sym_get_tristate_value(sym)) {
256 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
266 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
267 def_sym = sym_get_choice_value(sym);
270 for (child = menu->list; child; child = child->next) {
271 if (!menu_is_visible(child))
274 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
278 if (child->sym == def_sym) {
280 printf("%*c", indent, '>');
282 printf("%*c", indent, ' ');
283 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
284 if (child->sym->name)
285 printf(" (%s)", child->sym->name);
286 if (!sym_has_value(child->sym))
290 printf(_("%*schoice"), indent - 1, "");
295 printf("[1-%d", cnt);
296 if (menu_has_help(menu))
299 switch (input_mode) {
310 xfgets(line, 128, stdin);
312 if (line[0] == '?') {
318 else if (isdigit(line[0]))
328 for (child = menu->list; child; child = child->next) {
329 if (!child->sym || !menu_is_visible(child))
336 if (line[0] && line[strlen(line) - 1] == '?') {
340 sym_set_choice_value(sym, child->sym);
341 for (child = child->list; child; child = child->next) {
350 static void conf(struct menu *menu)
353 struct property *prop;
356 if (!menu_is_visible(menu))
364 switch (prop->type) {
366 if ((input_mode == listnewconfig ||
367 input_mode == oldnoconfig) &&
374 prompt = menu_get_prompt(menu);
376 printf("%*c\n%*c %s\n%*c\n",
378 indent, '*', _(prompt),
388 if (sym_is_choice(sym)) {
390 if (sym->curr.tri != mod)
409 for (child = menu->list; child; child = child->next)
415 static void check_conf(struct menu *menu)
420 if (!menu_is_visible(menu))
424 if (sym && !sym_has_value(sym)) {
425 if (sym_is_changable(sym) ||
426 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
427 if (input_mode == listnewconfig) {
428 if (sym->name && !sym_is_choice_value(sym)) {
429 printf("%s%s\n", CONFIG_, sym->name);
431 } else if (input_mode != oldnoconfig) {
433 printf(_("*\n* Restart config...\n*\n"));
434 rootEntry = menu_get_parent_menu(menu);
440 for (child = menu->list; child; child = child->next)
444 static struct option long_opts[] = {
445 {"askconfig", no_argument, NULL, oldaskconfig},
446 {"config", no_argument, NULL, oldconfig},
447 {"defconfig", optional_argument, NULL, defconfig},
448 {"savedefconfig", required_argument, NULL, savedefconfig},
449 {"allnoconfig", no_argument, NULL, allnoconfig},
450 {"allyesconfig", no_argument, NULL, allyesconfig},
451 {"allmodconfig", no_argument, NULL, allmodconfig},
452 {"alldefconfig", no_argument, NULL, alldefconfig},
453 {"randconfig", no_argument, NULL, randconfig},
454 {"listnewconfig", no_argument, NULL, listnewconfig},
455 {"noconfig", no_argument, NULL, oldnoconfig},
459 static void conf_usage(const char *progname)
462 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
463 printf("[option] is _one_ of the following:\n");
464 printf(" --listnewconfig List new options\n");
465 printf(" --askconfig Start a new configuration using a line-oriented program\n");
466 printf(" --config Update a configuration using a provided .config as base\n");
467 printf(" --silentconfig Same as config, but quietly, additionally update deps\n");
468 printf(" --noconfig Same as silentconfig but set new symbols to no\n");
469 printf(" --defconfig <file> New config with default defined in <file>\n");
470 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
471 printf(" --allnoconfig New config where all options are answered with no\n");
472 printf(" --allyesconfig New config where all options are answered with yes\n");
473 printf(" --allmodconfig New config where all options are answered with mod\n");
474 printf(" --alldefconfig New config with all symbols set to default\n");
475 printf(" --randconfig New config with random answer to all options\n");
478 int main(int ac, char **av)
480 const char *progname = av[0];
482 const char *name, *defconfig_file = NULL /* gcc uninit */;
485 setlocale(LC_ALL, "");
486 bindtextdomain(PACKAGE, LOCALEDIR);
489 tty_stdio = isatty(0) && isatty(1) && isatty(2);
491 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
493 conf_set_message_callback(NULL);
496 input_mode = (enum input_mode)opt;
500 defconfig_file = optarg;
509 * Use microseconds derived seed,
510 * compensate for systems where it may be zero
512 gettimeofday(&now, NULL);
513 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
515 seed_env = getenv("KCONFIG_SEED");
516 if( seed_env && *seed_env ) {
518 int tmp = (int)strtol(seed_env, &endp, 0);
523 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
537 conf_usage(progname);
543 printf(_("%s: Kconfig file missing\n"), av[0]);
544 conf_usage(progname);
551 switch (input_mode) {
554 defconfig_file = conf_get_default_confname();
555 if (conf_read(defconfig_file)) {
557 "*** Can't find default configuration \"%s\"!\n"
558 "***\n"), defconfig_file);
574 name = getenv("KCONFIG_ALLCONFIG");
577 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
578 if (conf_read_simple(name, S_DEF_USER)) {
580 _("*** Can't read seed configuration \"%s\"!\n"),
586 switch (input_mode) {
587 case allnoconfig: name = "allno.config"; break;
588 case allyesconfig: name = "allyes.config"; break;
589 case allmodconfig: name = "allmod.config"; break;
590 case alldefconfig: name = "alldef.config"; break;
591 case randconfig: name = "allrandom.config"; break;
594 if (conf_read_simple(name, S_DEF_USER) &&
595 conf_read_simple("all.config", S_DEF_USER)) {
597 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
606 valid_stdin = tty_stdio;
608 switch (input_mode) {
610 conf_set_all_new_symbols(def_no);
613 conf_set_all_new_symbols(def_yes);
616 conf_set_all_new_symbols(def_mod);
619 conf_set_all_new_symbols(def_default);
622 /* Really nothing to do in this loop */
623 while (conf_set_all_new_symbols(def_random)) ;
626 conf_set_all_new_symbols(def_default);
631 rootEntry = &rootmenu;
633 input_mode = oldconfig;
638 /* Update until a loop caused no more changes */
641 check_conf(&rootmenu);
643 (input_mode != listnewconfig &&
644 input_mode != oldnoconfig));
648 if (input_mode == savedefconfig) {
649 if (conf_write_defconfig(defconfig_file)) {
650 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
654 } else if (input_mode != listnewconfig) {
656 * build so we shall update autoconf.
658 if (conf_write(NULL)) {
659 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
662 if (conf_write_autoconf()) {
663 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
671 * Helper function to facilitate fgets() by Jean Sacren.
673 void xfgets(char *str, int size, FILE *in)
675 if (fgets(str, size, in) == NULL)
676 fprintf(stderr, "\nError in reading or end of file.\n");