1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
19 int kconfig_warnings
= 0;
21 static void conf(struct menu
*menu
);
22 static void check_conf(struct menu
*menu
);
41 static enum input_mode input_mode
= oldaskconfig
;
42 static int input_mode_opt
;
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 void set_randconfig_seed(void)
91 bool seed_set
= false;
93 env
= getenv("KCONFIG_SEED");
97 seed
= strtol(env
, &endp
, 0);
106 * Use microseconds derived seed, compensate for systems where it may
109 gettimeofday(&now
, NULL
);
110 seed
= (now
.tv_sec
+ 1) * (now
.tv_usec
+ 1);
113 printf("KCONFIG_SEED=0x%X\n", seed
);
117 static bool randomize_choice_values(struct symbol
*csym
)
119 struct property
*prop
;
125 * If choice is mod then we may have more items selected
126 * and if no then no-one.
127 * In both cases stop.
129 if (csym
->curr
.tri
!= yes
)
132 prop
= sym_get_choice_prop(csym
);
134 /* count entries in choice block */
136 expr_list_for_each_sym(prop
->expr
, e
, sym
)
140 * find a random value and set it to yes,
141 * set the rest to no so we have only one set
146 expr_list_for_each_sym(prop
->expr
, e
, sym
) {
148 sym
->def
[S_DEF_USER
].tri
= yes
;
149 csym
->def
[S_DEF_USER
].val
= sym
;
151 sym
->def
[S_DEF_USER
].tri
= no
;
153 sym
->flags
|= SYMBOL_DEF_USER
;
154 /* clear VALID to get value calculated */
155 sym
->flags
&= ~SYMBOL_VALID
;
157 csym
->flags
|= SYMBOL_DEF_USER
;
158 /* clear VALID to get value calculated */
159 csym
->flags
&= ~SYMBOL_VALID
;
174 static bool conf_set_all_new_symbols(enum conf_def_mode mode
)
176 struct symbol
*sym
, *csym
;
179 * can't go as the default in switch-case below, otherwise gcc whines
180 * about -Wmaybe-uninitialized
182 int pby
= 50; /* probability of bool = y */
183 int pty
= 33; /* probability of tristate = y */
184 int ptm
= 33; /* probability of tristate = m */
185 bool has_changed
= false;
187 if (mode
== def_random
) {
189 char *env
= getenv("KCONFIG_PROBABILITY");
192 while (env
&& *env
) {
194 int tmp
= strtol(env
, &endp
, 10);
196 if (tmp
>= 0 && tmp
<= 100) {
200 perror("KCONFIG_PROBABILITY");
203 env
= (*endp
== ':') ? endp
+ 1 : endp
;
225 if (pty
+ ptm
> 100) {
227 perror("KCONFIG_PROBABILITY");
232 for_all_symbols(i
, sym
) {
233 if (sym_has_value(sym
) || sym
->flags
& SYMBOL_VALID
)
235 switch (sym_get_type(sym
)) {
241 sym
->def
[S_DEF_USER
].tri
= yes
;
244 sym
->def
[S_DEF_USER
].tri
= mod
;
247 sym
->def
[S_DEF_USER
].tri
= no
;
250 sym
->def
[S_DEF_USER
].tri
= no
;
252 if (sym
->type
== S_TRISTATE
) {
254 sym
->def
[S_DEF_USER
].tri
= yes
;
255 else if (cnt
< pty
+ ptm
)
256 sym
->def
[S_DEF_USER
].tri
= mod
;
257 } else if (cnt
< pby
)
258 sym
->def
[S_DEF_USER
].tri
= yes
;
263 if (!(sym_is_choice(sym
) && mode
== def_random
))
264 sym
->flags
|= SYMBOL_DEF_USER
;
272 sym_clear_all_valid();
275 * We have different type of choice blocks.
276 * If curr.tri equals to mod then we can select several
277 * choice symbols in one block.
278 * In this case we do nothing.
279 * If curr.tri equals yes then only one symbol can be
280 * selected in a choice block and we set it to yes,
281 * and the rest to no.
283 if (mode
!= def_random
) {
284 for_all_symbols(i
, csym
) {
285 if ((sym_is_choice(csym
) && !sym_has_value(csym
)) ||
286 sym_is_choice_value(csym
))
287 csym
->flags
|= SYMBOL_NEED_SET_CHOICE_VALUES
;
291 for_all_symbols(i
, csym
) {
292 if (sym_has_value(csym
) || !sym_is_choice(csym
))
295 sym_calc_value(csym
);
296 if (mode
== def_random
)
297 has_changed
|= randomize_choice_values(csym
);
299 set_all_choice_values(csym
);
307 static void conf_rewrite_mod_or_yes(enum conf_def_mode mode
)
311 tristate old_val
= (mode
== def_y2m
) ? yes
: mod
;
312 tristate new_val
= (mode
== def_y2m
) ? mod
: yes
;
314 for_all_symbols(i
, sym
) {
315 if (sym_get_type(sym
) == S_TRISTATE
&&
316 sym
->def
[S_DEF_USER
].tri
== old_val
)
317 sym
->def
[S_DEF_USER
].tri
= new_val
;
319 sym_clear_all_valid();
322 static int conf_askvalue(struct symbol
*sym
, const char *def
)
324 if (!sym_has_value(sym
))
330 if (!sym_is_changeable(sym
)) {
337 switch (input_mode
) {
340 if (sym_has_value(sym
)) {
347 xfgets(line
, sizeof(line
), stdin
);
354 static int conf_string(struct menu
*menu
)
356 struct symbol
*sym
= menu
->sym
;
360 printf("%*s%s ", indent
- 1, "", menu
->prompt
->text
);
361 printf("(%s) ", sym
->name
);
362 def
= sym_get_string_value(sym
);
364 printf("[%s] ", def
);
365 if (!conf_askvalue(sym
, def
))
372 if (line
[1] == '\n') {
379 line
[strlen(line
)-1] = 0;
382 if (def
&& sym_set_string_value(sym
, def
))
387 static int conf_sym(struct menu
*menu
)
389 struct symbol
*sym
= menu
->sym
;
390 tristate oldval
, newval
;
393 printf("%*s%s ", indent
- 1, "", menu
->prompt
->text
);
395 printf("(%s) ", sym
->name
);
397 oldval
= sym_get_tristate_value(sym
);
409 if (oldval
!= no
&& sym_tristate_within_range(sym
, no
))
411 if (oldval
!= mod
&& sym_tristate_within_range(sym
, mod
))
413 if (oldval
!= yes
&& sym_tristate_within_range(sym
, yes
))
416 if (!conf_askvalue(sym
, sym_get_string_value(sym
)))
424 if (!line
[1] || !strcmp(&line
[1], "o"))
436 if (!line
[1] || !strcmp(&line
[1], "es"))
447 if (sym_set_tristate_value(sym
, newval
))
454 static int conf_choice(struct menu
*menu
)
456 struct symbol
*sym
, *def_sym
;
461 is_new
= !sym_has_value(sym
);
462 if (sym_is_changeable(sym
)) {
465 switch (sym_get_tristate_value(sym
)) {
474 switch (sym_get_tristate_value(sym
)) {
478 printf("%*s%s\n", indent
- 1, "", menu_get_prompt(menu
));
488 printf("%*s%s\n", indent
- 1, "", menu_get_prompt(menu
));
489 def_sym
= sym_get_choice_value(sym
);
492 for (child
= menu
->list
; child
; child
= child
->next
) {
493 if (!menu_is_visible(child
))
496 printf("%*c %s\n", indent
, '*', menu_get_prompt(child
));
500 if (child
->sym
== def_sym
) {
502 printf("%*c", indent
, '>');
504 printf("%*c", indent
, ' ');
505 printf(" %d. %s", cnt
, menu_get_prompt(child
));
506 if (child
->sym
->name
)
507 printf(" (%s)", child
->sym
->name
);
508 if (!sym_has_value(child
->sym
))
512 printf("%*schoice", indent
- 1, "");
517 printf("[1-%d?]: ", cnt
);
518 switch (input_mode
) {
529 xfgets(line
, sizeof(line
), stdin
);
531 if (line
[0] == '?') {
537 else if (isdigit(line
[0]))
547 for (child
= menu
->list
; child
; child
= child
->next
) {
548 if (!child
->sym
|| !menu_is_visible(child
))
555 if (line
[0] && line
[strlen(line
) - 1] == '?') {
559 sym_set_choice_value(sym
, child
->sym
);
560 for (child
= child
->list
; child
; child
= child
->next
) {
569 static void conf(struct menu
*menu
)
572 struct property
*prop
;
575 if (!menu_is_visible(menu
))
583 switch (prop
->type
) {
586 * Except in oldaskconfig mode, we show only menus that
587 * contain new symbols.
589 if (input_mode
!= oldaskconfig
&& rootEntry
!= menu
) {
595 prompt
= menu_get_prompt(menu
);
597 printf("%*c\n%*c %s\n%*c\n",
609 if (sym_is_choice(sym
)) {
611 if (sym
->curr
.tri
!= mod
)
630 for (child
= menu
->list
; child
; child
= child
->next
)
636 static void check_conf(struct menu
*menu
)
641 if (!menu_is_visible(menu
))
645 if (sym
&& !sym_has_value(sym
) &&
646 (sym_is_changeable(sym
) ||
647 (sym_is_choice(sym
) && sym_get_tristate_value(sym
) == yes
))) {
649 switch (input_mode
) {
654 if (sym
->type
== S_STRING
) {
655 str
= sym_get_string_value(sym
);
656 str
= sym_escape_string_value(str
);
657 printf("%s%s=%s\n", CONFIG_
, sym
->name
, str
);
660 str
= sym_get_string_value(sym
);
661 printf("%s%s=%s\n", CONFIG_
, sym
->name
, str
);
672 printf("*\n* Restart config...\n*\n");
673 rootEntry
= menu_get_parent_menu(menu
);
679 for (child
= menu
->list
; child
; child
= child
->next
)
683 static struct option long_opts
[] = {
684 {"help", no_argument
, NULL
, 'h'},
685 {"silent", no_argument
, NULL
, 's'},
686 {"oldaskconfig", no_argument
, &input_mode_opt
, oldaskconfig
},
687 {"oldconfig", no_argument
, &input_mode_opt
, oldconfig
},
688 {"syncconfig", no_argument
, &input_mode_opt
, syncconfig
},
689 {"defconfig", required_argument
, &input_mode_opt
, defconfig
},
690 {"savedefconfig", required_argument
, &input_mode_opt
, savedefconfig
},
691 {"allnoconfig", no_argument
, &input_mode_opt
, allnoconfig
},
692 {"allyesconfig", no_argument
, &input_mode_opt
, allyesconfig
},
693 {"allmodconfig", no_argument
, &input_mode_opt
, allmodconfig
},
694 {"alldefconfig", no_argument
, &input_mode_opt
, alldefconfig
},
695 {"randconfig", no_argument
, &input_mode_opt
, randconfig
},
696 {"listnewconfig", no_argument
, &input_mode_opt
, listnewconfig
},
697 {"helpnewconfig", no_argument
, &input_mode_opt
, helpnewconfig
},
698 {"olddefconfig", no_argument
, &input_mode_opt
, olddefconfig
},
699 {"yes2modconfig", no_argument
, &input_mode_opt
, yes2modconfig
},
700 {"mod2yesconfig", no_argument
, &input_mode_opt
, mod2yesconfig
},
704 static void conf_usage(const char *progname
)
706 printf("Usage: %s [options] <kconfig-file>\n", progname
);
708 printf("Generic options:\n");
709 printf(" -h, --help Print this message and exit.\n");
710 printf(" -s, --silent Do not print log.\n");
712 printf("Mode options:\n");
713 printf(" --listnewconfig List new options\n");
714 printf(" --helpnewconfig List new options and help text\n");
715 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
716 printf(" --oldconfig Update a configuration using a provided .config as base\n");
717 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
718 " include/{generated/,config/}\n");
719 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
720 printf(" --defconfig <file> New config with default defined in <file>\n");
721 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
722 printf(" --allnoconfig New config where all options are answered with no\n");
723 printf(" --allyesconfig New config where all options are answered with yes\n");
724 printf(" --allmodconfig New config where all options are answered with mod\n");
725 printf(" --alldefconfig New config with all symbols set to default\n");
726 printf(" --randconfig New config with random answer to all options\n");
727 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
728 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
729 printf(" (If none of the above is given, --oldaskconfig is the default)\n");
732 int main(int ac
, char **av
)
734 const char *progname
= av
[0];
736 const char *name
, *defconfig_file
= NULL
/* gcc uninit */;
738 int no_conf_write
= 0;
740 tty_stdio
= isatty(0) && isatty(1);
742 while ((opt
= getopt_long(ac
, av
, "hs", long_opts
, NULL
)) != -1) {
745 conf_usage(progname
);
749 conf_set_message_callback(NULL
);
752 input_mode
= input_mode_opt
;
753 switch (input_mode
) {
756 * syncconfig is invoked during the build stage.
757 * Suppress distracting
758 * "configuration written to ..."
760 conf_set_message_callback(NULL
);
765 defconfig_file
= optarg
;
768 set_randconfig_seed();
778 fprintf(stderr
, "%s: Kconfig file missing\n", av
[0]);
779 conf_usage(progname
);
782 conf_parse(av
[optind
]);
785 switch (input_mode
) {
787 if (conf_read(defconfig_file
)) {
790 "*** Can't find default configuration \"%s\"!\n"
812 name
= getenv("KCONFIG_ALLCONFIG");
815 if ((strcmp(name
, "") != 0) && (strcmp(name
, "1") != 0)) {
816 if (conf_read_simple(name
, S_DEF_USER
)) {
818 "*** Can't read seed configuration \"%s\"!\n",
824 switch (input_mode
) {
825 case allnoconfig
: name
= "allno.config"; break;
826 case allyesconfig
: name
= "allyes.config"; break;
827 case allmodconfig
: name
= "allmod.config"; break;
828 case alldefconfig
: name
= "alldef.config"; break;
829 case randconfig
: name
= "allrandom.config"; break;
832 if (conf_read_simple(name
, S_DEF_USER
) &&
833 conf_read_simple("all.config", S_DEF_USER
)) {
835 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
844 env
= getenv("KCONFIG_STRICT");
845 if (env
&& *env
&& kconfig_warnings
) {
846 fprintf(stderr
, "\n*** ERROR: %d warnings encountered, and "
847 "warnings are errors.\n\n", kconfig_warnings
);
852 name
= getenv("KCONFIG_NOSILENTUPDATE");
854 if (conf_get_changed()) {
856 "\n*** The configuration requires explicit update.\n\n");
863 switch (input_mode
) {
865 conf_set_all_new_symbols(def_no
);
868 conf_set_all_new_symbols(def_yes
);
871 conf_set_all_new_symbols(def_mod
);
874 conf_set_all_new_symbols(def_default
);
877 /* Really nothing to do in this loop */
878 while (conf_set_all_new_symbols(def_random
)) ;
881 conf_set_all_new_symbols(def_default
);
886 conf_rewrite_mod_or_yes(def_y2m
);
889 conf_rewrite_mod_or_yes(def_m2y
);
892 rootEntry
= &rootmenu
;
894 input_mode
= oldconfig
;
900 /* Update until a loop caused no more changes */
903 check_conf(&rootmenu
);
911 if (input_mode
== savedefconfig
) {
912 if (conf_write_defconfig(defconfig_file
)) {
913 fprintf(stderr
, "\n*** Error while saving defconfig to: %s\n\n",
917 } else if (input_mode
!= listnewconfig
&& input_mode
!= helpnewconfig
) {
918 if (!no_conf_write
&& conf_write(NULL
)) {
919 fprintf(stderr
, "\n*** Error during writing of the configuration.\n\n");
924 * Create auto.conf if it does not exist.
925 * This prevents GNU Make 4.1 or older from emitting
926 * "include/config/auto.conf: No such file or directory"
927 * in the top-level Makefile
929 * syncconfig always creates or updates auto.conf because it is
930 * used during the build.
932 if (conf_write_autoconf(sync_kconfig
) && sync_kconfig
) {
934 "\n*** Error during sync of the configuration.\n\n");