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");
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)) {
108 xfgets(line, sizeof(line), stdin);
127 static int conf_string(struct menu *menu)
129 struct symbol *sym = menu->sym;
133 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
134 printf("(%s) ", sym->name);
135 def = sym_get_string_value(sym);
136 if (sym_get_string_value(sym))
137 printf("[%s] ", def);
138 if (!conf_askvalue(sym, def))
145 if (line[1] == '\n') {
152 line[strlen(line)-1] = 0;
155 if (def && sym_set_string_value(sym, def))
160 static int conf_sym(struct menu *menu)
162 struct symbol *sym = menu->sym;
163 tristate oldval, newval;
166 printf("%*s%s ", indent - 1, "", _(menu->prompt->text));
168 printf("(%s) ", sym->name);
170 oldval = sym_get_tristate_value(sym);
182 if (oldval != no && sym_tristate_within_range(sym, no))
184 if (oldval != mod && sym_tristate_within_range(sym, mod))
186 if (oldval != yes && sym_tristate_within_range(sym, yes))
189 if (!conf_askvalue(sym, sym_get_string_value(sym)))
197 if (!line[1] || !strcmp(&line[1], "o"))
209 if (!line[1] || !strcmp(&line[1], "es"))
220 if (sym_set_tristate_value(sym, newval))
227 static int conf_choice(struct menu *menu)
229 struct symbol *sym, *def_sym;
234 is_new = !sym_has_value(sym);
235 if (sym_is_changable(sym)) {
238 switch (sym_get_tristate_value(sym)) {
247 switch (sym_get_tristate_value(sym)) {
251 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
261 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
262 def_sym = sym_get_choice_value(sym);
265 for (child = menu->list; child; child = child->next) {
266 if (!menu_is_visible(child))
269 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
273 if (child->sym == def_sym) {
275 printf("%*c", indent, '>');
277 printf("%*c", indent, ' ');
278 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
279 if (child->sym->name)
280 printf(" (%s)", child->sym->name);
281 if (!sym_has_value(child->sym))
285 printf(_("%*schoice"), indent - 1, "");
290 printf("[1-%d?]: ", cnt);
291 switch (input_mode) {
301 xfgets(line, sizeof(line), stdin);
303 if (line[0] == '?') {
309 else if (isdigit(line[0]))
319 for (child = menu->list; child; child = child->next) {
320 if (!child->sym || !menu_is_visible(child))
327 if (line[0] && line[strlen(line) - 1] == '?') {
331 sym_set_choice_value(sym, child->sym);
332 for (child = child->list; child; child = child->next) {
341 static void conf(struct menu *menu)
344 struct property *prop;
347 if (!menu_is_visible(menu))
355 switch (prop->type) {
357 if ((input_mode == listnewconfig ||
358 input_mode == oldnoconfig) &&
365 prompt = menu_get_prompt(menu);
367 printf("%*c\n%*c %s\n%*c\n",
369 indent, '*', _(prompt),
379 if (sym_is_choice(sym)) {
381 if (sym->curr.tri != mod)
400 for (child = menu->list; child; child = child->next)
406 static void check_conf(struct menu *menu)
411 if (!menu_is_visible(menu))
415 if (sym && !sym_has_value(sym)) {
416 if (sym_is_changable(sym) ||
417 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
418 if (input_mode == listnewconfig) {
419 if (sym->name && !sym_is_choice_value(sym)) {
420 printf("%s%s\n", CONFIG_, sym->name);
422 } else if (input_mode != oldnoconfig) {
424 printf(_("*\n* Restart config...\n*\n"));
425 rootEntry = menu_get_parent_menu(menu);
431 for (child = menu->list; child; child = child->next)
435 static struct option long_opts[] = {
436 {"askconfig", no_argument, NULL, oldaskconfig},
437 {"config", no_argument, NULL, oldconfig},
438 {"defconfig", optional_argument, NULL, defconfig},
439 {"savedefconfig", required_argument, NULL, savedefconfig},
440 {"allnoconfig", no_argument, NULL, allnoconfig},
441 {"allyesconfig", no_argument, NULL, allyesconfig},
442 {"allmodconfig", no_argument, NULL, allmodconfig},
443 {"alldefconfig", no_argument, NULL, alldefconfig},
444 {"randconfig", no_argument, NULL, randconfig},
445 {"listnewconfig", no_argument, NULL, listnewconfig},
446 {"noconfig", no_argument, NULL, oldnoconfig},
450 static void conf_usage(const char *progname)
453 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
454 printf("[option] is _one_ of the following:\n");
455 printf(" --listnewconfig List new options\n");
456 printf(" --askconfig Start a new configuration using a line-oriented program\n");
457 printf(" --config Update a configuration using a provided .config as base\n");
458 printf(" --silentconfig Same as config, but quietly, additionally update deps\n");
459 printf(" --noconfig Same as silentconfig but set new symbols to no\n");
460 printf(" --defconfig <file> New config with default defined in <file>\n");
461 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
462 printf(" --allnoconfig New config where all options are answered with no\n");
463 printf(" --allyesconfig New config where all options are answered with yes\n");
464 printf(" --allmodconfig New config where all options are answered with mod\n");
465 printf(" --alldefconfig New config with all symbols set to default\n");
466 printf(" --randconfig New config with random answer to all options\n");
469 int main(int ac, char **av)
471 const char *progname = av[0];
473 const char *name, *defconfig_file = NULL /* gcc uninit */;
476 setlocale(LC_ALL, "");
477 bindtextdomain(PACKAGE, LOCALEDIR);
480 tty_stdio = isatty(0) && isatty(1);
482 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
484 conf_set_message_callback(NULL);
487 input_mode = (enum input_mode)opt;
491 defconfig_file = optarg;
500 * Use microseconds derived seed,
501 * compensate for systems where it may be zero
503 gettimeofday(&now, NULL);
504 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
506 seed_env = getenv("KCONFIG_SEED");
507 if( seed_env && *seed_env ) {
509 int tmp = (int)strtol(seed_env, &endp, 0);
514 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
528 conf_usage(progname);
534 fprintf(stderr, _("%s: Kconfig file missing\n"), av[0]);
535 conf_usage(progname);
542 switch (input_mode) {
545 defconfig_file = conf_get_default_confname();
546 if (conf_read(defconfig_file)) {
549 "*** Can't find default configuration \"%s\"!\n"
567 name = getenv("KCONFIG_ALLCONFIG");
570 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
571 if (conf_read_simple(name, S_DEF_USER)) {
573 _("*** Can't read seed configuration \"%s\"!\n"),
579 switch (input_mode) {
580 case allnoconfig: name = "allno.config"; break;
581 case allyesconfig: name = "allyes.config"; break;
582 case allmodconfig: name = "allmod.config"; break;
583 case alldefconfig: name = "alldef.config"; break;
584 case randconfig: name = "allrandom.config"; break;
587 if (conf_read_simple(name, S_DEF_USER) &&
588 conf_read_simple("all.config", S_DEF_USER)) {
590 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
599 switch (input_mode) {
601 conf_set_all_new_symbols(def_no);
604 conf_set_all_new_symbols(def_yes);
607 conf_set_all_new_symbols(def_mod);
610 conf_set_all_new_symbols(def_default);
613 /* Really nothing to do in this loop */
614 while (conf_set_all_new_symbols(def_random)) ;
617 conf_set_all_new_symbols(def_default);
622 rootEntry = &rootmenu;
624 input_mode = oldconfig;
629 /* Update until a loop caused no more changes */
632 check_conf(&rootmenu);
634 (input_mode != listnewconfig &&
635 input_mode != oldnoconfig));
639 if (input_mode == savedefconfig) {
640 if (conf_write_defconfig(defconfig_file)) {
641 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
645 } else if (input_mode != listnewconfig) {
647 * build so we shall update autoconf.
649 if (conf_write(NULL)) {
650 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
653 if (conf_write_autoconf()) {
654 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));