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);
23 static void xfgets(char *str, int size, FILE *in);
38 static enum input_mode input_mode = oldaskconfig;
40 static int indent = 1;
42 static int valid_stdin = 1;
44 static char line[PATH_MAX];
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 config' 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 if (sym_has_value(sym)) {
111 xfgets(line, sizeof(line), stdin);
132 static int conf_string(struct menu *menu)
134 struct symbol *sym = menu->sym;
138 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
139 printf("(%s) ", sym->name);
140 def = sym_get_string_value(sym);
141 if (sym_get_string_value(sym))
142 printf("[%s] ", def);
143 if (!conf_askvalue(sym, def))
150 if (line[1] == '\n') {
157 line[strlen(line)-1] = 0;
160 if (def && sym_set_string_value(sym, def))
165 static int conf_sym(struct menu *menu)
167 struct symbol *sym = menu->sym;
168 tristate oldval, newval;
171 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
173 printf("(%s) ", sym->name);
175 oldval = sym_get_tristate_value(sym);
187 if (oldval != no && sym_tristate_within_range(sym, no))
189 if (oldval != mod && sym_tristate_within_range(sym, mod))
191 if (oldval != yes && sym_tristate_within_range(sym, yes))
193 if (menu_has_help(menu))
196 if (!conf_askvalue(sym, sym_get_string_value(sym)))
204 if (!line[1] || !strcmp(&line[1], "o"))
216 if (!line[1] || !strcmp(&line[1], "es"))
227 if (sym_set_tristate_value(sym, newval))
234 static int conf_choice(struct menu *menu)
236 struct symbol *sym, *def_sym;
241 is_new = !sym_has_value(sym);
242 if (sym_is_changable(sym)) {
245 switch (sym_get_tristate_value(sym)) {
254 switch (sym_get_tristate_value(sym)) {
258 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
268 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
269 def_sym = sym_get_choice_value(sym);
272 for (child = menu->list; child; child = child->next) {
273 if (!menu_is_visible(child))
276 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
280 if (child->sym == def_sym) {
282 printf("%*c", indent, '>');
284 printf("%*c", indent, ' ');
285 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
286 if (child->sym->name)
287 printf(" (%s)", child->sym->name);
288 if (!sym_has_value(child->sym))
292 printf(_("%*schoice"), indent - 1, "");
297 printf("[1-%d", cnt);
298 if (menu_has_help(menu))
301 switch (input_mode) {
312 xfgets(line, sizeof(line), stdin);
314 if (line[0] == '?') {
320 else if (isdigit(line[0]))
330 for (child = menu->list; child; child = child->next) {
331 if (!child->sym || !menu_is_visible(child))
338 if (line[0] && line[strlen(line) - 1] == '?') {
342 sym_set_choice_value(sym, child->sym);
343 for (child = child->list; child; child = child->next) {
352 static void conf(struct menu *menu)
355 struct property *prop;
358 if (!menu_is_visible(menu))
366 switch (prop->type) {
368 if ((input_mode == listnewconfig ||
369 input_mode == oldnoconfig) &&
376 prompt = menu_get_prompt(menu);
378 printf("%*c\n%*c %s\n%*c\n",
380 indent, '*', _(prompt),
390 if (sym_is_choice(sym)) {
392 if (sym->curr.tri != mod)
411 for (child = menu->list; child; child = child->next)
417 static void check_conf(struct menu *menu)
422 if (!menu_is_visible(menu))
426 if (sym && !sym_has_value(sym)) {
427 if (sym_is_changable(sym) ||
428 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
429 if (input_mode == listnewconfig) {
430 if (sym->name && !sym_is_choice_value(sym)) {
431 printf("%s%s\n", CONFIG_, sym->name);
433 } else if (input_mode != oldnoconfig) {
435 printf(_("*\n* Restart config...\n*\n"));
436 rootEntry = menu_get_parent_menu(menu);
442 for (child = menu->list; child; child = child->next)
446 static struct option long_opts[] = {
447 {"askconfig", no_argument, NULL, oldaskconfig},
448 {"config", no_argument, NULL, oldconfig},
449 {"defconfig", optional_argument, NULL, defconfig},
450 {"savedefconfig", required_argument, NULL, savedefconfig},
451 {"allnoconfig", no_argument, NULL, allnoconfig},
452 {"allyesconfig", no_argument, NULL, allyesconfig},
453 {"allmodconfig", no_argument, NULL, allmodconfig},
454 {"alldefconfig", no_argument, NULL, alldefconfig},
455 {"randconfig", no_argument, NULL, randconfig},
456 {"listnewconfig", no_argument, NULL, listnewconfig},
457 {"noconfig", no_argument, NULL, oldnoconfig},
461 static void conf_usage(const char *progname)
464 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
465 printf("[option] is _one_ of the following:\n");
466 printf(" --listnewconfig List new options\n");
467 printf(" --askconfig Start a new configuration using a line-oriented program\n");
468 printf(" --config Update a configuration using a provided .config as base\n");
469 printf(" --silentconfig Same as config, but quietly, additionally update deps\n");
470 printf(" --noconfig Same as silentconfig but set new symbols to no\n");
471 printf(" --defconfig <file> New config with default defined in <file>\n");
472 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
473 printf(" --allnoconfig New config where all options are answered with no\n");
474 printf(" --allyesconfig New config where all options are answered with yes\n");
475 printf(" --allmodconfig New config where all options are answered with mod\n");
476 printf(" --alldefconfig New config with all symbols set to default\n");
477 printf(" --randconfig New config with random answer to all options\n");
480 int main(int ac, char **av)
482 const char *progname = av[0];
484 const char *name, *defconfig_file = NULL /* gcc uninit */;
487 setlocale(LC_ALL, "");
488 bindtextdomain(PACKAGE, LOCALEDIR);
491 tty_stdio = isatty(0) && isatty(1) && isatty(2);
493 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
495 conf_set_message_callback(NULL);
498 input_mode = (enum input_mode)opt;
502 defconfig_file = optarg;
511 * Use microseconds derived seed,
512 * compensate for systems where it may be zero
514 gettimeofday(&now, NULL);
515 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
517 seed_env = getenv("KCONFIG_SEED");
518 if( seed_env && *seed_env ) {
520 int tmp = (int)strtol(seed_env, &endp, 0);
525 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
539 conf_usage(progname);
545 printf(_("%s: Kconfig file missing\n"), av[0]);
546 conf_usage(progname);
553 switch (input_mode) {
556 defconfig_file = conf_get_default_confname();
557 if (conf_read(defconfig_file)) {
559 "*** Can't find default configuration \"%s\"!\n"
560 "***\n"), defconfig_file);
576 name = getenv("KCONFIG_ALLCONFIG");
579 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
580 if (conf_read_simple(name, S_DEF_USER)) {
582 _("*** Can't read seed configuration \"%s\"!\n"),
588 switch (input_mode) {
589 case allnoconfig: name = "allno.config"; break;
590 case allyesconfig: name = "allyes.config"; break;
591 case allmodconfig: name = "allmod.config"; break;
592 case alldefconfig: name = "alldef.config"; break;
593 case randconfig: name = "allrandom.config"; break;
596 if (conf_read_simple(name, S_DEF_USER) &&
597 conf_read_simple("all.config", S_DEF_USER)) {
599 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
608 valid_stdin = tty_stdio;
610 switch (input_mode) {
612 conf_set_all_new_symbols(def_no);
615 conf_set_all_new_symbols(def_yes);
618 conf_set_all_new_symbols(def_mod);
621 conf_set_all_new_symbols(def_default);
624 /* Really nothing to do in this loop */
625 while (conf_set_all_new_symbols(def_random)) ;
628 conf_set_all_new_symbols(def_default);
633 rootEntry = &rootmenu;
635 input_mode = oldconfig;
640 /* Update until a loop caused no more changes */
643 check_conf(&rootmenu);
645 (input_mode != listnewconfig &&
646 input_mode != oldnoconfig));
650 if (input_mode == savedefconfig) {
651 if (conf_write_defconfig(defconfig_file)) {
652 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
656 } else if (input_mode != listnewconfig) {
658 * build so we shall update autoconf.
660 if (conf_write(NULL)) {
661 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
664 if (conf_write_autoconf()) {
665 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
673 * Helper function to facilitate fgets() by Jean Sacren.
675 void xfgets(char *str, int size, FILE *in)
677 if (fgets(str, size, in) == NULL)
678 fprintf(stderr, "\nError in reading or end of file.\n");