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;
40 static int input_mode_opt;
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 void set_randconfig_seed(void)
89 bool seed_set = false;
91 env = getenv("KCONFIG_SEED");
95 seed = strtol(env, &endp, 0);
104 * Use microseconds derived seed, compensate for systems where it may
107 gettimeofday(&now, NULL);
108 seed = (now.tv_sec + 1) * (now.tv_usec + 1);
111 printf("KCONFIG_SEED=0x%X\n", seed);
115 static void conf_rewrite_mod_or_yes(enum conf_def_mode mode)
119 tristate old_val = (mode == def_y2m) ? yes : mod;
120 tristate new_val = (mode == def_y2m) ? mod : yes;
122 for_all_symbols(i, sym) {
123 if (sym_get_type(sym) == S_TRISTATE &&
124 sym->def[S_DEF_USER].tri == old_val)
125 sym->def[S_DEF_USER].tri = new_val;
127 sym_clear_all_valid();
130 static int conf_askvalue(struct symbol *sym, const char *def)
132 if (!sym_has_value(sym))
138 if (!sym_is_changeable(sym)) {
145 switch (input_mode) {
148 if (sym_has_value(sym)) {
155 xfgets(line, sizeof(line), stdin);
162 static int conf_string(struct menu *menu)
164 struct symbol *sym = menu->sym;
168 printf("%*s%s ", indent - 1, "", menu->prompt->text);
169 printf("(%s) ", sym->name);
170 def = sym_get_string_value(sym);
172 printf("[%s] ", def);
173 if (!conf_askvalue(sym, def))
180 if (line[1] == '\n') {
187 line[strlen(line)-1] = 0;
190 if (def && sym_set_string_value(sym, def))
195 static int conf_sym(struct menu *menu)
197 struct symbol *sym = menu->sym;
198 tristate oldval, newval;
201 printf("%*s%s ", indent - 1, "", menu->prompt->text);
203 printf("(%s) ", sym->name);
205 oldval = sym_get_tristate_value(sym);
217 if (oldval != no && sym_tristate_within_range(sym, no))
219 if (oldval != mod && sym_tristate_within_range(sym, mod))
221 if (oldval != yes && sym_tristate_within_range(sym, yes))
224 if (!conf_askvalue(sym, sym_get_string_value(sym)))
232 if (!line[1] || !strcmp(&line[1], "o"))
244 if (!line[1] || !strcmp(&line[1], "es"))
255 if (sym_set_tristate_value(sym, newval))
262 static int conf_choice(struct menu *menu)
264 struct symbol *sym, *def_sym;
269 is_new = !sym_has_value(sym);
270 if (sym_is_changeable(sym)) {
273 switch (sym_get_tristate_value(sym)) {
282 switch (sym_get_tristate_value(sym)) {
286 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
296 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
297 def_sym = sym_get_choice_value(sym);
300 for (child = menu->list; child; child = child->next) {
301 if (!menu_is_visible(child))
304 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
308 if (child->sym == def_sym) {
310 printf("%*c", indent, '>');
312 printf("%*c", indent, ' ');
313 printf(" %d. %s", cnt, menu_get_prompt(child));
314 if (child->sym->name)
315 printf(" (%s)", child->sym->name);
316 if (!sym_has_value(child->sym))
320 printf("%*schoice", indent - 1, "");
325 printf("[1-%d?]: ", cnt);
326 switch (input_mode) {
337 xfgets(line, sizeof(line), stdin);
339 if (line[0] == '?') {
345 else if (isdigit(line[0]))
355 for (child = menu->list; child; child = child->next) {
356 if (!child->sym || !menu_is_visible(child))
363 if (line[0] && line[strlen(line) - 1] == '?') {
367 sym_set_choice_value(sym, child->sym);
368 for (child = child->list; child; child = child->next) {
377 static void conf(struct menu *menu)
380 struct property *prop;
383 if (!menu_is_visible(menu))
391 switch (prop->type) {
394 * Except in oldaskconfig mode, we show only menus that
395 * contain new symbols.
397 if (input_mode != oldaskconfig && rootEntry != menu) {
403 prompt = menu_get_prompt(menu);
405 printf("%*c\n%*c %s\n%*c\n",
417 if (sym_is_choice(sym)) {
419 if (sym->curr.tri != mod)
438 for (child = menu->list; child; child = child->next)
444 static void check_conf(struct menu *menu)
449 if (!menu_is_visible(menu))
453 if (sym && !sym_has_value(sym) &&
454 (sym_is_changeable(sym) ||
455 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) {
457 switch (input_mode) {
462 if (sym->type == S_STRING) {
463 str = sym_get_string_value(sym);
464 str = sym_escape_string_value(str);
465 printf("%s%s=%s\n", CONFIG_, sym->name, str);
468 str = sym_get_string_value(sym);
469 printf("%s%s=%s\n", CONFIG_, sym->name, str);
480 printf("*\n* Restart config...\n*\n");
481 rootEntry = menu_get_parent_menu(menu);
487 for (child = menu->list; child; child = child->next)
491 static struct option long_opts[] = {
492 {"help", no_argument, NULL, 'h'},
493 {"silent", no_argument, NULL, 's'},
494 {"oldaskconfig", no_argument, &input_mode_opt, oldaskconfig},
495 {"oldconfig", no_argument, &input_mode_opt, oldconfig},
496 {"syncconfig", no_argument, &input_mode_opt, syncconfig},
497 {"defconfig", required_argument, &input_mode_opt, defconfig},
498 {"savedefconfig", required_argument, &input_mode_opt, savedefconfig},
499 {"allnoconfig", no_argument, &input_mode_opt, allnoconfig},
500 {"allyesconfig", no_argument, &input_mode_opt, allyesconfig},
501 {"allmodconfig", no_argument, &input_mode_opt, allmodconfig},
502 {"alldefconfig", no_argument, &input_mode_opt, alldefconfig},
503 {"randconfig", no_argument, &input_mode_opt, randconfig},
504 {"listnewconfig", no_argument, &input_mode_opt, listnewconfig},
505 {"helpnewconfig", no_argument, &input_mode_opt, helpnewconfig},
506 {"olddefconfig", no_argument, &input_mode_opt, olddefconfig},
507 {"yes2modconfig", no_argument, &input_mode_opt, yes2modconfig},
508 {"mod2yesconfig", no_argument, &input_mode_opt, mod2yesconfig},
512 static void conf_usage(const char *progname)
514 printf("Usage: %s [options] <kconfig-file>\n", progname);
516 printf("Generic options:\n");
517 printf(" -h, --help Print this message and exit.\n");
518 printf(" -s, --silent Do not print log.\n");
520 printf("Mode options:\n");
521 printf(" --listnewconfig List new options\n");
522 printf(" --helpnewconfig List new options and help text\n");
523 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
524 printf(" --oldconfig Update a configuration using a provided .config as base\n");
525 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
526 " include/{generated/,config/}\n");
527 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
528 printf(" --defconfig <file> New config with default defined in <file>\n");
529 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
530 printf(" --allnoconfig New config where all options are answered with no\n");
531 printf(" --allyesconfig New config where all options are answered with yes\n");
532 printf(" --allmodconfig New config where all options are answered with mod\n");
533 printf(" --alldefconfig New config with all symbols set to default\n");
534 printf(" --randconfig New config with random answer to all options\n");
535 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
536 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
537 printf(" (If none of the above is given, --oldaskconfig is the default)\n");
540 int main(int ac, char **av)
542 const char *progname = av[0];
544 const char *name, *defconfig_file = NULL /* gcc uninit */;
545 int no_conf_write = 0;
547 tty_stdio = isatty(0) && isatty(1);
549 while ((opt = getopt_long(ac, av, "hs", long_opts, NULL)) != -1) {
552 conf_usage(progname);
556 conf_set_message_callback(NULL);
559 input_mode = input_mode_opt;
560 switch (input_mode) {
563 * syncconfig is invoked during the build stage.
564 * Suppress distracting
565 * "configuration written to ..."
567 conf_set_message_callback(NULL);
572 defconfig_file = optarg;
575 set_randconfig_seed();
585 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
586 conf_usage(progname);
589 conf_parse(av[optind]);
592 switch (input_mode) {
594 if (conf_read(defconfig_file)) {
597 "*** Can't find default configuration \"%s\"!\n"
619 name = getenv("KCONFIG_ALLCONFIG");
622 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
623 if (conf_read_simple(name, S_DEF_USER)) {
625 "*** Can't read seed configuration \"%s\"!\n",
631 switch (input_mode) {
632 case allnoconfig: name = "allno.config"; break;
633 case allyesconfig: name = "allyes.config"; break;
634 case allmodconfig: name = "allmod.config"; break;
635 case alldefconfig: name = "alldef.config"; break;
636 case randconfig: name = "allrandom.config"; break;
639 if (conf_read_simple(name, S_DEF_USER) &&
640 conf_read_simple("all.config", S_DEF_USER)) {
642 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
652 name = getenv("KCONFIG_NOSILENTUPDATE");
654 if (conf_get_changed()) {
656 "\n*** The configuration requires explicit update.\n\n");
663 switch (input_mode) {
665 conf_set_all_new_symbols(def_no);
668 conf_set_all_new_symbols(def_yes);
671 conf_set_all_new_symbols(def_mod);
674 conf_set_all_new_symbols(def_default);
677 /* Really nothing to do in this loop */
678 while (conf_set_all_new_symbols(def_random)) ;
681 conf_set_all_new_symbols(def_default);
686 conf_rewrite_mod_or_yes(def_y2m);
689 conf_rewrite_mod_or_yes(def_m2y);
692 rootEntry = &rootmenu;
694 input_mode = oldconfig;
700 /* Update until a loop caused no more changes */
703 check_conf(&rootmenu);
711 if (input_mode == savedefconfig) {
712 if (conf_write_defconfig(defconfig_file)) {
713 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
717 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
718 if (!no_conf_write && conf_write(NULL)) {
719 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
724 * Create auto.conf if it does not exist.
725 * This prevents GNU Make 4.1 or older from emitting
726 * "include/config/auto.conf: No such file or directory"
727 * in the top-level Makefile
729 * syncconfig always creates or updates auto.conf because it is
730 * used during the build.
732 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
734 "\n*** Error during sync of the configuration.\n\n");