1 // SPDX-License-Identifier: GPL-2.0-only
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
);
40 static enum input_mode input_mode
= oldaskconfig
;
41 static int input_mode_opt
;
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 void set_randconfig_seed(void)
90 bool seed_set
= false;
92 env
= getenv("KCONFIG_SEED");
96 seed
= strtol(env
, &endp
, 0);
105 * Use microseconds derived seed, compensate for systems where it may
108 gettimeofday(&now
, NULL
);
109 seed
= (now
.tv_sec
+ 1) * (now
.tv_usec
+ 1);
112 printf("KCONFIG_SEED=0x%X\n", seed
);
116 static bool randomize_choice_values(struct symbol
*csym
)
118 struct property
*prop
;
124 * If choice is mod then we may have more items selected
125 * and if no then no-one.
126 * In both cases stop.
128 if (csym
->curr
.tri
!= yes
)
131 prop
= sym_get_choice_prop(csym
);
133 /* count entries in choice block */
135 expr_list_for_each_sym(prop
->expr
, e
, sym
)
139 * find a random value and set it to yes,
140 * set the rest to no so we have only one set
145 expr_list_for_each_sym(prop
->expr
, e
, sym
) {
147 sym
->def
[S_DEF_USER
].tri
= yes
;
148 csym
->def
[S_DEF_USER
].val
= sym
;
150 sym
->def
[S_DEF_USER
].tri
= no
;
152 sym
->flags
|= SYMBOL_DEF_USER
;
153 /* clear VALID to get value calculated */
154 sym
->flags
&= ~SYMBOL_VALID
;
156 csym
->flags
|= SYMBOL_DEF_USER
;
157 /* clear VALID to get value calculated */
158 csym
->flags
&= ~SYMBOL_VALID
;
171 static bool conf_set_all_new_symbols(enum conf_def_mode mode
)
173 struct symbol
*sym
, *csym
;
176 * can't go as the default in switch-case below, otherwise gcc whines
177 * about -Wmaybe-uninitialized
179 int pby
= 50; /* probability of bool = y */
180 int pty
= 33; /* probability of tristate = y */
181 int ptm
= 33; /* probability of tristate = m */
182 bool has_changed
= false;
184 if (mode
== def_random
) {
186 char *env
= getenv("KCONFIG_PROBABILITY");
189 while (env
&& *env
) {
191 int tmp
= strtol(env
, &endp
, 10);
193 if (tmp
>= 0 && tmp
<= 100) {
197 perror("KCONFIG_PROBABILITY");
200 env
= (*endp
== ':') ? endp
+ 1 : endp
;
222 if (pty
+ ptm
> 100) {
224 perror("KCONFIG_PROBABILITY");
229 for_all_symbols(i
, sym
) {
230 if (sym_has_value(sym
) || sym
->flags
& SYMBOL_VALID
)
232 switch (sym_get_type(sym
)) {
238 sym
->def
[S_DEF_USER
].tri
= yes
;
241 sym
->def
[S_DEF_USER
].tri
= mod
;
244 sym
->def
[S_DEF_USER
].tri
= no
;
247 sym
->def
[S_DEF_USER
].tri
= no
;
249 if (sym
->type
== S_TRISTATE
) {
251 sym
->def
[S_DEF_USER
].tri
= yes
;
252 else if (cnt
< pty
+ ptm
)
253 sym
->def
[S_DEF_USER
].tri
= mod
;
254 } else if (cnt
< pby
)
255 sym
->def
[S_DEF_USER
].tri
= yes
;
260 if (!(sym_is_choice(sym
) && mode
== def_random
))
261 sym
->flags
|= SYMBOL_DEF_USER
;
269 sym_clear_all_valid();
272 * We have different type of choice blocks.
273 * If curr.tri equals to mod then we can select several
274 * choice symbols in one block.
275 * In this case we do nothing.
276 * If curr.tri equals yes then only one symbol can be
277 * selected in a choice block and we set it to yes,
278 * and the rest to no.
280 if (mode
!= def_random
) {
281 for_all_symbols(i
, csym
) {
282 if ((sym_is_choice(csym
) && !sym_has_value(csym
)) ||
283 sym_is_choice_value(csym
))
284 csym
->flags
|= SYMBOL_NEED_SET_CHOICE_VALUES
;
288 for_all_symbols(i
, csym
) {
289 if (sym_has_value(csym
) || !sym_is_choice(csym
))
292 sym_calc_value(csym
);
293 if (mode
== def_random
)
294 has_changed
|= randomize_choice_values(csym
);
296 set_all_choice_values(csym
);
304 static void conf_rewrite_tristates(tristate old_val
, tristate new_val
)
309 for_all_symbols(i
, sym
) {
310 if (sym_get_type(sym
) == S_TRISTATE
&&
311 sym
->def
[S_DEF_USER
].tri
== old_val
)
312 sym
->def
[S_DEF_USER
].tri
= new_val
;
314 sym_clear_all_valid();
317 static int conf_askvalue(struct symbol
*sym
, const char *def
)
319 if (!sym_has_value(sym
))
325 if (!sym_is_changeable(sym
)) {
332 switch (input_mode
) {
335 if (sym_has_value(sym
)) {
342 xfgets(line
, sizeof(line
), stdin
);
349 static int conf_string(struct menu
*menu
)
351 struct symbol
*sym
= menu
->sym
;
355 printf("%*s%s ", indent
- 1, "", menu
->prompt
->text
);
356 printf("(%s) ", sym
->name
);
357 def
= sym_get_string_value(sym
);
359 printf("[%s] ", def
);
360 if (!conf_askvalue(sym
, def
))
367 if (line
[1] == '\n') {
374 line
[strlen(line
)-1] = 0;
377 if (def
&& sym_set_string_value(sym
, def
))
382 static int conf_sym(struct menu
*menu
)
384 struct symbol
*sym
= menu
->sym
;
385 tristate oldval
, newval
;
388 printf("%*s%s ", indent
- 1, "", menu
->prompt
->text
);
390 printf("(%s) ", sym
->name
);
392 oldval
= sym_get_tristate_value(sym
);
404 if (oldval
!= no
&& sym_tristate_within_range(sym
, no
))
406 if (oldval
!= mod
&& sym_tristate_within_range(sym
, mod
))
408 if (oldval
!= yes
&& sym_tristate_within_range(sym
, yes
))
411 if (!conf_askvalue(sym
, sym_get_string_value(sym
)))
419 if (!line
[1] || !strcmp(&line
[1], "o"))
431 if (!line
[1] || !strcmp(&line
[1], "es"))
442 if (sym_set_tristate_value(sym
, newval
))
449 static int conf_choice(struct menu
*menu
)
451 struct symbol
*sym
, *def_sym
;
456 is_new
= !sym_has_value(sym
);
457 if (sym_is_changeable(sym
)) {
460 switch (sym_get_tristate_value(sym
)) {
469 switch (sym_get_tristate_value(sym
)) {
473 printf("%*s%s\n", indent
- 1, "", menu_get_prompt(menu
));
483 printf("%*s%s\n", indent
- 1, "", menu_get_prompt(menu
));
484 def_sym
= sym_get_choice_value(sym
);
487 for (child
= menu
->list
; child
; child
= child
->next
) {
488 if (!menu_is_visible(child
))
491 printf("%*c %s\n", indent
, '*', menu_get_prompt(child
));
495 if (child
->sym
== def_sym
) {
497 printf("%*c", indent
, '>');
499 printf("%*c", indent
, ' ');
500 printf(" %d. %s", cnt
, menu_get_prompt(child
));
501 if (child
->sym
->name
)
502 printf(" (%s)", child
->sym
->name
);
503 if (!sym_has_value(child
->sym
))
507 printf("%*schoice", indent
- 1, "");
512 printf("[1-%d?]: ", cnt
);
513 switch (input_mode
) {
524 xfgets(line
, sizeof(line
), stdin
);
526 if (line
[0] == '?') {
532 else if (isdigit(line
[0]))
542 for (child
= menu
->list
; child
; child
= child
->next
) {
543 if (!child
->sym
|| !menu_is_visible(child
))
550 if (line
[0] && line
[strlen(line
) - 1] == '?') {
554 sym_set_tristate_value(child
->sym
, yes
);
555 for (child
= child
->list
; child
; child
= child
->next
) {
564 static void conf(struct menu
*menu
)
567 struct property
*prop
;
570 if (!menu_is_visible(menu
))
578 switch (prop
->type
) {
581 * Except in oldaskconfig mode, we show only menus that
582 * contain new symbols.
584 if (input_mode
!= oldaskconfig
&& rootEntry
!= menu
) {
590 prompt
= menu_get_prompt(menu
);
592 printf("%*c\n%*c %s\n%*c\n",
604 if (sym_is_choice(sym
)) {
606 if (sym
->curr
.tri
!= mod
)
625 for (child
= menu
->list
; child
; child
= child
->next
)
631 static void check_conf(struct menu
*menu
)
636 if (!menu_is_visible(menu
))
640 if (sym
&& !sym_has_value(sym
) &&
641 (sym_is_changeable(sym
) ||
642 (sym_is_choice(sym
) && sym_get_tristate_value(sym
) == yes
))) {
644 switch (input_mode
) {
647 print_symbol_for_listconfig(sym
);
656 printf("*\n* Restart config...\n*\n");
657 rootEntry
= menu_get_parent_menu(menu
);
663 for (child
= menu
->list
; child
; child
= child
->next
)
667 static const struct option long_opts
[] = {
668 {"help", no_argument
, NULL
, 'h'},
669 {"silent", no_argument
, NULL
, 's'},
670 {"oldaskconfig", no_argument
, &input_mode_opt
, oldaskconfig
},
671 {"oldconfig", no_argument
, &input_mode_opt
, oldconfig
},
672 {"syncconfig", no_argument
, &input_mode_opt
, syncconfig
},
673 {"defconfig", required_argument
, &input_mode_opt
, defconfig
},
674 {"savedefconfig", required_argument
, &input_mode_opt
, savedefconfig
},
675 {"allnoconfig", no_argument
, &input_mode_opt
, allnoconfig
},
676 {"allyesconfig", no_argument
, &input_mode_opt
, allyesconfig
},
677 {"allmodconfig", no_argument
, &input_mode_opt
, allmodconfig
},
678 {"alldefconfig", no_argument
, &input_mode_opt
, alldefconfig
},
679 {"randconfig", no_argument
, &input_mode_opt
, randconfig
},
680 {"listnewconfig", no_argument
, &input_mode_opt
, listnewconfig
},
681 {"helpnewconfig", no_argument
, &input_mode_opt
, helpnewconfig
},
682 {"olddefconfig", no_argument
, &input_mode_opt
, olddefconfig
},
683 {"yes2modconfig", no_argument
, &input_mode_opt
, yes2modconfig
},
684 {"mod2yesconfig", no_argument
, &input_mode_opt
, mod2yesconfig
},
685 {"mod2noconfig", no_argument
, &input_mode_opt
, mod2noconfig
},
689 static void conf_usage(const char *progname
)
691 printf("Usage: %s [options] <kconfig-file>\n", progname
);
693 printf("Generic options:\n");
694 printf(" -h, --help Print this message and exit.\n");
695 printf(" -s, --silent Do not print log.\n");
697 printf("Mode options:\n");
698 printf(" --listnewconfig List new options\n");
699 printf(" --helpnewconfig List new options and help text\n");
700 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
701 printf(" --oldconfig Update a configuration using a provided .config as base\n");
702 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
703 " include/{generated/,config/}\n");
704 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
705 printf(" --defconfig <file> New config with default defined in <file>\n");
706 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
707 printf(" --allnoconfig New config where all options are answered with no\n");
708 printf(" --allyesconfig New config where all options are answered with yes\n");
709 printf(" --allmodconfig New config where all options are answered with mod\n");
710 printf(" --alldefconfig New config with all symbols set to default\n");
711 printf(" --randconfig New config with random answer to all options\n");
712 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
713 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
714 printf(" --mod2noconfig Change answers from mod to no if possible\n");
715 printf(" (If none of the above is given, --oldaskconfig is the default)\n");
718 int main(int ac
, char **av
)
720 const char *progname
= av
[0];
722 const char *name
, *defconfig_file
= NULL
/* gcc uninit */;
723 int no_conf_write
= 0;
725 tty_stdio
= isatty(0) && isatty(1);
727 while ((opt
= getopt_long(ac
, av
, "hs", long_opts
, NULL
)) != -1) {
730 conf_usage(progname
);
734 conf_set_message_callback(NULL
);
737 input_mode
= input_mode_opt
;
738 switch (input_mode
) {
741 * syncconfig is invoked during the build stage.
742 * Suppress distracting
743 * "configuration written to ..."
745 conf_set_message_callback(NULL
);
750 defconfig_file
= optarg
;
753 set_randconfig_seed();
763 fprintf(stderr
, "%s: Kconfig file missing\n", av
[0]);
764 conf_usage(progname
);
767 conf_parse(av
[optind
]);
770 switch (input_mode
) {
772 if (conf_read(defconfig_file
)) {
775 "*** Can't find default configuration \"%s\"!\n"
798 name
= getenv("KCONFIG_ALLCONFIG");
801 if ((strcmp(name
, "") != 0) && (strcmp(name
, "1") != 0)) {
802 if (conf_read_simple(name
, S_DEF_USER
)) {
804 "*** Can't read seed configuration \"%s\"!\n",
810 switch (input_mode
) {
811 case allnoconfig
: name
= "allno.config"; break;
812 case allyesconfig
: name
= "allyes.config"; break;
813 case allmodconfig
: name
= "allmod.config"; break;
814 case alldefconfig
: name
= "alldef.config"; break;
815 case randconfig
: name
= "allrandom.config"; break;
818 if (conf_read_simple(name
, S_DEF_USER
) &&
819 conf_read_simple("all.config", S_DEF_USER
)) {
821 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
831 name
= getenv("KCONFIG_NOSILENTUPDATE");
833 if (conf_get_changed()) {
835 "\n*** The configuration requires explicit update.\n\n");
842 switch (input_mode
) {
844 conf_set_all_new_symbols(def_no
);
847 conf_set_all_new_symbols(def_yes
);
850 conf_set_all_new_symbols(def_mod
);
853 conf_set_all_new_symbols(def_default
);
856 /* Really nothing to do in this loop */
857 while (conf_set_all_new_symbols(def_random
)) ;
860 conf_set_all_new_symbols(def_default
);
865 conf_rewrite_tristates(yes
, mod
);
868 conf_rewrite_tristates(mod
, yes
);
871 conf_rewrite_tristates(mod
, no
);
874 rootEntry
= &rootmenu
;
876 input_mode
= oldconfig
;
882 /* Update until a loop caused no more changes */
885 check_conf(&rootmenu
);
893 if (input_mode
== savedefconfig
) {
894 if (conf_write_defconfig(defconfig_file
)) {
895 fprintf(stderr
, "\n*** Error while saving defconfig to: %s\n\n",
899 } else if (input_mode
!= listnewconfig
&& input_mode
!= helpnewconfig
) {
900 if (!no_conf_write
&& conf_write(NULL
)) {
901 fprintf(stderr
, "\n*** Error during writing of the configuration.\n\n");
906 * Create auto.conf if it does not exist.
907 * This prevents GNU Make 4.1 or older from emitting
908 * "include/config/auto.conf: No such file or directory"
909 * in the top-level Makefile
911 * syncconfig always creates or updates auto.conf because it is
912 * used during the build.
914 if (conf_write_autoconf(sync_kconfig
) && sync_kconfig
) {
916 "\n*** Error during sync of the configuration.\n\n");