1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
20 static void conf(struct menu *menu);
21 static void check_conf(struct menu *menu);
41 static enum input_mode input_mode = oldaskconfig;
43 static int indent = 1;
45 static int sync_kconfig;
47 static char line[PATH_MAX];
48 static struct menu *rootEntry;
50 static void print_help(struct menu *menu)
52 struct gstr help = str_new();
54 menu_get_ext_help(menu, &help);
56 printf("\n%s\n", str_get(&help));
60 static void strip(char *str)
69 memmove(str, p, l + 1);
77 /* Helper function to facilitate fgets() by Jean Sacren. */
78 static void xfgets(char *str, int size, FILE *in)
80 if (!fgets(str, size, in))
81 fprintf(stderr, "\nError in reading or end of file.\n");
87 static int conf_askvalue(struct symbol *sym, const char *def)
89 enum symbol_type type = sym_get_type(sym);
91 if (!sym_has_value(sym))
97 if (!sym_is_changeable(sym)) {
104 switch (input_mode) {
107 if (sym_has_value(sym)) {
114 xfgets(line, sizeof(line), stdin);
133 static int conf_string(struct menu *menu)
135 struct symbol *sym = menu->sym;
139 printf("%*s%s ", indent - 1, "", menu->prompt->text);
140 printf("(%s) ", sym->name);
141 def = sym_get_string_value(sym);
142 if (sym_get_string_value(sym))
143 printf("[%s] ", def);
144 if (!conf_askvalue(sym, def))
151 if (line[1] == '\n') {
158 line[strlen(line)-1] = 0;
161 if (def && sym_set_string_value(sym, def))
166 static int conf_sym(struct menu *menu)
168 struct symbol *sym = menu->sym;
169 tristate oldval, newval;
172 printf("%*s%s ", indent - 1, "", menu->prompt->text);
174 printf("(%s) ", sym->name);
176 oldval = sym_get_tristate_value(sym);
188 if (oldval != no && sym_tristate_within_range(sym, no))
190 if (oldval != mod && sym_tristate_within_range(sym, mod))
192 if (oldval != yes && sym_tristate_within_range(sym, yes))
195 if (!conf_askvalue(sym, sym_get_string_value(sym)))
203 if (!line[1] || !strcmp(&line[1], "o"))
215 if (!line[1] || !strcmp(&line[1], "es"))
226 if (sym_set_tristate_value(sym, newval))
233 static int conf_choice(struct menu *menu)
235 struct symbol *sym, *def_sym;
240 is_new = !sym_has_value(sym);
241 if (sym_is_changeable(sym)) {
244 switch (sym_get_tristate_value(sym)) {
253 switch (sym_get_tristate_value(sym)) {
257 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
267 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
268 def_sym = sym_get_choice_value(sym);
271 for (child = menu->list; child; child = child->next) {
272 if (!menu_is_visible(child))
275 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
279 if (child->sym == def_sym) {
281 printf("%*c", indent, '>');
283 printf("%*c", indent, ' ');
284 printf(" %d. %s", cnt, menu_get_prompt(child));
285 if (child->sym->name)
286 printf(" (%s)", child->sym->name);
287 if (!sym_has_value(child->sym))
291 printf("%*schoice", indent - 1, "");
296 printf("[1-%d?]: ", cnt);
297 switch (input_mode) {
308 xfgets(line, sizeof(line), stdin);
310 if (line[0] == '?') {
316 else if (isdigit(line[0]))
326 for (child = menu->list; child; child = child->next) {
327 if (!child->sym || !menu_is_visible(child))
334 if (line[0] && line[strlen(line) - 1] == '?') {
338 sym_set_choice_value(sym, child->sym);
339 for (child = child->list; child; child = child->next) {
348 static void conf(struct menu *menu)
351 struct property *prop;
354 if (!menu_is_visible(menu))
362 switch (prop->type) {
365 * Except in oldaskconfig mode, we show only menus that
366 * contain new symbols.
368 if (input_mode != oldaskconfig && rootEntry != menu) {
374 prompt = menu_get_prompt(menu);
376 printf("%*c\n%*c %s\n%*c\n",
388 if (sym_is_choice(sym)) {
390 if (sym->curr.tri != mod)
409 for (child = menu->list; child; child = child->next)
415 static void check_conf(struct menu *menu)
420 if (!menu_is_visible(menu))
424 if (sym && !sym_has_value(sym)) {
425 if (sym_is_changeable(sym) ||
426 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
427 if (input_mode == listnewconfig) {
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);
441 } else if (input_mode == helpnewconfig) {
448 printf("*\n* Restart config...\n*\n");
449 rootEntry = menu_get_parent_menu(menu);
455 for (child = menu->list; child; child = child->next)
459 static struct option long_opts[] = {
460 {"oldaskconfig", no_argument, NULL, oldaskconfig},
461 {"oldconfig", no_argument, NULL, oldconfig},
462 {"syncconfig", no_argument, NULL, syncconfig},
463 {"defconfig", required_argument, NULL, defconfig},
464 {"savedefconfig", required_argument, NULL, savedefconfig},
465 {"allnoconfig", no_argument, NULL, allnoconfig},
466 {"allyesconfig", no_argument, NULL, allyesconfig},
467 {"allmodconfig", no_argument, NULL, allmodconfig},
468 {"alldefconfig", no_argument, NULL, alldefconfig},
469 {"randconfig", no_argument, NULL, randconfig},
470 {"listnewconfig", no_argument, NULL, listnewconfig},
471 {"helpnewconfig", no_argument, NULL, helpnewconfig},
472 {"olddefconfig", no_argument, NULL, olddefconfig},
473 {"yes2modconfig", no_argument, NULL, yes2modconfig},
474 {"mod2yesconfig", no_argument, NULL, mod2yesconfig},
475 {"fatalrecursive", no_argument, NULL, fatalrecursive},
479 static void conf_usage(const char *progname)
482 printf("Usage: %s [-s] [--fatalrecursive] [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 const char *input_file = NULL, *output_file = NULL;
508 int no_conf_write = 0;
510 tty_stdio = isatty(0) && isatty(1);
512 while ((opt = getopt_long(ac, av, "r:w:s", long_opts, NULL)) != -1) {
514 conf_set_message_callback(NULL);
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 recursive_is_error = 1;
574 output_file = optarg;
577 conf_usage(progname);
581 input_mode = (enum input_mode)opt;
584 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
585 conf_usage(progname);
592 switch (input_mode) {
594 if (conf_read(defconfig_file)) {
597 "*** Can't find default configuration \"%s\"!\n"
617 conf_read(input_file);
624 name = getenv("KCONFIG_NOSILENTUPDATE");
626 if (conf_get_changed()) {
628 "\n*** The configuration requires explicit update.\n\n");
635 switch (input_mode) {
637 conf_set_all_new_symbols(def_no);
640 conf_set_all_new_symbols(def_yes);
643 conf_set_all_new_symbols(def_mod);
646 conf_set_all_new_symbols(def_default);
649 /* Really nothing to do in this loop */
650 while (conf_set_all_new_symbols(def_random)) ;
653 conf_set_all_new_symbols(def_default);
658 conf_rewrite_mod_or_yes(def_y2m);
661 conf_rewrite_mod_or_yes(def_m2y);
664 rootEntry = &rootmenu;
666 input_mode = oldconfig;
672 /* Update until a loop caused no more changes */
675 check_conf(&rootmenu);
683 if (input_mode == savedefconfig) {
684 if (conf_write_defconfig(defconfig_file)) {
685 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
689 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
690 if ((output_file || !no_conf_write) &&
691 conf_write(output_file)) {
692 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
697 * Create auto.conf if it does not exist.
698 * This prevents GNU Make 4.1 or older from emitting
699 * "include/config/auto.conf: No such file or directory"
700 * in the top-level Makefile
702 * syncconfig always creates or updates auto.conf because it is
703 * used during the build.
705 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
707 "\n*** Error during sync of the configuration.\n\n");