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);
37 } input_mode = oldaskconfig;
39 static int indent = 1;
41 static int valid_stdin = 1;
43 static char line[PATH_MAX];
44 static struct menu *rootEntry;
46 static void print_help(struct menu *menu)
48 struct gstr help = str_new();
50 menu_get_ext_help(menu, &help);
52 printf("\n%s\n", str_get(&help));
56 static void strip(char *str)
65 memmove(str, p, l + 1);
73 static void check_stdin(void)
76 printf(_("aborted!\n\n"));
77 printf(_("Console input/output is redirected. "));
78 printf(_("Run 'make config' to update configuration.\n\n"));
83 static int conf_askvalue(struct symbol *sym, const char *def)
85 enum symbol_type type = sym_get_type(sym);
87 if (!sym_has_value(sym))
93 if (!sym_is_changable(sym)) {
100 switch (input_mode) {
102 if (sym_has_value(sym)) {
110 xfgets(line, sizeof(line), stdin);
131 static int conf_string(struct menu *menu)
133 struct symbol *sym = menu->sym;
137 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
138 printf("(%s) ", sym->name);
139 def = sym_get_string_value(sym);
140 if (sym_get_string_value(sym))
141 printf("[%s] ", def);
142 if (!conf_askvalue(sym, def))
149 if (line[1] == '\n') {
156 line[strlen(line)-1] = 0;
159 if (def && sym_set_string_value(sym, def))
164 static int conf_sym(struct menu *menu)
166 struct symbol *sym = menu->sym;
167 tristate oldval, newval;
170 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
172 printf("(%s) ", sym->name);
174 oldval = sym_get_tristate_value(sym);
186 if (oldval != no && sym_tristate_within_range(sym, no))
188 if (oldval != mod && sym_tristate_within_range(sym, mod))
190 if (oldval != yes && sym_tristate_within_range(sym, yes))
192 if (menu_has_help(menu))
195 if (!conf_askvalue(sym, sym_get_string_value(sym)))
203 if (!line[1] || !strcmp(&line[1], "o"))
215 if (!line[1] || !strcmp(&line[1], "es"))
226 if (sym_set_tristate_value(sym, newval))
233 static int conf_choice(struct menu *menu)
235 struct symbol *sym, *def_sym;
240 is_new = !sym_has_value(sym);
241 if (sym_is_changable(sym)) {
244 switch (sym_get_tristate_value(sym)) {
253 switch (sym_get_tristate_value(sym)) {
257 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
267 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
268 def_sym = sym_get_choice_value(sym);
271 for (child = menu->list; child; child = child->next) {
272 if (!menu_is_visible(child))
275 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
279 if (child->sym == def_sym) {
281 printf("%*c", indent, '>');
283 printf("%*c", indent, ' ');
284 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
285 if (child->sym->name)
286 printf(" (%s)", child->sym->name);
287 if (!sym_has_value(child->sym))
291 printf(_("%*schoice"), indent - 1, "");
296 printf("[1-%d", cnt);
297 if (menu_has_help(menu))
300 switch (input_mode) {
311 xfgets(line, sizeof(line), stdin);
313 if (line[0] == '?') {
319 else if (isdigit(line[0]))
329 for (child = menu->list; child; child = child->next) {
330 if (!child->sym || !menu_is_visible(child))
337 if (line[0] && line[strlen(line) - 1] == '?') {
341 sym_set_choice_value(sym, child->sym);
342 for (child = child->list; child; child = child->next) {
351 static void conf(struct menu *menu)
354 struct property *prop;
357 if (!menu_is_visible(menu))
365 switch (prop->type) {
367 if ((input_mode == listnewconfig ||
368 input_mode == oldnoconfig) &&
375 prompt = menu_get_prompt(menu);
377 printf("%*c\n%*c %s\n%*c\n",
379 indent, '*', _(prompt),
389 if (sym_is_choice(sym)) {
391 if (sym->curr.tri != mod)
410 for (child = menu->list; child; child = child->next)
416 static void check_conf(struct menu *menu)
421 if (!menu_is_visible(menu))
425 if (sym && !sym_has_value(sym)) {
426 if (sym_is_changable(sym) ||
427 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
428 if (input_mode == listnewconfig) {
429 if (sym->name && !sym_is_choice_value(sym)) {
430 printf("%s%s\n", CONFIG_, sym->name);
432 } else if (input_mode != oldnoconfig) {
434 printf(_("*\n* Restart config...\n*\n"));
435 rootEntry = menu_get_parent_menu(menu);
441 for (child = menu->list; child; child = child->next)
445 static struct option long_opts[] = {
446 {"askconfig", no_argument, NULL, oldaskconfig},
447 {"config", no_argument, NULL, oldconfig},
448 {"defconfig", optional_argument, NULL, defconfig},
449 {"savedefconfig", required_argument, NULL, savedefconfig},
450 {"allnoconfig", no_argument, NULL, allnoconfig},
451 {"allyesconfig", no_argument, NULL, allyesconfig},
452 {"allmodconfig", no_argument, NULL, allmodconfig},
453 {"alldefconfig", no_argument, NULL, alldefconfig},
454 {"randconfig", no_argument, NULL, randconfig},
455 {"listnewconfig", no_argument, NULL, listnewconfig},
456 {"noconfig", no_argument, NULL, oldnoconfig},
460 static void conf_usage(const char *progname)
463 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
464 printf("[option] is _one_ of the following:\n");
465 printf(" --listnewconfig List new options\n");
466 printf(" --askconfig Start a new configuration using a line-oriented program\n");
467 printf(" --config Update a configuration using a provided .config as base\n");
468 printf(" --silentconfig Same as config, but quietly, additionally update deps\n");
469 printf(" --noconfig Same as silentconfig but set new symbols to no\n");
470 printf(" --defconfig <file> New config with default defined in <file>\n");
471 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
472 printf(" --allnoconfig New config where all options are answered with no\n");
473 printf(" --allyesconfig New config where all options are answered with yes\n");
474 printf(" --allmodconfig New config where all options are answered with mod\n");
475 printf(" --alldefconfig New config with all symbols set to default\n");
476 printf(" --randconfig New config with random answer to all options\n");
479 int main(int ac, char **av)
481 const char *progname = av[0];
483 const char *name, *defconfig_file = NULL /* gcc uninit */;
486 setlocale(LC_ALL, "");
487 bindtextdomain(PACKAGE, LOCALEDIR);
490 tty_stdio = isatty(0) && isatty(1) && isatty(2);
492 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
494 conf_set_message_callback(NULL);
497 input_mode = (enum input_mode)opt;
501 defconfig_file = optarg;
510 * Use microseconds derived seed,
511 * compensate for systems where it may be zero
513 gettimeofday(&now, NULL);
514 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
516 seed_env = getenv("KCONFIG_SEED");
517 if( seed_env && *seed_env ) {
519 int tmp = (int)strtol(seed_env, &endp, 0);
524 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
538 conf_usage(progname);
544 printf(_("%s: Kconfig file missing\n"), av[0]);
545 conf_usage(progname);
552 switch (input_mode) {
555 defconfig_file = conf_get_default_confname();
556 if (conf_read(defconfig_file)) {
558 "*** Can't find default configuration \"%s\"!\n"
559 "***\n"), defconfig_file);
575 name = getenv("KCONFIG_ALLCONFIG");
578 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
579 if (conf_read_simple(name, S_DEF_USER)) {
581 _("*** Can't read seed configuration \"%s\"!\n"),
587 switch (input_mode) {
588 case allnoconfig: name = "allno.config"; break;
589 case allyesconfig: name = "allyes.config"; break;
590 case allmodconfig: name = "allmod.config"; break;
591 case alldefconfig: name = "alldef.config"; break;
592 case randconfig: name = "allrandom.config"; break;
595 if (conf_read_simple(name, S_DEF_USER) &&
596 conf_read_simple("all.config", S_DEF_USER)) {
598 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
607 valid_stdin = tty_stdio;
609 switch (input_mode) {
611 conf_set_all_new_symbols(def_no);
614 conf_set_all_new_symbols(def_yes);
617 conf_set_all_new_symbols(def_mod);
620 conf_set_all_new_symbols(def_default);
623 /* Really nothing to do in this loop */
624 while (conf_set_all_new_symbols(def_random)) ;
627 conf_set_all_new_symbols(def_default);
632 rootEntry = &rootmenu;
634 input_mode = oldconfig;
639 /* Update until a loop caused no more changes */
642 check_conf(&rootmenu);
644 (input_mode != listnewconfig &&
645 input_mode != oldnoconfig));
649 if (input_mode == savedefconfig) {
650 if (conf_write_defconfig(defconfig_file)) {
651 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
655 } else if (input_mode != listnewconfig) {
657 * build so we shall update autoconf.
659 if (conf_write(NULL)) {
660 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
663 if (conf_write_autoconf()) {
664 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
672 * Helper function to facilitate fgets() by Jean Sacren.
674 void xfgets(char *str, int size, FILE *in)
676 if (fgets(str, size, in) == NULL)
677 fprintf(stderr, "\nError in reading or end of file.\n");