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 int conf_askvalue(struct symbol *sym, const char *def)
117 if (!sym_has_value(sym))
123 if (!sym_is_changeable(sym)) {
130 switch (input_mode) {
133 if (sym_has_value(sym)) {
140 xfgets(line, sizeof(line), stdin);
147 static int conf_string(struct menu *menu)
149 struct symbol *sym = menu->sym;
153 printf("%*s%s ", indent - 1, "", menu->prompt->text);
154 printf("(%s) ", sym->name);
155 def = sym_get_string_value(sym);
157 printf("[%s] ", def);
158 if (!conf_askvalue(sym, def))
165 if (line[1] == '\n') {
172 line[strlen(line)-1] = 0;
175 if (def && sym_set_string_value(sym, def))
180 static int conf_sym(struct menu *menu)
182 struct symbol *sym = menu->sym;
183 tristate oldval, newval;
186 printf("%*s%s ", indent - 1, "", menu->prompt->text);
188 printf("(%s) ", sym->name);
190 oldval = sym_get_tristate_value(sym);
202 if (oldval != no && sym_tristate_within_range(sym, no))
204 if (oldval != mod && sym_tristate_within_range(sym, mod))
206 if (oldval != yes && sym_tristate_within_range(sym, yes))
209 if (!conf_askvalue(sym, sym_get_string_value(sym)))
217 if (!line[1] || !strcmp(&line[1], "o"))
229 if (!line[1] || !strcmp(&line[1], "es"))
240 if (sym_set_tristate_value(sym, newval))
247 static int conf_choice(struct menu *menu)
249 struct symbol *sym, *def_sym;
254 is_new = !sym_has_value(sym);
255 if (sym_is_changeable(sym)) {
258 switch (sym_get_tristate_value(sym)) {
267 switch (sym_get_tristate_value(sym)) {
271 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
281 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
282 def_sym = sym_get_choice_value(sym);
285 for (child = menu->list; child; child = child->next) {
286 if (!menu_is_visible(child))
289 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
293 if (child->sym == def_sym) {
295 printf("%*c", indent, '>');
297 printf("%*c", indent, ' ');
298 printf(" %d. %s", cnt, menu_get_prompt(child));
299 if (child->sym->name)
300 printf(" (%s)", child->sym->name);
301 if (!sym_has_value(child->sym))
305 printf("%*schoice", indent - 1, "");
310 printf("[1-%d?]: ", cnt);
311 switch (input_mode) {
322 xfgets(line, sizeof(line), stdin);
324 if (line[0] == '?') {
330 else if (isdigit(line[0]))
340 for (child = menu->list; child; child = child->next) {
341 if (!child->sym || !menu_is_visible(child))
348 if (line[0] && line[strlen(line) - 1] == '?') {
352 sym_set_choice_value(sym, child->sym);
353 for (child = child->list; child; child = child->next) {
362 static void conf(struct menu *menu)
365 struct property *prop;
368 if (!menu_is_visible(menu))
376 switch (prop->type) {
379 * Except in oldaskconfig mode, we show only menus that
380 * contain new symbols.
382 if (input_mode != oldaskconfig && rootEntry != menu) {
388 prompt = menu_get_prompt(menu);
390 printf("%*c\n%*c %s\n%*c\n",
402 if (sym_is_choice(sym)) {
404 if (sym->curr.tri != mod)
423 for (child = menu->list; child; child = child->next)
429 static void check_conf(struct menu *menu)
434 if (!menu_is_visible(menu))
438 if (sym && !sym_has_value(sym) &&
439 (sym_is_changeable(sym) ||
440 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) {
442 switch (input_mode) {
447 if (sym->type == S_STRING) {
448 str = sym_get_string_value(sym);
449 str = sym_escape_string_value(str);
450 printf("%s%s=%s\n", CONFIG_, sym->name, str);
453 str = sym_get_string_value(sym);
454 printf("%s%s=%s\n", CONFIG_, sym->name, str);
465 printf("*\n* Restart config...\n*\n");
466 rootEntry = menu_get_parent_menu(menu);
472 for (child = menu->list; child; child = child->next)
476 static struct option long_opts[] = {
477 {"help", no_argument, NULL, 'h'},
478 {"silent", no_argument, NULL, 's'},
479 {"oldaskconfig", no_argument, &input_mode_opt, oldaskconfig},
480 {"oldconfig", no_argument, &input_mode_opt, oldconfig},
481 {"syncconfig", no_argument, &input_mode_opt, syncconfig},
482 {"defconfig", required_argument, &input_mode_opt, defconfig},
483 {"savedefconfig", required_argument, &input_mode_opt, savedefconfig},
484 {"allnoconfig", no_argument, &input_mode_opt, allnoconfig},
485 {"allyesconfig", no_argument, &input_mode_opt, allyesconfig},
486 {"allmodconfig", no_argument, &input_mode_opt, allmodconfig},
487 {"alldefconfig", no_argument, &input_mode_opt, alldefconfig},
488 {"randconfig", no_argument, &input_mode_opt, randconfig},
489 {"listnewconfig", no_argument, &input_mode_opt, listnewconfig},
490 {"helpnewconfig", no_argument, &input_mode_opt, helpnewconfig},
491 {"olddefconfig", no_argument, &input_mode_opt, olddefconfig},
492 {"yes2modconfig", no_argument, &input_mode_opt, yes2modconfig},
493 {"mod2yesconfig", no_argument, &input_mode_opt, mod2yesconfig},
497 static void conf_usage(const char *progname)
499 printf("Usage: %s [options] <kconfig-file>\n", progname);
501 printf("Generic options:\n");
502 printf(" -h, --help Print this message and exit.\n");
503 printf(" -s, --silent Do not print log.\n");
505 printf("Mode options:\n");
506 printf(" --listnewconfig List new options\n");
507 printf(" --helpnewconfig List new options and help text\n");
508 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
509 printf(" --oldconfig Update a configuration using a provided .config as base\n");
510 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
511 " include/{generated/,config/}\n");
512 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
513 printf(" --defconfig <file> New config with default defined in <file>\n");
514 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
515 printf(" --allnoconfig New config where all options are answered with no\n");
516 printf(" --allyesconfig New config where all options are answered with yes\n");
517 printf(" --allmodconfig New config where all options are answered with mod\n");
518 printf(" --alldefconfig New config with all symbols set to default\n");
519 printf(" --randconfig New config with random answer to all options\n");
520 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
521 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
522 printf(" (If none of the above is given, --oldaskconfig is the default)\n");
525 int main(int ac, char **av)
527 const char *progname = av[0];
529 const char *name, *defconfig_file = NULL /* gcc uninit */;
530 int no_conf_write = 0;
532 tty_stdio = isatty(0) && isatty(1);
534 while ((opt = getopt_long(ac, av, "hs", long_opts, NULL)) != -1) {
537 conf_usage(progname);
541 conf_set_message_callback(NULL);
544 input_mode = input_mode_opt;
545 switch (input_mode) {
548 * syncconfig is invoked during the build stage.
549 * Suppress distracting
550 * "configuration written to ..."
552 conf_set_message_callback(NULL);
557 defconfig_file = optarg;
560 set_randconfig_seed();
570 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
571 conf_usage(progname);
574 conf_parse(av[optind]);
577 switch (input_mode) {
579 if (conf_read(defconfig_file)) {
582 "*** Can't find default configuration \"%s\"!\n"
604 name = getenv("KCONFIG_ALLCONFIG");
607 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
608 if (conf_read_simple(name, S_DEF_USER)) {
610 "*** Can't read seed configuration \"%s\"!\n",
616 switch (input_mode) {
617 case allnoconfig: name = "allno.config"; break;
618 case allyesconfig: name = "allyes.config"; break;
619 case allmodconfig: name = "allmod.config"; break;
620 case alldefconfig: name = "alldef.config"; break;
621 case randconfig: name = "allrandom.config"; break;
624 if (conf_read_simple(name, S_DEF_USER) &&
625 conf_read_simple("all.config", S_DEF_USER)) {
627 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
637 name = getenv("KCONFIG_NOSILENTUPDATE");
639 if (conf_get_changed()) {
641 "\n*** The configuration requires explicit update.\n\n");
648 switch (input_mode) {
650 conf_set_all_new_symbols(def_no);
653 conf_set_all_new_symbols(def_yes);
656 conf_set_all_new_symbols(def_mod);
659 conf_set_all_new_symbols(def_default);
662 /* Really nothing to do in this loop */
663 while (conf_set_all_new_symbols(def_random)) ;
666 conf_set_all_new_symbols(def_default);
671 conf_rewrite_mod_or_yes(def_y2m);
674 conf_rewrite_mod_or_yes(def_m2y);
677 rootEntry = &rootmenu;
679 input_mode = oldconfig;
685 /* Update until a loop caused no more changes */
688 check_conf(&rootmenu);
696 if (input_mode == savedefconfig) {
697 if (conf_write_defconfig(defconfig_file)) {
698 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
702 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
703 if (!no_conf_write && conf_write(NULL)) {
704 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
709 * Create auto.conf if it does not exist.
710 * This prevents GNU Make 4.1 or older from emitting
711 * "include/config/auto.conf: No such file or directory"
712 * in the top-level Makefile
714 * syncconfig always creates or updates auto.conf because it is
715 * used during the build.
717 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
719 "\n*** Error during sync of the configuration.\n\n");