2 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
3 * Released under the terms of the GNU GPL v2.0.
19 static void conf(struct menu *menu);
20 static void check_conf(struct menu *menu);
21 static void xfgets(char *str, int size, FILE *in);
35 } input_mode = oldaskconfig;
37 static int indent = 1;
38 static int valid_stdin = 1;
40 static char line[128];
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 static void check_stdin(void)
73 printf(_("aborted!\n\n"));
74 printf(_("Console input/output is redirected. "));
75 printf(_("Run 'make config' to update configuration.\n\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)) {
107 xfgets(line, 128, 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))
187 if (menu_has_help(menu))
190 if (!conf_askvalue(sym, sym_get_string_value(sym)))
198 if (!line[1] || !strcmp(&line[1], "o"))
210 if (!line[1] || !strcmp(&line[1], "es"))
221 if (sym_set_tristate_value(sym, newval))
228 static int conf_choice(struct menu *menu)
230 struct symbol *sym, *def_sym;
235 is_new = !sym_has_value(sym);
236 if (sym_is_changable(sym)) {
239 switch (sym_get_tristate_value(sym)) {
248 switch (sym_get_tristate_value(sym)) {
252 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
262 printf("%*s%s\n", indent - 1, "", _(menu_get_prompt(menu)));
263 def_sym = sym_get_choice_value(sym);
266 for (child = menu->list; child; child = child->next) {
267 if (!menu_is_visible(child))
270 printf("%*c %s\n", indent, '*', _(menu_get_prompt(child)));
274 if (child->sym == def_sym) {
276 printf("%*c", indent, '>');
278 printf("%*c", indent, ' ');
279 printf(" %d. %s", cnt, _(menu_get_prompt(child)));
280 if (child->sym->name)
281 printf(" (%s)", child->sym->name);
282 if (!sym_has_value(child->sym))
286 printf(_("%*schoice"), indent - 1, "");
291 printf("[1-%d", cnt);
292 if (menu_has_help(menu))
295 switch (input_mode) {
306 xfgets(line, 128, stdin);
308 if (line[0] == '?') {
314 else if (isdigit(line[0]))
324 for (child = menu->list; child; child = child->next) {
325 if (!child->sym || !menu_is_visible(child))
332 if (line[0] && line[strlen(line) - 1] == '?') {
336 sym_set_choice_value(sym, child->sym);
337 for (child = child->list; child; child = child->next) {
346 static void conf(struct menu *menu)
349 struct property *prop;
352 if (!menu_is_visible(menu))
360 switch (prop->type) {
362 if ((input_mode == listnewconfig ||
363 input_mode == oldnoconfig) &&
370 prompt = menu_get_prompt(menu);
372 printf("%*c\n%*c %s\n%*c\n",
374 indent, '*', _(prompt),
384 if (sym_is_choice(sym)) {
386 if (sym->curr.tri != mod)
405 for (child = menu->list; child; child = child->next)
411 static void check_conf(struct menu *menu)
416 if (!menu_is_visible(menu))
420 if (sym && !sym_has_value(sym)) {
421 if (sym_is_changable(sym) ||
422 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
423 if (input_mode == listnewconfig) {
424 if (sym->name && !sym_is_choice_value(sym)) {
425 printf("%s%s\n", CONFIG_, sym->name);
427 } else if (input_mode != oldnoconfig) {
429 printf(_("*\n* Restart config...\n*\n"));
430 rootEntry = menu_get_parent_menu(menu);
436 for (child = menu->list; child; child = child->next)
440 static struct option long_opts[] = {
441 {"askconfig", no_argument, NULL, oldaskconfig},
442 {"config", no_argument, NULL, oldconfig},
443 {"defconfig", optional_argument, NULL, defconfig},
444 {"savedefconfig", required_argument, NULL, savedefconfig},
445 {"allnoconfig", no_argument, NULL, allnoconfig},
446 {"allyesconfig", no_argument, NULL, allyesconfig},
447 {"allmodconfig", no_argument, NULL, allmodconfig},
448 {"alldefconfig", no_argument, NULL, alldefconfig},
449 {"randconfig", no_argument, NULL, randconfig},
450 {"listnewconfig", no_argument, NULL, listnewconfig},
451 {"noconfig", no_argument, NULL, oldnoconfig},
455 static void conf_usage(const char *progname)
458 printf("Usage: %s [option] <kconfig-file>\n", progname);
459 printf("[option] is _one_ of the following:\n");
460 printf(" --listnewconfig List new options\n");
461 printf(" --askconfig Start a new configuration using a line-oriented program\n");
462 printf(" --config Update a configuration using a provided .config as base\n");
463 printf(" --silentconfig Same as config, but quietly, additionally update deps\n");
464 printf(" --noconfig Same as silentconfig but set new symbols to no\n");
465 printf(" --defconfig <file> New config with default defined in <file>\n");
466 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
467 printf(" --allnoconfig New config where all options are answered with no\n");
468 printf(" --allyesconfig New config where all options are answered with yes\n");
469 printf(" --allmodconfig New config where all options are answered with mod\n");
470 printf(" --alldefconfig New config with all symbols set to default\n");
471 printf(" --randconfig New config with random answer to all options\n");
474 int main(int ac, char **av)
476 const char *progname = av[0];
478 const char *name, *defconfig_file = NULL /* gcc uninit */;
481 setlocale(LC_ALL, "");
482 bindtextdomain(PACKAGE, LOCALEDIR);
485 while ((opt = getopt_long(ac, av, "", long_opts, NULL)) != -1) {
486 input_mode = (enum input_mode)opt;
490 defconfig_file = optarg;
498 * Use microseconds derived seed,
499 * compensate for systems where it may be zero
501 gettimeofday(&now, NULL);
503 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
517 conf_usage(progname);
523 printf(_("%s: Kconfig file missing\n"), av[0]);
524 conf_usage(progname);
531 switch (input_mode) {
534 defconfig_file = conf_get_default_confname();
535 if (conf_read(defconfig_file)) {
537 "*** Can't find default configuration \"%s\"!\n"
538 "***\n"), defconfig_file);
554 name = getenv("KCONFIG_ALLCONFIG");
557 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
558 if (conf_read_simple(name, S_DEF_USER)) {
560 _("*** Can't read seed configuration \"%s\"!\n"),
566 switch (input_mode) {
567 case allnoconfig: name = "allno.config"; break;
568 case allyesconfig: name = "allyes.config"; break;
569 case allmodconfig: name = "allmod.config"; break;
570 case alldefconfig: name = "alldef.config"; break;
571 case randconfig: name = "allrandom.config"; break;
574 if (conf_read_simple(name, S_DEF_USER) &&
575 conf_read_simple("all.config", S_DEF_USER)) {
577 _("*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n"),
586 valid_stdin = isatty(0) && isatty(1) && isatty(2);
588 switch (input_mode) {
590 conf_set_all_new_symbols(def_no);
593 conf_set_all_new_symbols(def_yes);
596 conf_set_all_new_symbols(def_mod);
599 conf_set_all_new_symbols(def_default);
602 conf_set_all_new_symbols(def_random);
605 conf_set_all_new_symbols(def_default);
610 rootEntry = &rootmenu;
612 input_mode = oldconfig;
617 /* Update until a loop caused no more changes */
620 check_conf(&rootmenu);
622 (input_mode != listnewconfig &&
623 input_mode != oldnoconfig));
627 if (input_mode == savedefconfig) {
628 if (conf_write_defconfig(defconfig_file)) {
629 fprintf(stderr, _("n*** Error while saving defconfig to: %s\n\n"),
633 } else if (input_mode != listnewconfig) {
635 * build so we shall update autoconf.
637 if (conf_write(NULL)) {
638 fprintf(stderr, _("\n*** Error during writing of the configuration.\n\n"));
641 if (conf_write_autoconf()) {
642 fprintf(stderr, _("\n*** Error during update of the configuration.\n\n"));
650 * Helper function to facilitate fgets() by Jean Sacren.
652 void xfgets(char *str, int size, FILE *in)
654 if (fgets(str, size, in) == NULL)
655 fprintf(stderr, "\nError in reading or end of file.\n");