2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
20 static void conf(struct menu *menu);
21 static void check_conf(struct menu *menu);
35 static enum input_mode input_mode = oldaskconfig;
37 static int indent = 1;
40 static char line[PATH_MAX];
41 static struct menu *rootEntry;
43 static void print_help(struct menu *menu)
45 struct gstr help = str_new();
47 menu_get_ext_help(menu, &help);
49 printf("\n%s\n", str_get(&help));
53 static void strip(char *str)
62 memmove(str, p, l + 1);
70 /* Helper function to facilitate fgets() by Jean Sacren. */
71 static void xfgets(char *str, int size, FILE *in)
73 if (!fgets(str, size, in))
74 fprintf(stderr, "\nError in reading or end of file.\n");
80 static int conf_askvalue(struct symbol *sym, const char *def)
82 enum symbol_type type = sym_get_type(sym);
84 if (!sym_has_value(sym))
90 if (!sym_is_changable(sym)) {
99 if (sym_has_value(sym)) {
106 xfgets(line, sizeof(line), stdin);
125 static int conf_string(struct menu *menu)
127 struct symbol *sym = menu->sym;
131 printf("%*s%s ", indent - 1, "", menu->prompt->text);
132 printf("(%s) ", sym->name);
133 def = sym_get_string_value(sym);
134 if (sym_get_string_value(sym))
135 printf("[%s] ", def);
136 if (!conf_askvalue(sym, def))
143 if (line[1] == '\n') {
150 line[strlen(line)-1] = 0;
153 if (def && sym_set_string_value(sym, def))
158 static int conf_sym(struct menu *menu)
160 struct symbol *sym = menu->sym;
161 tristate oldval, newval;
164 printf("%*s%s ", indent - 1, "", menu->prompt->text);
166 printf("(%s) ", sym->name);
168 oldval = sym_get_tristate_value(sym);
180 if (oldval != no && sym_tristate_within_range(sym, no))
182 if (oldval != mod && sym_tristate_within_range(sym, mod))
184 if (oldval != yes && sym_tristate_within_range(sym, yes))
187 if (!conf_askvalue(sym, sym_get_string_value(sym)))
195 if (!line[1] || !strcmp(&line[1], "o"))
207 if (!line[1] || !strcmp(&line[1], "es"))
218 if (sym_set_tristate_value(sym, newval))
225 static int conf_choice(struct menu *menu)
227 struct symbol *sym, *def_sym;
232 is_new = !sym_has_value(sym);
233 if (sym_is_changable(sym)) {
236 switch (sym_get_tristate_value(sym)) {
245 switch (sym_get_tristate_value(sym)) {
249 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
259 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
260 def_sym = sym_get_choice_value(sym);
263 for (child = menu->list; child; child = child->next) {
264 if (!menu_is_visible(child))
267 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
271 if (child->sym == def_sym) {
273 printf("%*c", indent, '>');
275 printf("%*c", indent, ' ');
276 printf(" %d. %s", cnt, menu_get_prompt(child));
277 if (child->sym->name)
278 printf(" (%s)", child->sym->name);
279 if (!sym_has_value(child->sym))
283 printf("%*schoice", indent - 1, "");
288 printf("[1-%d?]: ", cnt);
289 switch (input_mode) {
299 xfgets(line, sizeof(line), stdin);
301 if (line[0] == '?') {
307 else if (isdigit(line[0]))
317 for (child = menu->list; child; child = child->next) {
318 if (!child->sym || !menu_is_visible(child))
325 if (line[0] && line[strlen(line) - 1] == '?') {
329 sym_set_choice_value(sym, child->sym);
330 for (child = child->list; child; child = child->next) {
339 static void conf(struct menu *menu)
342 struct property *prop;
345 if (!menu_is_visible(menu))
353 switch (prop->type) {
356 * Except in oldaskconfig mode, we show only menus that
357 * contain new symbols.
359 if (input_mode != oldaskconfig && rootEntry != menu) {
365 prompt = menu_get_prompt(menu);
367 printf("%*c\n%*c %s\n%*c\n",
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) {
422 if (sym->type == S_STRING) {
423 str = sym_get_string_value(sym);
424 str = sym_escape_string_value(str);
425 printf("%s%s=%s\n", CONFIG_, sym->name, str);
428 str = sym_get_string_value(sym);
429 printf("%s%s=%s\n", CONFIG_, sym->name, str);
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},
459 static void conf_usage(const char *progname)
462 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
463 printf("[option] is _one_ of the following:\n");
464 printf(" --listnewconfig List new options\n");
465 printf(" --askconfig Start a new configuration using a line-oriented program\n");
466 printf(" --config Update a configuration using a provided .config as base\n");
467 printf(" --silentconfig Same as config, but quietly, additionally update deps\n");
468 printf(" --noconfig Same as silentconfig but set new symbols to no\n");
469 printf(" --defconfig <file> New config with default defined in <file>\n");
470 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
471 printf(" --allnoconfig New config where all options are answered with no\n");
472 printf(" --allyesconfig New config where all options are answered with yes\n");
473 printf(" --allmodconfig New config where all options are answered with mod\n");
474 printf(" --alldefconfig New config with all symbols set to default\n");
475 printf(" --randconfig New config with random answer to all options\n");
478 int main(int ac, char **av)
480 const char *progname = av[0];
482 const char *name, *defconfig_file = NULL /* gcc uninit */;
484 int no_conf_write = 0;
486 tty_stdio = isatty(0) && isatty(1);
488 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
490 conf_set_message_callback(NULL);
493 input_mode = (enum input_mode)opt;
497 * syncconfig is invoked during the build stage.
498 * Suppress distracting "configuration written to ..."
500 conf_set_message_callback(NULL);
505 defconfig_file = optarg;
514 * Use microseconds derived seed,
515 * compensate for systems where it may be zero
517 gettimeofday(&now, NULL);
518 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
520 seed_env = getenv("KCONFIG_SEED");
521 if( seed_env && *seed_env ) {
523 int tmp = (int)strtol(seed_env, &endp, 0);
528 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
541 conf_usage(progname);
547 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
548 conf_usage(progname);
555 switch (input_mode) {
558 defconfig_file = conf_get_default_confname();
559 if (conf_read(defconfig_file)) {
562 "*** Can't find default configuration \"%s\"!\n"
579 name = getenv("KCONFIG_ALLCONFIG");
582 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
583 if (conf_read_simple(name, S_DEF_USER)) {
585 "*** Can't read seed configuration \"%s\"!\n",
591 switch (input_mode) {
592 case allnoconfig: name = "allno.config"; break;
593 case allyesconfig: name = "allyes.config"; break;
594 case allmodconfig: name = "allmod.config"; break;
595 case alldefconfig: name = "alldef.config"; break;
596 case randconfig: name = "allrandom.config"; break;
599 if (conf_read_simple(name, S_DEF_USER) &&
600 conf_read_simple("all.config", S_DEF_USER)) {
602 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
611 switch (input_mode) {
613 conf_set_all_new_symbols(def_no);
616 conf_set_all_new_symbols(def_yes);
619 conf_set_all_new_symbols(def_mod);
622 conf_set_all_new_symbols(def_default);
625 /* Really nothing to do in this loop */
626 while (conf_set_all_new_symbols(def_random)) ;
629 conf_set_all_new_symbols(def_default);
634 rootEntry = &rootmenu;
636 input_mode = oldconfig;
640 /* Update until a loop caused no more changes */
643 check_conf(&rootmenu);
648 if (input_mode == savedefconfig) {
649 if (conf_write_defconfig(defconfig_file)) {
650 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
654 } else if (input_mode != listnewconfig) {
656 * build so we shall update autoconf.
658 if (!no_conf_write && conf_write(NULL)) {
659 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
662 if (conf_write_autoconf()) {
663 fprintf(stderr, "\n*** Error during update of the configuration.\n\n");