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
);
42 static enum input_mode input_mode
= oldaskconfig
;
43 static int input_mode_opt
;
44 static int indent
= 1;
46 static int sync_kconfig
;
48 static char line
[PATH_MAX
];
49 static struct menu
*rootEntry
;
51 static void print_help(struct menu
*menu
)
53 struct gstr help
= str_new();
55 menu_get_ext_help(menu
, &help
);
57 printf("\n%s\n", str_get(&help
));
61 static void strip(char *str
)
70 memmove(str
, p
, l
+ 1);
78 /* Helper function to facilitate fgets() by Jean Sacren. */
79 static void xfgets(char *str
, int size
, FILE *in
)
81 if (!fgets(str
, size
, in
))
82 fprintf(stderr
, "\nError in reading or end of file.\n");
88 static void set_randconfig_seed(void)
92 bool seed_set
= false;
94 env
= getenv("KCONFIG_SEED");
98 seed
= strtol(env
, &endp
, 0);
107 * Use microseconds derived seed, compensate for systems where it may
110 gettimeofday(&now
, NULL
);
111 seed
= (now
.tv_sec
+ 1) * (now
.tv_usec
+ 1);
114 printf("KCONFIG_SEED=0x%X\n", seed
);
118 static bool randomize_choice_values(struct symbol
*csym
)
120 struct property
*prop
;
126 * If choice is mod then we may have more items selected
127 * and if no then no-one.
128 * In both cases stop.
130 if (csym
->curr
.tri
!= yes
)
133 prop
= sym_get_choice_prop(csym
);
135 /* count entries in choice block */
137 expr_list_for_each_sym(prop
->expr
, e
, sym
)
141 * find a random value and set it to yes,
142 * set the rest to no so we have only one set
147 expr_list_for_each_sym(prop
->expr
, e
, sym
) {
149 sym
->def
[S_DEF_USER
].tri
= yes
;
150 csym
->def
[S_DEF_USER
].val
= sym
;
152 sym
->def
[S_DEF_USER
].tri
= no
;
154 sym
->flags
|= SYMBOL_DEF_USER
;
155 /* clear VALID to get value calculated */
156 sym
->flags
&= ~SYMBOL_VALID
;
158 csym
->flags
|= SYMBOL_DEF_USER
;
159 /* clear VALID to get value calculated */
160 csym
->flags
&= ~SYMBOL_VALID
;
173 static bool conf_set_all_new_symbols(enum conf_def_mode mode
)
175 struct symbol
*sym
, *csym
;
178 * can't go as the default in switch-case below, otherwise gcc whines
179 * about -Wmaybe-uninitialized
181 int pby
= 50; /* probability of bool = y */
182 int pty
= 33; /* probability of tristate = y */
183 int ptm
= 33; /* probability of tristate = m */
184 bool has_changed
= false;
186 if (mode
== def_random
) {
188 char *env
= getenv("KCONFIG_PROBABILITY");
191 while (env
&& *env
) {
193 int tmp
= strtol(env
, &endp
, 10);
195 if (tmp
>= 0 && tmp
<= 100) {
199 perror("KCONFIG_PROBABILITY");
202 env
= (*endp
== ':') ? endp
+ 1 : endp
;
224 if (pty
+ ptm
> 100) {
226 perror("KCONFIG_PROBABILITY");
231 for_all_symbols(i
, sym
) {
232 if (sym_has_value(sym
) || sym
->flags
& SYMBOL_VALID
)
234 switch (sym_get_type(sym
)) {
240 sym
->def
[S_DEF_USER
].tri
= yes
;
243 sym
->def
[S_DEF_USER
].tri
= mod
;
246 sym
->def
[S_DEF_USER
].tri
= no
;
249 sym
->def
[S_DEF_USER
].tri
= no
;
251 if (sym
->type
== S_TRISTATE
) {
253 sym
->def
[S_DEF_USER
].tri
= yes
;
254 else if (cnt
< pty
+ ptm
)
255 sym
->def
[S_DEF_USER
].tri
= mod
;
256 } else if (cnt
< pby
)
257 sym
->def
[S_DEF_USER
].tri
= yes
;
262 if (!(sym_is_choice(sym
) && mode
== def_random
))
263 sym
->flags
|= SYMBOL_DEF_USER
;
271 sym_clear_all_valid();
274 * We have different type of choice blocks.
275 * If curr.tri equals to mod then we can select several
276 * choice symbols in one block.
277 * In this case we do nothing.
278 * If curr.tri equals yes then only one symbol can be
279 * selected in a choice block and we set it to yes,
280 * and the rest to no.
282 if (mode
!= def_random
) {
283 for_all_symbols(i
, csym
) {
284 if ((sym_is_choice(csym
) && !sym_has_value(csym
)) ||
285 sym_is_choice_value(csym
))
286 csym
->flags
|= SYMBOL_NEED_SET_CHOICE_VALUES
;
290 for_all_symbols(i
, csym
) {
291 if (sym_has_value(csym
) || !sym_is_choice(csym
))
294 sym_calc_value(csym
);
295 if (mode
== def_random
)
296 has_changed
|= randomize_choice_values(csym
);
298 set_all_choice_values(csym
);
306 static void conf_rewrite_tristates(tristate old_val
, tristate new_val
)
311 for_all_symbols(i
, sym
) {
312 if (sym_get_type(sym
) == S_TRISTATE
&&
313 sym
->def
[S_DEF_USER
].tri
== old_val
)
314 sym
->def
[S_DEF_USER
].tri
= new_val
;
316 sym_clear_all_valid();
319 static int conf_askvalue(struct symbol
*sym
, const char *def
)
321 if (!sym_has_value(sym
))
327 if (!sym_is_changeable(sym
)) {
334 switch (input_mode
) {
337 if (sym_has_value(sym
)) {
344 xfgets(line
, sizeof(line
), stdin
);
351 static int conf_string(struct menu
*menu
)
353 struct symbol
*sym
= menu
->sym
;
357 printf("%*s%s ", indent
- 1, "", menu
->prompt
->text
);
358 printf("(%s) ", sym
->name
);
359 def
= sym_get_string_value(sym
);
361 printf("[%s] ", def
);
362 if (!conf_askvalue(sym
, def
))
369 if (line
[1] == '\n') {
376 line
[strlen(line
)-1] = 0;
379 if (def
&& sym_set_string_value(sym
, def
))
384 static int conf_sym(struct menu
*menu
)
386 struct symbol
*sym
= menu
->sym
;
387 tristate oldval
, newval
;
390 printf("%*s%s ", indent
- 1, "", menu
->prompt
->text
);
392 printf("(%s) ", sym
->name
);
394 oldval
= sym_get_tristate_value(sym
);
406 if (oldval
!= no
&& sym_tristate_within_range(sym
, no
))
408 if (oldval
!= mod
&& sym_tristate_within_range(sym
, mod
))
410 if (oldval
!= yes
&& sym_tristate_within_range(sym
, yes
))
413 if (!conf_askvalue(sym
, sym_get_string_value(sym
)))
421 if (!line
[1] || !strcmp(&line
[1], "o"))
433 if (!line
[1] || !strcmp(&line
[1], "es"))
444 if (sym_set_tristate_value(sym
, newval
))
451 static int conf_choice(struct menu
*menu
)
453 struct symbol
*sym
, *def_sym
;
458 is_new
= !sym_has_value(sym
);
459 if (sym_is_changeable(sym
)) {
462 switch (sym_get_tristate_value(sym
)) {
471 switch (sym_get_tristate_value(sym
)) {
475 printf("%*s%s\n", indent
- 1, "", menu_get_prompt(menu
));
485 printf("%*s%s\n", indent
- 1, "", menu_get_prompt(menu
));
486 def_sym
= sym_get_choice_value(sym
);
489 for (child
= menu
->list
; child
; child
= child
->next
) {
490 if (!menu_is_visible(child
))
493 printf("%*c %s\n", indent
, '*', menu_get_prompt(child
));
497 if (child
->sym
== def_sym
) {
499 printf("%*c", indent
, '>');
501 printf("%*c", indent
, ' ');
502 printf(" %d. %s", cnt
, menu_get_prompt(child
));
503 if (child
->sym
->name
)
504 printf(" (%s)", child
->sym
->name
);
505 if (!sym_has_value(child
->sym
))
509 printf("%*schoice", indent
- 1, "");
514 printf("[1-%d?]: ", cnt
);
515 switch (input_mode
) {
526 xfgets(line
, sizeof(line
), stdin
);
528 if (line
[0] == '?') {
534 else if (isdigit(line
[0]))
544 for (child
= menu
->list
; child
; child
= child
->next
) {
545 if (!child
->sym
|| !menu_is_visible(child
))
552 if (line
[0] && line
[strlen(line
) - 1] == '?') {
556 sym_set_choice_value(sym
, child
->sym
);
557 for (child
= child
->list
; child
; child
= child
->next
) {
566 static void conf(struct menu
*menu
)
569 struct property
*prop
;
572 if (!menu_is_visible(menu
))
580 switch (prop
->type
) {
583 * Except in oldaskconfig mode, we show only menus that
584 * contain new symbols.
586 if (input_mode
!= oldaskconfig
&& rootEntry
!= menu
) {
592 prompt
= menu_get_prompt(menu
);
594 printf("%*c\n%*c %s\n%*c\n",
606 if (sym_is_choice(sym
)) {
608 if (sym
->curr
.tri
!= mod
)
627 for (child
= menu
->list
; child
; child
= child
->next
)
633 static void check_conf(struct menu
*menu
)
638 if (!menu_is_visible(menu
))
642 if (sym
&& !sym_has_value(sym
) &&
643 (sym_is_changeable(sym
) ||
644 (sym_is_choice(sym
) && sym_get_tristate_value(sym
) == yes
))) {
646 switch (input_mode
) {
649 print_symbol_for_listconfig(sym
);
658 printf("*\n* Restart config...\n*\n");
659 rootEntry
= menu_get_parent_menu(menu
);
665 for (child
= menu
->list
; child
; child
= child
->next
)
669 static const struct option long_opts
[] = {
670 {"help", no_argument
, NULL
, 'h'},
671 {"silent", no_argument
, NULL
, 's'},
672 {"oldaskconfig", no_argument
, &input_mode_opt
, oldaskconfig
},
673 {"oldconfig", no_argument
, &input_mode_opt
, oldconfig
},
674 {"syncconfig", no_argument
, &input_mode_opt
, syncconfig
},
675 {"defconfig", required_argument
, &input_mode_opt
, defconfig
},
676 {"savedefconfig", required_argument
, &input_mode_opt
, savedefconfig
},
677 {"allnoconfig", no_argument
, &input_mode_opt
, allnoconfig
},
678 {"allyesconfig", no_argument
, &input_mode_opt
, allyesconfig
},
679 {"allmodconfig", no_argument
, &input_mode_opt
, allmodconfig
},
680 {"alldefconfig", no_argument
, &input_mode_opt
, alldefconfig
},
681 {"randconfig", no_argument
, &input_mode_opt
, randconfig
},
682 {"listnewconfig", no_argument
, &input_mode_opt
, listnewconfig
},
683 {"helpnewconfig", no_argument
, &input_mode_opt
, helpnewconfig
},
684 {"olddefconfig", no_argument
, &input_mode_opt
, olddefconfig
},
685 {"yes2modconfig", no_argument
, &input_mode_opt
, yes2modconfig
},
686 {"mod2yesconfig", no_argument
, &input_mode_opt
, mod2yesconfig
},
687 {"mod2noconfig", no_argument
, &input_mode_opt
, mod2noconfig
},
691 static void conf_usage(const char *progname
)
693 printf("Usage: %s [options] <kconfig-file>\n", progname
);
695 printf("Generic options:\n");
696 printf(" -h, --help Print this message and exit.\n");
697 printf(" -s, --silent Do not print log.\n");
699 printf("Mode options:\n");
700 printf(" --listnewconfig List new options\n");
701 printf(" --helpnewconfig List new options and help text\n");
702 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
703 printf(" --oldconfig Update a configuration using a provided .config as base\n");
704 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
705 " include/{generated/,config/}\n");
706 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
707 printf(" --defconfig <file> New config with default defined in <file>\n");
708 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
709 printf(" --allnoconfig New config where all options are answered with no\n");
710 printf(" --allyesconfig New config where all options are answered with yes\n");
711 printf(" --allmodconfig New config where all options are answered with mod\n");
712 printf(" --alldefconfig New config with all symbols set to default\n");
713 printf(" --randconfig New config with random answer to all options\n");
714 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
715 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
716 printf(" --mod2noconfig Change answers from mod to no if possible\n");
717 printf(" (If none of the above is given, --oldaskconfig is the default)\n");
720 int main(int ac
, char **av
)
722 const char *progname
= av
[0];
724 const char *name
, *defconfig_file
= NULL
/* gcc uninit */;
726 int no_conf_write
= 0;
728 tty_stdio
= isatty(0) && isatty(1);
730 while ((opt
= getopt_long(ac
, av
, "hs", long_opts
, NULL
)) != -1) {
733 conf_usage(progname
);
737 conf_set_message_callback(NULL
);
740 input_mode
= input_mode_opt
;
741 switch (input_mode
) {
744 * syncconfig is invoked during the build stage.
745 * Suppress distracting
746 * "configuration written to ..."
748 conf_set_message_callback(NULL
);
753 defconfig_file
= optarg
;
756 set_randconfig_seed();
766 fprintf(stderr
, "%s: Kconfig file missing\n", av
[0]);
767 conf_usage(progname
);
770 conf_parse(av
[optind
]);
773 switch (input_mode
) {
775 if (conf_read(defconfig_file
)) {
778 "*** Can't find default configuration \"%s\"!\n"
801 name
= getenv("KCONFIG_ALLCONFIG");
804 if ((strcmp(name
, "") != 0) && (strcmp(name
, "1") != 0)) {
805 if (conf_read_simple(name
, S_DEF_USER
)) {
807 "*** Can't read seed configuration \"%s\"!\n",
813 switch (input_mode
) {
814 case allnoconfig
: name
= "allno.config"; break;
815 case allyesconfig
: name
= "allyes.config"; break;
816 case allmodconfig
: name
= "allmod.config"; break;
817 case alldefconfig
: name
= "alldef.config"; break;
818 case randconfig
: name
= "allrandom.config"; break;
821 if (conf_read_simple(name
, S_DEF_USER
) &&
822 conf_read_simple("all.config", S_DEF_USER
)) {
824 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
833 env
= getenv("KCONFIG_STRICT");
834 if (env
&& *env
&& kconfig_warnings
) {
835 fprintf(stderr
, "\n*** ERROR: %d warnings encountered, and "
836 "warnings are errors.\n\n", kconfig_warnings
);
841 name
= getenv("KCONFIG_NOSILENTUPDATE");
843 if (conf_get_changed()) {
845 "\n*** The configuration requires explicit update.\n\n");
852 switch (input_mode
) {
854 conf_set_all_new_symbols(def_no
);
857 conf_set_all_new_symbols(def_yes
);
860 conf_set_all_new_symbols(def_mod
);
863 conf_set_all_new_symbols(def_default
);
866 /* Really nothing to do in this loop */
867 while (conf_set_all_new_symbols(def_random
)) ;
870 conf_set_all_new_symbols(def_default
);
875 conf_rewrite_tristates(yes
, mod
);
878 conf_rewrite_tristates(mod
, yes
);
881 conf_rewrite_tristates(mod
, no
);
884 rootEntry
= &rootmenu
;
886 input_mode
= oldconfig
;
892 /* Update until a loop caused no more changes */
895 check_conf(&rootmenu
);
903 if (input_mode
== savedefconfig
) {
904 if (conf_write_defconfig(defconfig_file
)) {
905 fprintf(stderr
, "\n*** Error while saving defconfig to: %s\n\n",
909 } else if (input_mode
!= listnewconfig
&& input_mode
!= helpnewconfig
) {
910 if (!no_conf_write
&& conf_write(NULL
)) {
911 fprintf(stderr
, "\n*** Error during writing of the configuration.\n\n");
916 * Create auto.conf if it does not exist.
917 * This prevents GNU Make 4.1 or older from emitting
918 * "include/config/auto.conf: No such file or directory"
919 * in the top-level Makefile
921 * syncconfig always creates or updates auto.conf because it is
922 * used during the build.
924 if (conf_write_autoconf(sync_kconfig
) && sync_kconfig
) {
926 "\n*** Error during sync of the configuration.\n\n");