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);
37 static enum input_mode 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 /* Helper function to facilitate fgets() by Jean Sacren. */
84 static void xfgets(char *str, int size, FILE *in)
86 if (!fgets(str, size, in))
87 fprintf(stderr, "\nError in reading or end of file.\n");
90 static int conf_askvalue(struct symbol *sym, const char *def)
92 enum symbol_type type = sym_get_type(sym);
94 if (!sym_has_value(sym))
100 if (!sym_is_changable(sym)) {
107 switch (input_mode) {
109 if (sym_has_value(sym)) {
117 xfgets(line, sizeof(line), stdin);
138 static int conf_string(struct menu *menu)
140 struct symbol *sym = menu->sym;
144 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
145 printf("(%s) ", sym->name);
146 def = sym_get_string_value(sym);
147 if (sym_get_string_value(sym))
148 printf("[%s] ", def);
149 if (!conf_askvalue(sym, def))
156 if (line[1] == '\n') {
163 line[strlen(line)-1] = 0;
166 if (def && sym_set_string_value(sym, def))
171 static int conf_sym(struct menu *menu)
173 struct symbol *sym = menu->sym;
174 tristate oldval, newval;
177 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
179 printf("(%s) ", sym->name);
181 oldval = sym_get_tristate_value(sym);
193 if (oldval != no && sym_tristate_within_range(sym, no))
195 if (oldval != mod && sym_tristate_within_range(sym, mod))
197 if (oldval != yes && sym_tristate_within_range(sym, yes))
199 if (menu_has_help(menu))
202 if (!conf_askvalue(sym, sym_get_string_value(sym)))
210 if (!line[1] || !strcmp(&line[1], "o"))
222 if (!line[1] || !strcmp(&line[1], "es"))
233 if (sym_set_tristate_value(sym, newval))
240 static int conf_choice(struct menu *menu)
242 struct symbol *sym, *def_sym;
247 is_new = !sym_has_value(sym);
248 if (sym_is_changable(sym)) {
251 switch (sym_get_tristate_value(sym)) {
260 switch (sym_get_tristate_value(sym)) {
264 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
274 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
275 def_sym = sym_get_choice_value(sym);
278 for (child = menu->list; child; child = child->next) {
279 if (!menu_is_visible(child))
282 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
286 if (child->sym == def_sym) {
288 printf("%*c", indent, '>');
290 printf("%*c", indent, ' ');
291 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
292 if (child->sym->name)
293 printf(" (%s)", child->sym->name);
294 if (!sym_has_value(child->sym))
298 printf(_("%*schoice"), indent - 1, "");
303 printf("[1-%d", cnt);
304 if (menu_has_help(menu))
307 switch (input_mode) {
318 xfgets(line, sizeof(line), stdin);
320 if (line[0] == '?') {
326 else if (isdigit(line[0]))
336 for (child = menu->list; child; child = child->next) {
337 if (!child->sym || !menu_is_visible(child))
344 if (line[0] && line[strlen(line) - 1] == '?') {
348 sym_set_choice_value(sym, child->sym);
349 for (child = child->list; child; child = child->next) {
358 static void conf(struct menu *menu)
361 struct property *prop;
364 if (!menu_is_visible(menu))
372 switch (prop->type) {
374 if ((input_mode == listnewconfig ||
375 input_mode == oldnoconfig) &&
382 prompt = menu_get_prompt(menu);
384 printf("%*c\n%*c %s\n%*c\n",
386 indent, '*', _(prompt),
396 if (sym_is_choice(sym)) {
398 if (sym->curr.tri != mod)
417 for (child = menu->list; child; child = child->next)
423 static void check_conf(struct menu *menu)
428 if (!menu_is_visible(menu))
432 if (sym && !sym_has_value(sym)) {
433 if (sym_is_changable(sym) ||
434 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
435 if (input_mode == listnewconfig) {
436 if (sym->name && !sym_is_choice_value(sym)) {
437 printf("%s%s\n", CONFIG_, sym->name);
439 } else if (input_mode != oldnoconfig) {
441 printf(_("*\n* Restart config...\n*\n"));
442 rootEntry = menu_get_parent_menu(menu);
448 for (child = menu->list; child; child = child->next)
452 static struct option long_opts[] = {
453 {"askconfig", no_argument, NULL, oldaskconfig},
454 {"config", no_argument, NULL, oldconfig},
455 {"defconfig", optional_argument, NULL, defconfig},
456 {"savedefconfig", required_argument, NULL, savedefconfig},
457 {"allnoconfig", no_argument, NULL, allnoconfig},
458 {"allyesconfig", no_argument, NULL, allyesconfig},
459 {"allmodconfig", no_argument, NULL, allmodconfig},
460 {"alldefconfig", no_argument, NULL, alldefconfig},
461 {"randconfig", no_argument, NULL, randconfig},
462 {"listnewconfig", no_argument, NULL, listnewconfig},
463 {"noconfig", no_argument, NULL, oldnoconfig},
467 static void conf_usage(const char *progname)
470 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
471 printf("[option] is _one_ of the following:\n");
472 printf(" --listnewconfig List new options\n");
473 printf(" --askconfig Start a new configuration using a line-oriented program\n");
474 printf(" --config Update a configuration using a provided .config as base\n");
475 printf(" --silentconfig Same as config, but quietly, additionally update deps\n");
476 printf(" --noconfig Same as silentconfig but set new symbols to no\n");
477 printf(" --defconfig <file> New config with default defined in <file>\n");
478 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
479 printf(" --allnoconfig New config where all options are answered with no\n");
480 printf(" --allyesconfig New config where all options are answered with yes\n");
481 printf(" --allmodconfig New config where all options are answered with mod\n");
482 printf(" --alldefconfig New config with all symbols set to default\n");
483 printf(" --randconfig New config with random answer to all options\n");
486 int main(int ac, char **av)
488 const char *progname = av[0];
490 const char *name, *defconfig_file = NULL /* gcc uninit */;
493 setlocale(LC_ALL, "");
494 bindtextdomain(PACKAGE, LOCALEDIR);
497 tty_stdio = isatty(0) && isatty(1) && isatty(2);
499 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
501 conf_set_message_callback(NULL);
504 input_mode = (enum input_mode)opt;
508 defconfig_file = optarg;
517 * Use microseconds derived seed,
518 * compensate for systems where it may be zero
520 gettimeofday(&now, NULL);
521 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
523 seed_env = getenv("KCONFIG_SEED");
524 if( seed_env && *seed_env ) {
526 int tmp = (int)strtol(seed_env, &endp, 0);
531 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
545 conf_usage(progname);
551 printf(_("%s: Kconfig file missing\n"), av[0]);
552 conf_usage(progname);
559 switch (input_mode) {
562 defconfig_file = conf_get_default_confname();
563 if (conf_read(defconfig_file)) {
565 "*** Can't find default configuration \"%s\"!\n"
566 "***\n"), defconfig_file);
582 name = getenv("KCONFIG_ALLCONFIG");
585 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
586 if (conf_read_simple(name, S_DEF_USER)) {
588 _("*** Can't read seed configuration \"%s\"!\n"),
594 switch (input_mode) {
595 case allnoconfig: name = "allno.config"; break;
596 case allyesconfig: name = "allyes.config"; break;
597 case allmodconfig: name = "allmod.config"; break;
598 case alldefconfig: name = "alldef.config"; break;
599 case randconfig: name = "allrandom.config"; break;
602 if (conf_read_simple(name, S_DEF_USER) &&
603 conf_read_simple("all.config", S_DEF_USER)) {
605 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
614 valid_stdin = tty_stdio;
616 switch (input_mode) {
618 conf_set_all_new_symbols(def_no);
621 conf_set_all_new_symbols(def_yes);
624 conf_set_all_new_symbols(def_mod);
627 conf_set_all_new_symbols(def_default);
630 /* Really nothing to do in this loop */
631 while (conf_set_all_new_symbols(def_random)) ;
634 conf_set_all_new_symbols(def_default);
639 rootEntry = &rootmenu;
641 input_mode = oldconfig;
646 /* Update until a loop caused no more changes */
649 check_conf(&rootmenu);
651 (input_mode != listnewconfig &&
652 input_mode != oldnoconfig));
656 if (input_mode == savedefconfig) {
657 if (conf_write_defconfig(defconfig_file)) {
658 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
662 } else if (input_mode != listnewconfig) {
664 * build so we shall update autoconf.
666 if (conf_write(NULL)) {
667 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
670 if (conf_write_autoconf()) {
671 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));