1 // SPDX-License-Identifier: GPL-2.0
20 static void conf(struct menu *menu);
21 static void check_conf(struct menu *menu);
40 static enum input_mode input_mode = oldaskconfig;
42 static int indent = 1;
44 static int sync_kconfig;
46 static char line[PATH_MAX];
47 static struct menu *rootEntry;
49 static void print_help(struct menu *menu)
51 struct gstr help = str_new();
53 menu_get_ext_help(menu, &help);
55 printf("\n%s\n", str_get(&help));
59 static void strip(char *str)
68 memmove(str, p, l + 1);
76 /* Helper function to facilitate fgets() by Jean Sacren. */
77 static void xfgets(char *str, int size, FILE *in)
79 if (!fgets(str, size, in))
80 fprintf(stderr, "\nError in reading or end of file.\n");
86 static int conf_askvalue(struct symbol *sym, const char *def)
88 enum symbol_type type = sym_get_type(sym);
90 if (!sym_has_value(sym))
96 if (!sym_is_changeable(sym)) {
103 switch (input_mode) {
106 if (sym_has_value(sym)) {
113 xfgets(line, sizeof(line), stdin);
132 static int conf_string(struct menu *menu)
134 struct symbol *sym = menu->sym;
138 printf("%*s%s ", indent - 1, "", menu->prompt->text);
139 printf("(%s) ", sym->name);
140 def = sym_get_string_value(sym);
141 if (sym_get_string_value(sym))
142 printf("[%s] ", def);
143 if (!conf_askvalue(sym, def))
150 if (line[1] == '\n') {
157 line[strlen(line)-1] = 0;
160 if (def && sym_set_string_value(sym, def))
165 static int conf_sym(struct menu *menu)
167 struct symbol *sym = menu->sym;
168 tristate oldval, newval;
171 printf("%*s%s ", indent - 1, "", menu->prompt->text);
173 printf("(%s) ", sym->name);
175 oldval = sym_get_tristate_value(sym);
187 if (oldval != no && sym_tristate_within_range(sym, no))
189 if (oldval != mod && sym_tristate_within_range(sym, mod))
191 if (oldval != yes && sym_tristate_within_range(sym, yes))
194 if (!conf_askvalue(sym, sym_get_string_value(sym)))
202 if (!line[1] || !strcmp(&line[1], "o"))
214 if (!line[1] || !strcmp(&line[1], "es"))
225 if (sym_set_tristate_value(sym, newval))
232 static int conf_choice(struct menu *menu)
234 struct symbol *sym, *def_sym;
239 is_new = !sym_has_value(sym);
240 if (sym_is_changeable(sym)) {
243 switch (sym_get_tristate_value(sym)) {
252 switch (sym_get_tristate_value(sym)) {
256 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
266 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
267 def_sym = sym_get_choice_value(sym);
270 for (child = menu->list; child; child = child->next) {
271 if (!menu_is_visible(child))
274 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
278 if (child->sym == def_sym) {
280 printf("%*c", indent, '>');
282 printf("%*c", indent, ' ');
283 printf(" %d. %s", cnt, menu_get_prompt(child));
284 if (child->sym->name)
285 printf(" (%s)", child->sym->name);
286 if (!sym_has_value(child->sym))
290 printf("%*schoice", indent - 1, "");
295 printf("[1-%d?]: ", cnt);
296 switch (input_mode) {
307 xfgets(line, sizeof(line), stdin);
309 if (line[0] == '?') {
315 else if (isdigit(line[0]))
325 for (child = menu->list; child; child = child->next) {
326 if (!child->sym || !menu_is_visible(child))
333 if (line[0] && line[strlen(line) - 1] == '?') {
337 sym_set_choice_value(sym, child->sym);
338 for (child = child->list; child; child = child->next) {
347 static void conf(struct menu *menu)
350 struct property *prop;
353 if (!menu_is_visible(menu))
361 switch (prop->type) {
364 * Except in oldaskconfig mode, we show only menus that
365 * contain new symbols.
367 if (input_mode != oldaskconfig && rootEntry != menu) {
373 prompt = menu_get_prompt(menu);
375 printf("%*c\n%*c %s\n%*c\n",
387 if (sym_is_choice(sym)) {
389 if (sym->curr.tri != mod)
408 for (child = menu->list; child; child = child->next)
414 static void check_conf(struct menu *menu)
419 if (!menu_is_visible(menu))
423 if (sym && !sym_has_value(sym)) {
424 if (sym_is_changeable(sym) ||
425 (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes)) {
426 if (input_mode == listnewconfig) {
430 if (sym->type == S_STRING) {
431 str = sym_get_string_value(sym);
432 str = sym_escape_string_value(str);
433 printf("%s%s=%s\n", CONFIG_, sym->name, str);
436 str = sym_get_string_value(sym);
437 printf("%s%s=%s\n", CONFIG_, sym->name, str);
440 } else if (input_mode == helpnewconfig) {
447 printf("*\n* Restart config...\n*\n");
448 rootEntry = menu_get_parent_menu(menu);
454 for (child = menu->list; child; child = child->next)
458 static struct option long_opts[] = {
459 {"oldaskconfig", no_argument, NULL, oldaskconfig},
460 {"oldconfig", no_argument, NULL, oldconfig},
461 {"syncconfig", no_argument, NULL, syncconfig},
462 {"defconfig", required_argument, NULL, defconfig},
463 {"savedefconfig", required_argument, NULL, savedefconfig},
464 {"allnoconfig", no_argument, NULL, allnoconfig},
465 {"allyesconfig", no_argument, NULL, allyesconfig},
466 {"allmodconfig", no_argument, NULL, allmodconfig},
467 {"alldefconfig", no_argument, NULL, alldefconfig},
468 {"randconfig", no_argument, NULL, randconfig},
469 {"listnewconfig", no_argument, NULL, listnewconfig},
470 {"helpnewconfig", no_argument, NULL, helpnewconfig},
471 {"olddefconfig", no_argument, NULL, olddefconfig},
472 {"yes2modconfig", no_argument, NULL, yes2modconfig},
473 {"mod2yesconfig", no_argument, NULL, mod2yesconfig},
477 static void conf_usage(const char *progname)
480 printf("Usage: %s [-s] [option] <kconfig-file>\n", progname);
481 printf("[option] is _one_ of the following:\n");
482 printf(" --listnewconfig List new options\n");
483 printf(" --helpnewconfig List new options and help text\n");
484 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
485 printf(" --oldconfig Update a configuration using a provided .config as base\n");
486 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
487 " include/{generated/,config/}\n");
488 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
489 printf(" --defconfig <file> New config with default defined in <file>\n");
490 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
491 printf(" --allnoconfig New config where all options are answered with no\n");
492 printf(" --allyesconfig New config where all options are answered with yes\n");
493 printf(" --allmodconfig New config where all options are answered with mod\n");
494 printf(" --alldefconfig New config with all symbols set to default\n");
495 printf(" --randconfig New config with random answer to all options\n");
496 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
497 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
500 int main(int ac, char **av)
502 const char *progname = av[0];
504 const char *name, *defconfig_file = NULL /* gcc uninit */;
505 int no_conf_write = 0;
507 tty_stdio = isatty(0) && isatty(1);
509 while ((opt = getopt_long(ac, av, "s", long_opts, NULL)) != -1) {
511 conf_set_message_callback(NULL);
514 input_mode = (enum input_mode)opt;
518 * syncconfig is invoked during the build stage.
519 * Suppress distracting "configuration written to ..."
521 conf_set_message_callback(NULL);
526 defconfig_file = optarg;
535 * Use microseconds derived seed,
536 * compensate for systems where it may be zero
538 gettimeofday(&now, NULL);
539 seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
541 seed_env = getenv("KCONFIG_SEED");
542 if( seed_env && *seed_env ) {
544 int tmp = (int)strtol(seed_env, &endp, 0);
549 fprintf( stderr, "KCONFIG_SEED=0x%X\n", seed );
566 conf_usage(progname);
572 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
573 conf_usage(progname);
580 switch (input_mode) {
582 if (conf_read(defconfig_file)) {
585 "*** Can't find default configuration \"%s\"!\n"
607 name = getenv("KCONFIG_ALLCONFIG");
610 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
611 if (conf_read_simple(name, S_DEF_USER)) {
613 "*** Can't read seed configuration \"%s\"!\n",
619 switch (input_mode) {
620 case allnoconfig: name = "allno.config"; break;
621 case allyesconfig: name = "allyes.config"; break;
622 case allmodconfig: name = "allmod.config"; break;
623 case alldefconfig: name = "alldef.config"; break;
624 case randconfig: name = "allrandom.config"; break;
627 if (conf_read_simple(name, S_DEF_USER) &&
628 conf_read_simple("all.config", S_DEF_USER)) {
630 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
640 name = getenv("KCONFIG_NOSILENTUPDATE");
642 if (conf_get_changed()) {
644 "\n*** The configuration requires explicit update.\n\n");
651 switch (input_mode) {
653 conf_set_all_new_symbols(def_no);
656 conf_set_all_new_symbols(def_yes);
659 conf_set_all_new_symbols(def_mod);
662 conf_set_all_new_symbols(def_default);
665 /* Really nothing to do in this loop */
666 while (conf_set_all_new_symbols(def_random)) ;
669 conf_set_all_new_symbols(def_default);
674 conf_rewrite_mod_or_yes(def_y2m);
677 conf_rewrite_mod_or_yes(def_m2y);
680 rootEntry = &rootmenu;
682 input_mode = oldconfig;
688 /* Update until a loop caused no more changes */
691 check_conf(&rootmenu);
699 if (input_mode == savedefconfig) {
700 if (conf_write_defconfig(defconfig_file)) {
701 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
705 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
706 if (!no_conf_write && conf_write(NULL)) {
707 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
712 * Create auto.conf if it does not exist.
713 * This prevents GNU Make 4.1 or older from emitting
714 * "include/config/auto.conf: No such file or directory"
715 * in the top-level Makefile
717 * syncconfig always creates or updates auto.conf because it is
718 * used during the build.
720 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
722 "\n*** Error during sync of the configuration.\n\n");