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;
42 static char line[PATH_MAX];
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 /* Helper function to facilitate fgets() by Jean Sacren. */
73 static void xfgets(char *str, int size, FILE *in)
75 if (!fgets(str, size, in))
76 fprintf(stderr, "\nError in reading or end of file.\n");
79 static int conf_askvalue(struct symbol *sym, const char *def)
81 enum symbol_type type = sym_get_type(sym);
83 if (!sym_has_value(sym))
89 if (!sym_is_changable(sym)) {
98 if (sym_has_value(sym)) {
105 xfgets(line, sizeof(line), stdin);
126 static int conf_string(struct menu *menu)
128 struct symbol *sym = menu->sym;
132 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
133 printf("(%s) ", sym->name);
134 def = sym_get_string_value(sym);
135 if (sym_get_string_value(sym))
136 printf("[%s] ", def);
137 if (!conf_askvalue(sym, def))
144 if (line[1] == '\n') {
151 line[strlen(line)-1] = 0;
154 if (def && sym_set_string_value(sym, def))
159 static int conf_sym(struct menu *menu)
161 struct symbol *sym = menu->sym;
162 tristate oldval, newval;
165 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
167 printf("(%s) ", sym->name);
169 oldval = sym_get_tristate_value(sym);
181 if (oldval != no && sym_tristate_within_range(sym, no))
183 if (oldval != mod && sym_tristate_within_range(sym, mod))
185 if (oldval != yes && sym_tristate_within_range(sym, yes))
188 if (!conf_askvalue(sym, sym_get_string_value(sym)))
196 if (!line[1] || !strcmp(&line[1], "o"))
208 if (!line[1] || !strcmp(&line[1], "es"))
219 if (sym_set_tristate_value(sym, newval))
226 static int conf_choice(struct menu *menu)
228 struct symbol *sym, *def_sym;
233 is_new = !sym_has_value(sym);
234 if (sym_is_changable(sym)) {
237 switch (sym_get_tristate_value(sym)) {
246 switch (sym_get_tristate_value(sym)) {
250 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
260 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
261 def_sym = sym_get_choice_value(sym);
264 for (child = menu->list; child; child = child->next) {
265 if (!menu_is_visible(child))
268 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
272 if (child->sym == def_sym) {
274 printf("%*c", indent, '>');
276 printf("%*c", indent, ' ');
277 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
278 if (child->sym->name)
279 printf(" (%s)", child->sym->name);
280 if (!sym_has_value(child->sym))
284 printf(_("%*schoice"), indent - 1, "");
289 printf("[1-%d?]: ", cnt);
290 switch (input_mode) {
300 xfgets(line, sizeof(line), stdin);
302 if (line[0] == '?') {
308 else if (isdigit(line[0]))
318 for (child = menu->list; child; child = child->next) {
319 if (!child->sym || !menu_is_visible(child))
326 if (line[0] && line[strlen(line) - 1] == '?') {
330 sym_set_choice_value(sym, child->sym);
331 for (child = child->list; child; child = child->next) {
340 static void conf(struct menu *menu)
343 struct property *prop;
346 if (!menu_is_visible(menu))
354 switch (prop->type) {
356 if ((input_mode == listnewconfig ||
357 input_mode == oldnoconfig) &&
364 prompt = menu_get_prompt(menu);
366 printf("%*c\n%*c %s\n%*c\n",
368 indent, '*', _(prompt),
378 if (sym_is_choice(sym)) {
380 if (sym->curr.tri != mod)
399 for (child = menu->list; child; child = child->next)
405 static void check_conf(struct menu *menu)
410 if (!menu_is_visible(menu))
414 if (sym && !sym_has_value(sym)) {
415 if (sym_is_changable(sym) ||
416 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
417 if (input_mode == listnewconfig) {
418 if (sym->name && !sym_is_choice_value(sym)) {
419 printf("%s%s\n", CONFIG_, sym->name);
421 } else if (input_mode != oldnoconfig) {
423 printf(_("*\n* Restart config...\n*\n"));
424 rootEntry = menu_get_parent_menu(menu);
430 for (child = menu->list; child; child = child->next)
434 static struct option long_opts[] = {
435 {"askconfig", no_argument, NULL, oldaskconfig},
436 {"config", no_argument, NULL, oldconfig},
437 {"defconfig", optional_argument, NULL, defconfig},
438 {"savedefconfig", required_argument, NULL, savedefconfig},
439 {"allnoconfig", no_argument, NULL, allnoconfig},
440 {"allyesconfig", no_argument, NULL, allyesconfig},
441 {"allmodconfig", no_argument, NULL, allmodconfig},
442 {"alldefconfig", no_argument, NULL, alldefconfig},
443 {"randconfig", no_argument, NULL, randconfig},
444 {"listnewconfig", no_argument, NULL, listnewconfig},
445 {"noconfig", no_argument, NULL, oldnoconfig},
449 static void conf_usage(const char *progname)
452 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
453 printf("[option] is _one_ of the following:\n");
454 printf(" --listnewconfig List new options\n");
455 printf(" --askconfig Start a new configuration using a line-oriented program\n");
456 printf(" --config Update a configuration using a provided .config as base\n");
457 printf(" --silentconfig Same as config, but quietly, additionally update deps\n");
458 printf(" --noconfig Same as silentconfig but set new symbols to no\n");
459 printf(" --defconfig <file> New config with default defined in <file>\n");
460 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
461 printf(" --allnoconfig New config where all options are answered with no\n");
462 printf(" --allyesconfig New config where all options are answered with yes\n");
463 printf(" --allmodconfig New config where all options are answered with mod\n");
464 printf(" --alldefconfig New config with all symbols set to default\n");
465 printf(" --randconfig New config with random answer to all options\n");
468 int main(int ac, char **av)
470 const char *progname = av[0];
472 const char *name, *defconfig_file = NULL /* gcc uninit */;
475 setlocale(LC_ALL, "");
476 bindtextdomain(PACKAGE, LOCALEDIR);
479 tty_stdio = isatty(0) && isatty(1) && isatty(2);
481 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
483 conf_set_message_callback(NULL);
486 input_mode = (enum input_mode)opt;
490 defconfig_file = optarg;
499 * Use microseconds derived seed,
500 * compensate for systems where it may be zero
502 gettimeofday(&now, NULL);
503 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
505 seed_env = getenv("KCONFIG_SEED");
506 if( seed_env && *seed_env ) {
508 int tmp = (int)strtol(seed_env, &endp, 0);
513 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
527 conf_usage(progname);
533 printf(_("%s: Kconfig file missing\n"), av[0]);
534 conf_usage(progname);
541 switch (input_mode) {
544 defconfig_file = conf_get_default_confname();
545 if (conf_read(defconfig_file)) {
547 "*** Can't find default configuration \"%s\"!\n"
548 "***\n"), defconfig_file);
564 name = getenv("KCONFIG_ALLCONFIG");
567 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
568 if (conf_read_simple(name, S_DEF_USER)) {
570 _("*** Can't read seed configuration \"%s\"!\n"),
576 switch (input_mode) {
577 case allnoconfig: name = "allno.config"; break;
578 case allyesconfig: name = "allyes.config"; break;
579 case allmodconfig: name = "allmod.config"; break;
580 case alldefconfig: name = "alldef.config"; break;
581 case randconfig: name = "allrandom.config"; break;
584 if (conf_read_simple(name, S_DEF_USER) &&
585 conf_read_simple("all.config", S_DEF_USER)) {
587 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
596 switch (input_mode) {
598 conf_set_all_new_symbols(def_no);
601 conf_set_all_new_symbols(def_yes);
604 conf_set_all_new_symbols(def_mod);
607 conf_set_all_new_symbols(def_default);
610 /* Really nothing to do in this loop */
611 while (conf_set_all_new_symbols(def_random)) ;
614 conf_set_all_new_symbols(def_default);
619 rootEntry = &rootmenu;
621 input_mode = oldconfig;
626 /* Update until a loop caused no more changes */
629 check_conf(&rootmenu);
631 (input_mode != listnewconfig &&
632 input_mode != oldnoconfig));
636 if (input_mode == savedefconfig) {
637 if (conf_write_defconfig(defconfig_file)) {
638 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
642 } else if (input_mode != listnewconfig) {
644 * build so we shall update autoconf.
646 if (conf_write(NULL)) {
647 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
650 if (conf_write_autoconf()) {
651 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));