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 enum symbol_type type = sym_get_type(sym);
89 if (!sym_has_value(sym))
95 if (!sym_is_changeable(sym)) {
102 switch (input_mode) {
105 if (sym_has_value(sym)) {
112 xfgets(line, sizeof(line), stdin);
131 static int conf_string(struct menu *menu)
133 struct symbol *sym = menu->sym;
137 printf("%*s%s ", indent - 1, "", menu->prompt->text);
138 printf("(%s) ", sym->name);
139 def = sym_get_string_value(sym);
141 printf("[%s] ", def);
142 if (!conf_askvalue(sym, def))
149 if (line[1] == '\n') {
156 line[strlen(line)-1] = 0;
159 if (def && sym_set_string_value(sym, def))
164 static int conf_sym(struct menu *menu)
166 struct symbol *sym = menu->sym;
167 tristate oldval, newval;
170 printf("%*s%s ", indent - 1, "", menu->prompt->text);
172 printf("(%s) ", sym->name);
174 oldval = sym_get_tristate_value(sym);
186 if (oldval != no && sym_tristate_within_range(sym, no))
188 if (oldval != mod && sym_tristate_within_range(sym, mod))
190 if (oldval != yes && sym_tristate_within_range(sym, yes))
193 if (!conf_askvalue(sym, sym_get_string_value(sym)))
201 if (!line[1] || !strcmp(&line[1], "o"))
213 if (!line[1] || !strcmp(&line[1], "es"))
224 if (sym_set_tristate_value(sym, newval))
231 static int conf_choice(struct menu *menu)
233 struct symbol *sym, *def_sym;
238 is_new = !sym_has_value(sym);
239 if (sym_is_changeable(sym)) {
242 switch (sym_get_tristate_value(sym)) {
251 switch (sym_get_tristate_value(sym)) {
255 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
265 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
266 def_sym = sym_get_choice_value(sym);
269 for (child = menu->list; child; child = child->next) {
270 if (!menu_is_visible(child))
273 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
277 if (child->sym == def_sym) {
279 printf("%*c", indent, '>');
281 printf("%*c", indent, ' ');
282 printf(" %d. %s", cnt, menu_get_prompt(child));
283 if (child->sym->name)
284 printf(" (%s)", child->sym->name);
285 if (!sym_has_value(child->sym))
289 printf("%*schoice", indent - 1, "");
294 printf("[1-%d?]: ", cnt);
295 switch (input_mode) {
306 xfgets(line, sizeof(line), 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) {
363 * Except in oldaskconfig mode, we show only menus that
364 * contain new symbols.
366 if (input_mode != oldaskconfig && rootEntry != menu) {
372 prompt = menu_get_prompt(menu);
374 printf("%*c\n%*c %s\n%*c\n",
386 if (sym_is_choice(sym)) {
388 if (sym->curr.tri != mod)
407 for (child = menu->list; child; child = child->next)
413 static void check_conf(struct menu *menu)
418 if (!menu_is_visible(menu))
422 if (sym && !sym_has_value(sym) &&
423 (sym_is_changeable(sym) ||
424 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) {
426 switch (input_mode) {
431 if (sym->type == S_STRING) {
432 str = sym_get_string_value(sym);
433 str = sym_escape_string_value(str);
434 printf("%s%s=%s\n", CONFIG_, sym->name, str);
437 str = sym_get_string_value(sym);
438 printf("%s%s=%s\n", CONFIG_, sym->name, str);
449 printf("*\n* Restart config...\n*\n");
450 rootEntry = menu_get_parent_menu(menu);
456 for (child = menu->list; child; child = child->next)
460 static struct option long_opts[] = {
461 {"oldaskconfig", no_argument, NULL, oldaskconfig},
462 {"oldconfig", no_argument, NULL, oldconfig},
463 {"syncconfig", no_argument, NULL, syncconfig},
464 {"defconfig", required_argument, NULL, defconfig},
465 {"savedefconfig", required_argument, NULL, savedefconfig},
466 {"allnoconfig", no_argument, NULL, allnoconfig},
467 {"allyesconfig", no_argument, NULL, allyesconfig},
468 {"allmodconfig", no_argument, NULL, allmodconfig},
469 {"alldefconfig", no_argument, NULL, alldefconfig},
470 {"randconfig", no_argument, NULL, randconfig},
471 {"listnewconfig", no_argument, NULL, listnewconfig},
472 {"helpnewconfig", no_argument, NULL, helpnewconfig},
473 {"olddefconfig", no_argument, NULL, olddefconfig},
474 {"yes2modconfig", no_argument, NULL, yes2modconfig},
475 {"mod2yesconfig", no_argument, NULL, mod2yesconfig},
479 static void conf_usage(const char *progname)
482 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
483 printf("[option] is _one_ of the following:\n");
484 printf(" --listnewconfig List new options\n");
485 printf(" --helpnewconfig List new options and help text\n");
486 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
487 printf(" --oldconfig Update a configuration using a provided .config as base\n");
488 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
489 " include/{generated/,config/}\n");
490 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
491 printf(" --defconfig <file> New config with default defined in <file>\n");
492 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
493 printf(" --allnoconfig New config where all options are answered with no\n");
494 printf(" --allyesconfig New config where all options are answered with yes\n");
495 printf(" --allmodconfig New config where all options are answered with mod\n");
496 printf(" --alldefconfig New config with all symbols set to default\n");
497 printf(" --randconfig New config with random answer to all options\n");
498 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
499 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
502 int main(int ac, char **av)
504 const char *progname = av[0];
506 const char *name, *defconfig_file = NULL /* gcc uninit */;
507 int no_conf_write = 0;
509 tty_stdio = isatty(0) && isatty(1);
511 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
513 conf_set_message_callback(NULL);
516 input_mode = (enum input_mode)opt;
520 * syncconfig is invoked during the build stage.
521 * Suppress distracting "configuration written to ..."
523 conf_set_message_callback(NULL);
528 defconfig_file = optarg;
537 * Use microseconds derived seed,
538 * compensate for systems where it may be zero
540 gettimeofday(&now, NULL);
541 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
543 seed_env = getenv("KCONFIG_SEED");
544 if( seed_env && *seed_env ) {
546 int tmp = (int)strtol(seed_env, &endp, 0);
551 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
568 conf_usage(progname);
574 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
575 conf_usage(progname);
582 switch (input_mode) {
584 if (conf_read(defconfig_file)) {
587 "*** Can't find default configuration \"%s\"!\n"
609 name = getenv("KCONFIG_ALLCONFIG");
612 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
613 if (conf_read_simple(name, S_DEF_USER)) {
615 "*** Can't read seed configuration \"%s\"!\n",
621 switch (input_mode) {
622 case allnoconfig: name = "allno.config"; break;
623 case allyesconfig: name = "allyes.config"; break;
624 case allmodconfig: name = "allmod.config"; break;
625 case alldefconfig: name = "alldef.config"; break;
626 case randconfig: name = "allrandom.config"; break;
629 if (conf_read_simple(name, S_DEF_USER) &&
630 conf_read_simple("all.config", S_DEF_USER)) {
632 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
642 name = getenv("KCONFIG_NOSILENTUPDATE");
644 if (conf_get_changed()) {
646 "\n*** The configuration requires explicit update.\n\n");
653 switch (input_mode) {
655 conf_set_all_new_symbols(def_no);
658 conf_set_all_new_symbols(def_yes);
661 conf_set_all_new_symbols(def_mod);
664 conf_set_all_new_symbols(def_default);
667 /* Really nothing to do in this loop */
668 while (conf_set_all_new_symbols(def_random)) ;
671 conf_set_all_new_symbols(def_default);
676 conf_rewrite_mod_or_yes(def_y2m);
679 conf_rewrite_mod_or_yes(def_m2y);
682 rootEntry = &rootmenu;
684 input_mode = oldconfig;
690 /* Update until a loop caused no more changes */
693 check_conf(&rootmenu);
701 if (input_mode == savedefconfig) {
702 if (conf_write_defconfig(defconfig_file)) {
703 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
707 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
708 if (!no_conf_write && conf_write(NULL)) {
709 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
714 * Create auto.conf if it does not exist.
715 * This prevents GNU Make 4.1 or older from emitting
716 * "include/config/auto.conf: No such file or directory"
717 * in the top-level Makefile
719 * syncconfig always creates or updates auto.conf because it is
720 * used during the build.
722 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
724 "\n*** Error during sync of the configuration.\n\n");