1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
19 static void conf(struct menu *menu);
20 static void check_conf(struct menu *menu);
39 static enum input_mode input_mode = oldaskconfig;
41 static int indent = 1;
43 static int sync_kconfig;
45 static char line[PATH_MAX];
46 static struct menu *rootEntry;
48 static void print_help(struct menu *menu)
50 struct gstr help = str_new();
52 menu_get_ext_help(menu, &help);
54 printf("\n%s\n", str_get(&help));
58 static void strip(char *str)
67 memmove(str, p, l + 1);
75 /* Helper function to facilitate fgets() by Jean Sacren. */
76 static void xfgets(char *str, int size, FILE *in)
78 if (!fgets(str, size, in))
79 fprintf(stderr, "\nError in reading or end of file.\n");
85 static int conf_askvalue(struct symbol *sym, const char *def)
87 if (!sym_has_value(sym))
93 if (!sym_is_changeable(sym)) {
100 switch (input_mode) {
103 if (sym_has_value(sym)) {
110 xfgets(line, sizeof(line), stdin);
117 static int conf_string(struct menu *menu)
119 struct symbol *sym = menu->sym;
123 printf("%*s%s ", indent - 1, "", menu->prompt->text);
124 printf("(%s) ", sym->name);
125 def = sym_get_string_value(sym);
127 printf("[%s] ", def);
128 if (!conf_askvalue(sym, def))
135 if (line[1] == '\n') {
142 line[strlen(line)-1] = 0;
145 if (def && sym_set_string_value(sym, def))
150 static int conf_sym(struct menu *menu)
152 struct symbol *sym = menu->sym;
153 tristate oldval, newval;
156 printf("%*s%s ", indent - 1, "", menu->prompt->text);
158 printf("(%s) ", sym->name);
160 oldval = sym_get_tristate_value(sym);
172 if (oldval != no && sym_tristate_within_range(sym, no))
174 if (oldval != mod && sym_tristate_within_range(sym, mod))
176 if (oldval != yes && sym_tristate_within_range(sym, yes))
179 if (!conf_askvalue(sym, sym_get_string_value(sym)))
187 if (!line[1] || !strcmp(&line[1], "o"))
199 if (!line[1] || !strcmp(&line[1], "es"))
210 if (sym_set_tristate_value(sym, newval))
217 static int conf_choice(struct menu *menu)
219 struct symbol *sym, *def_sym;
224 is_new = !sym_has_value(sym);
225 if (sym_is_changeable(sym)) {
228 switch (sym_get_tristate_value(sym)) {
237 switch (sym_get_tristate_value(sym)) {
241 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
251 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
252 def_sym = sym_get_choice_value(sym);
255 for (child = menu->list; child; child = child->next) {
256 if (!menu_is_visible(child))
259 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
263 if (child->sym == def_sym) {
265 printf("%*c", indent, '>');
267 printf("%*c", indent, ' ');
268 printf(" %d. %s", cnt, menu_get_prompt(child));
269 if (child->sym->name)
270 printf(" (%s)", child->sym->name);
271 if (!sym_has_value(child->sym))
275 printf("%*schoice", indent - 1, "");
280 printf("[1-%d?]: ", cnt);
281 switch (input_mode) {
292 xfgets(line, sizeof(line), stdin);
294 if (line[0] == '?') {
300 else if (isdigit(line[0]))
310 for (child = menu->list; child; child = child->next) {
311 if (!child->sym || !menu_is_visible(child))
318 if (line[0] && line[strlen(line) - 1] == '?') {
322 sym_set_choice_value(sym, child->sym);
323 for (child = child->list; child; child = child->next) {
332 static void conf(struct menu *menu)
335 struct property *prop;
338 if (!menu_is_visible(menu))
346 switch (prop->type) {
349 * Except in oldaskconfig mode, we show only menus that
350 * contain new symbols.
352 if (input_mode != oldaskconfig && rootEntry != menu) {
358 prompt = menu_get_prompt(menu);
360 printf("%*c\n%*c %s\n%*c\n",
372 if (sym_is_choice(sym)) {
374 if (sym->curr.tri != mod)
393 for (child = menu->list; child; child = child->next)
399 static void check_conf(struct menu *menu)
404 if (!menu_is_visible(menu))
408 if (sym && !sym_has_value(sym) &&
409 (sym_is_changeable(sym) ||
410 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) {
412 switch (input_mode) {
417 if (sym->type == S_STRING) {
418 str = sym_get_string_value(sym);
419 str = sym_escape_string_value(str);
420 printf("%s%s=%s\n", CONFIG_, sym->name, str);
423 str = sym_get_string_value(sym);
424 printf("%s%s=%s\n", CONFIG_, sym->name, str);
435 printf("*\n* Restart config...\n*\n");
436 rootEntry = menu_get_parent_menu(menu);
442 for (child = menu->list; child; child = child->next)
446 static struct option long_opts[] = {
447 {"oldaskconfig", no_argument, NULL, oldaskconfig},
448 {"oldconfig", no_argument, NULL, oldconfig},
449 {"syncconfig", no_argument, NULL, syncconfig},
450 {"defconfig", required_argument, NULL, defconfig},
451 {"savedefconfig", required_argument, NULL, savedefconfig},
452 {"allnoconfig", no_argument, NULL, allnoconfig},
453 {"allyesconfig", no_argument, NULL, allyesconfig},
454 {"allmodconfig", no_argument, NULL, allmodconfig},
455 {"alldefconfig", no_argument, NULL, alldefconfig},
456 {"randconfig", no_argument, NULL, randconfig},
457 {"listnewconfig", no_argument, NULL, listnewconfig},
458 {"helpnewconfig", no_argument, NULL, helpnewconfig},
459 {"olddefconfig", no_argument, NULL, olddefconfig},
460 {"yes2modconfig", no_argument, NULL, yes2modconfig},
461 {"mod2yesconfig", no_argument, NULL, mod2yesconfig},
465 static void conf_usage(const char *progname)
468 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
469 printf("[option] is _one_ of the following:\n");
470 printf(" --listnewconfig List new options\n");
471 printf(" --helpnewconfig List new options and help text\n");
472 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
473 printf(" --oldconfig Update a configuration using a provided .config as base\n");
474 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
475 " include/{generated/,config/}\n");
476 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\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");
484 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
485 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
486 printf(" (If none of the above is given, --oldaskconfig is the default)\n");
489 int main(int ac, char **av)
491 const char *progname = av[0];
493 const char *name, *defconfig_file = NULL /* gcc uninit */;
494 int no_conf_write = 0;
496 tty_stdio = isatty(0) && isatty(1);
498 while ((opt = getopt_long(ac, av, "hs", long_opts, NULL)) != -1) {
500 conf_set_message_callback(NULL);
503 input_mode = (enum input_mode)opt;
507 * syncconfig is invoked during the build stage.
508 * Suppress distracting "configuration written to ..."
510 conf_set_message_callback(NULL);
515 defconfig_file = optarg;
524 * Use microseconds derived seed,
525 * compensate for systems where it may be zero
527 gettimeofday(&now, NULL);
528 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
530 seed_env = getenv("KCONFIG_SEED");
531 if( seed_env && *seed_env ) {
533 int tmp = (int)strtol(seed_env, &endp, 0);
538 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
555 conf_usage(progname);
561 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
562 conf_usage(progname);
569 switch (input_mode) {
571 if (conf_read(defconfig_file)) {
574 "*** Can't find default configuration \"%s\"!\n"
596 name = getenv("KCONFIG_ALLCONFIG");
599 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
600 if (conf_read_simple(name, S_DEF_USER)) {
602 "*** Can't read seed configuration \"%s\"!\n",
608 switch (input_mode) {
609 case allnoconfig: name = "allno.config"; break;
610 case allyesconfig: name = "allyes.config"; break;
611 case allmodconfig: name = "allmod.config"; break;
612 case alldefconfig: name = "alldef.config"; break;
613 case randconfig: name = "allrandom.config"; break;
616 if (conf_read_simple(name, S_DEF_USER) &&
617 conf_read_simple("all.config", S_DEF_USER)) {
619 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
629 name = getenv("KCONFIG_NOSILENTUPDATE");
631 if (conf_get_changed()) {
633 "\n*** The configuration requires explicit update.\n\n");
640 switch (input_mode) {
642 conf_set_all_new_symbols(def_no);
645 conf_set_all_new_symbols(def_yes);
648 conf_set_all_new_symbols(def_mod);
651 conf_set_all_new_symbols(def_default);
654 /* Really nothing to do in this loop */
655 while (conf_set_all_new_symbols(def_random)) ;
658 conf_set_all_new_symbols(def_default);
663 conf_rewrite_mod_or_yes(def_y2m);
666 conf_rewrite_mod_or_yes(def_m2y);
669 rootEntry = &rootmenu;
671 input_mode = oldconfig;
677 /* Update until a loop caused no more changes */
680 check_conf(&rootmenu);
688 if (input_mode == savedefconfig) {
689 if (conf_write_defconfig(defconfig_file)) {
690 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
694 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
695 if (!no_conf_write && conf_write(NULL)) {
696 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
701 * Create auto.conf if it does not exist.
702 * This prevents GNU Make 4.1 or older from emitting
703 * "include/config/auto.conf: No such file or directory"
704 * in the top-level Makefile
706 * syncconfig always creates or updates auto.conf because it is
707 * used during the build.
709 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
711 "\n*** Error during sync of the configuration.\n\n");