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);
140 if (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 if (sym_is_changeable(sym) ||
424 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
425 if (input_mode == listnewconfig) {
429 if (sym->type == S_STRING) {
430 str = sym_get_string_value(sym);
431 str = sym_escape_string_value(str);
432 printf("%s%s=%s\n", CONFIG_, sym->name, str);
435 str = sym_get_string_value(sym);
436 printf("%s%s=%s\n", CONFIG_, sym->name, str);
439 } else if (input_mode == helpnewconfig) {
446 printf("*\n* Restart config...\n*\n");
447 rootEntry = menu_get_parent_menu(menu);
453 for (child = menu->list; child; child = child->next)
457 static struct option long_opts[] = {
458 {"oldaskconfig", no_argument, NULL, oldaskconfig},
459 {"oldconfig", no_argument, NULL, oldconfig},
460 {"syncconfig", no_argument, NULL, syncconfig},
461 {"defconfig", required_argument, NULL, defconfig},
462 {"savedefconfig", required_argument, NULL, savedefconfig},
463 {"allnoconfig", no_argument, NULL, allnoconfig},
464 {"allyesconfig", no_argument, NULL, allyesconfig},
465 {"allmodconfig", no_argument, NULL, allmodconfig},
466 {"alldefconfig", no_argument, NULL, alldefconfig},
467 {"randconfig", no_argument, NULL, randconfig},
468 {"listnewconfig", no_argument, NULL, listnewconfig},
469 {"helpnewconfig", no_argument, NULL, helpnewconfig},
470 {"olddefconfig", no_argument, NULL, olddefconfig},
471 {"yes2modconfig", no_argument, NULL, yes2modconfig},
472 {"mod2yesconfig", no_argument, NULL, mod2yesconfig},
476 static void conf_usage(const char *progname)
479 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
480 printf("[option] is _one_ of the following:\n");
481 printf(" --listnewconfig List new options\n");
482 printf(" --helpnewconfig List new options and help text\n");
483 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
484 printf(" --oldconfig Update a configuration using a provided .config as base\n");
485 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
486 " include/{generated/,config/}\n");
487 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
488 printf(" --defconfig <file> New config with default defined in <file>\n");
489 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
490 printf(" --allnoconfig New config where all options are answered with no\n");
491 printf(" --allyesconfig New config where all options are answered with yes\n");
492 printf(" --allmodconfig New config where all options are answered with mod\n");
493 printf(" --alldefconfig New config with all symbols set to default\n");
494 printf(" --randconfig New config with random answer to all options\n");
495 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
496 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
499 int main(int ac, char **av)
501 const char *progname = av[0];
503 const char *name, *defconfig_file = NULL /* gcc uninit */;
504 int no_conf_write = 0;
506 tty_stdio = isatty(0) && isatty(1);
508 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
510 conf_set_message_callback(NULL);
513 input_mode = (enum input_mode)opt;
517 * syncconfig is invoked during the build stage.
518 * Suppress distracting "configuration written to ..."
520 conf_set_message_callback(NULL);
525 defconfig_file = optarg;
534 * Use microseconds derived seed,
535 * compensate for systems where it may be zero
537 gettimeofday(&now, NULL);
538 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
540 seed_env = getenv("KCONFIG_SEED");
541 if( seed_env && *seed_env ) {
543 int tmp = (int)strtol(seed_env, &endp, 0);
548 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
565 conf_usage(progname);
571 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
572 conf_usage(progname);
579 switch (input_mode) {
581 if (conf_read(defconfig_file)) {
584 "*** Can't find default configuration \"%s\"!\n"
606 name = getenv("KCONFIG_ALLCONFIG");
609 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
610 if (conf_read_simple(name, S_DEF_USER)) {
612 "*** Can't read seed configuration \"%s\"!\n",
618 switch (input_mode) {
619 case allnoconfig: name = "allno.config"; break;
620 case allyesconfig: name = "allyes.config"; break;
621 case allmodconfig: name = "allmod.config"; break;
622 case alldefconfig: name = "alldef.config"; break;
623 case randconfig: name = "allrandom.config"; break;
626 if (conf_read_simple(name, S_DEF_USER) &&
627 conf_read_simple("all.config", S_DEF_USER)) {
629 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
639 name = getenv("KCONFIG_NOSILENTUPDATE");
641 if (conf_get_changed()) {
643 "\n*** The configuration requires explicit update.\n\n");
650 switch (input_mode) {
652 conf_set_all_new_symbols(def_no);
655 conf_set_all_new_symbols(def_yes);
658 conf_set_all_new_symbols(def_mod);
661 conf_set_all_new_symbols(def_default);
664 /* Really nothing to do in this loop */
665 while (conf_set_all_new_symbols(def_random)) ;
668 conf_set_all_new_symbols(def_default);
673 conf_rewrite_mod_or_yes(def_y2m);
676 conf_rewrite_mod_or_yes(def_m2y);
679 rootEntry = &rootmenu;
681 input_mode = oldconfig;
687 /* Update until a loop caused no more changes */
690 check_conf(&rootmenu);
698 if (input_mode == savedefconfig) {
699 if (conf_write_defconfig(defconfig_file)) {
700 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
704 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
705 if (!no_conf_write && conf_write(NULL)) {
706 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
711 * Create auto.conf if it does not exist.
712 * This prevents GNU Make 4.1 or older from emitting
713 * "include/generated/auto.conf: No such file or directory"
714 * in the top-level Makefile
716 * syncconfig always creates or updates auto.conf because it is
717 * used during the build.
721 * In our cmake case, we always want to update the autogenerated
726 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
728 "\n*** Error during sync of the configuration.\n\n");