1 // SPDX-License-Identifier: GPL-2.0
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
);
39 static enum input_mode input_mode
= oldaskconfig
;
40 static int input_mode_opt
;
41 static int indent
= 1;
43 static int sync_kconfig
;
45 static char line
[PATH_MAX
];
46 static struct menu
*rootEntry
;
48 static void print_help(struct menu
*menu
)
50 struct gstr help
= str_new();
52 menu_get_ext_help(menu
, &help
);
54 printf("\n%s\n", str_get(&help
));
58 static void strip(char *str
)
67 memmove(str
, p
, l
+ 1);
75 /* Helper function to facilitate fgets() by Jean Sacren. */
76 static void xfgets(char *str
, int size
, FILE *in
)
78 if (!fgets(str
, size
, in
))
79 fprintf(stderr
, "\nError in reading or end of file.\n");
85 static void set_randconfig_seed(void)
89 bool seed_set
= false;
91 env
= getenv("KCONFIG_SEED");
95 seed
= strtol(env
, &endp
, 0);
104 * Use microseconds derived seed, compensate for systems where it may
107 gettimeofday(&now
, NULL
);
108 seed
= (now
.tv_sec
+ 1) * (now
.tv_usec
+ 1);
111 printf("KCONFIG_SEED=0x%X\n", seed
);
115 static bool randomize_choice_values(struct symbol
*csym
)
117 struct property
*prop
;
123 * If choice is mod then we may have more items selected
124 * and if no then no-one.
125 * In both cases stop.
127 if (csym
->curr
.tri
!= yes
)
130 prop
= sym_get_choice_prop(csym
);
132 /* count entries in choice block */
134 expr_list_for_each_sym(prop
->expr
, e
, sym
)
138 * find a random value and set it to yes,
139 * set the rest to no so we have only one set
144 expr_list_for_each_sym(prop
->expr
, e
, sym
) {
146 sym
->def
[S_DEF_USER
].tri
= yes
;
147 csym
->def
[S_DEF_USER
].val
= sym
;
149 sym
->def
[S_DEF_USER
].tri
= no
;
151 sym
->flags
|= SYMBOL_DEF_USER
;
152 /* clear VALID to get value calculated */
153 sym
->flags
&= ~SYMBOL_VALID
;
155 csym
->flags
|= SYMBOL_DEF_USER
;
156 /* clear VALID to get value calculated */
157 csym
->flags
&= ~SYMBOL_VALID
;
172 static bool conf_set_all_new_symbols(enum conf_def_mode mode
)
174 struct symbol
*sym
, *csym
;
177 * can't go as the default in switch-case below, otherwise gcc whines
178 * about -Wmaybe-uninitialized
180 int pby
= 50; /* probability of bool = y */
181 int pty
= 33; /* probability of tristate = y */
182 int ptm
= 33; /* probability of tristate = m */
183 bool has_changed
= false;
185 if (mode
== def_random
) {
187 char *env
= getenv("KCONFIG_PROBABILITY");
190 while (env
&& *env
) {
192 int tmp
= strtol(env
, &endp
, 10);
194 if (tmp
>= 0 && tmp
<= 100) {
198 perror("KCONFIG_PROBABILITY");
201 env
= (*endp
== ':') ? endp
+ 1 : endp
;
223 if (pty
+ ptm
> 100) {
225 perror("KCONFIG_PROBABILITY");
230 for_all_symbols(i
, sym
) {
231 if (sym_has_value(sym
) || sym
->flags
& SYMBOL_VALID
)
233 switch (sym_get_type(sym
)) {
239 sym
->def
[S_DEF_USER
].tri
= yes
;
242 sym
->def
[S_DEF_USER
].tri
= mod
;
245 sym
->def
[S_DEF_USER
].tri
= no
;
248 sym
->def
[S_DEF_USER
].tri
= no
;
250 if (sym
->type
== S_TRISTATE
) {
252 sym
->def
[S_DEF_USER
].tri
= yes
;
253 else if (cnt
< pty
+ ptm
)
254 sym
->def
[S_DEF_USER
].tri
= mod
;
255 } else if (cnt
< pby
)
256 sym
->def
[S_DEF_USER
].tri
= yes
;
261 if (!(sym_is_choice(sym
) && mode
== def_random
))
262 sym
->flags
|= SYMBOL_DEF_USER
;
270 sym_clear_all_valid();
273 * We have different type of choice blocks.
274 * If curr.tri equals to mod then we can select several
275 * choice symbols in one block.
276 * In this case we do nothing.
277 * If curr.tri equals yes then only one symbol can be
278 * selected in a choice block and we set it to yes,
279 * and the rest to no.
281 if (mode
!= def_random
) {
282 for_all_symbols(i
, csym
) {
283 if ((sym_is_choice(csym
) && !sym_has_value(csym
)) ||
284 sym_is_choice_value(csym
))
285 csym
->flags
|= SYMBOL_NEED_SET_CHOICE_VALUES
;
289 for_all_symbols(i
, csym
) {
290 if (sym_has_value(csym
) || !sym_is_choice(csym
))
293 sym_calc_value(csym
);
294 if (mode
== def_random
)
295 has_changed
|= randomize_choice_values(csym
);
297 set_all_choice_values(csym
);
305 static void conf_rewrite_mod_or_yes(enum conf_def_mode mode
)
309 tristate old_val
= (mode
== def_y2m
) ? yes
: mod
;
310 tristate new_val
= (mode
== def_y2m
) ? mod
: yes
;
312 for_all_symbols(i
, sym
) {
313 if (sym_get_type(sym
) == S_TRISTATE
&&
314 sym
->def
[S_DEF_USER
].tri
== old_val
)
315 sym
->def
[S_DEF_USER
].tri
= new_val
;
317 sym_clear_all_valid();
320 static int conf_askvalue(struct symbol
*sym
, const char *def
)
322 if (!sym_has_value(sym
))
328 if (!sym_is_changeable(sym
)) {
335 switch (input_mode
) {
338 if (sym_has_value(sym
)) {
345 xfgets(line
, sizeof(line
), stdin
);
352 static int conf_string(struct menu
*menu
)
354 struct symbol
*sym
= menu
->sym
;
358 printf("%*s%s ", indent
- 1, "", menu
->prompt
->text
);
359 printf("(%s) ", sym
->name
);
360 def
= sym_get_string_value(sym
);
362 printf("[%s] ", def
);
363 if (!conf_askvalue(sym
, def
))
370 if (line
[1] == '\n') {
377 line
[strlen(line
)-1] = 0;
380 if (def
&& sym_set_string_value(sym
, def
))
385 static int conf_sym(struct menu
*menu
)
387 struct symbol
*sym
= menu
->sym
;
388 tristate oldval
, newval
;
391 printf("%*s%s ", indent
- 1, "", menu
->prompt
->text
);
393 printf("(%s) ", sym
->name
);
395 oldval
= sym_get_tristate_value(sym
);
407 if (oldval
!= no
&& sym_tristate_within_range(sym
, no
))
409 if (oldval
!= mod
&& sym_tristate_within_range(sym
, mod
))
411 if (oldval
!= yes
&& sym_tristate_within_range(sym
, yes
))
414 if (!conf_askvalue(sym
, sym_get_string_value(sym
)))
422 if (!line
[1] || !strcmp(&line
[1], "o"))
434 if (!line
[1] || !strcmp(&line
[1], "es"))
445 if (sym_set_tristate_value(sym
, newval
))
452 static int conf_choice(struct menu
*menu
)
454 struct symbol
*sym
, *def_sym
;
459 is_new
= !sym_has_value(sym
);
460 if (sym_is_changeable(sym
)) {
463 switch (sym_get_tristate_value(sym
)) {
472 switch (sym_get_tristate_value(sym
)) {
476 printf("%*s%s\n", indent
- 1, "", menu_get_prompt(menu
));
486 printf("%*s%s\n", indent
- 1, "", menu_get_prompt(menu
));
487 def_sym
= sym_get_choice_value(sym
);
490 for (child
= menu
->list
; child
; child
= child
->next
) {
491 if (!menu_is_visible(child
))
494 printf("%*c %s\n", indent
, '*', menu_get_prompt(child
));
498 if (child
->sym
== def_sym
) {
500 printf("%*c", indent
, '>');
502 printf("%*c", indent
, ' ');
503 printf(" %d. %s", cnt
, menu_get_prompt(child
));
504 if (child
->sym
->name
)
505 printf(" (%s)", child
->sym
->name
);
506 if (!sym_has_value(child
->sym
))
510 printf("%*schoice", indent
- 1, "");
515 printf("[1-%d?]: ", cnt
);
516 switch (input_mode
) {
527 xfgets(line
, sizeof(line
), stdin
);
529 if (line
[0] == '?') {
535 else if (isdigit(line
[0]))
545 for (child
= menu
->list
; child
; child
= child
->next
) {
546 if (!child
->sym
|| !menu_is_visible(child
))
553 if (line
[0] && line
[strlen(line
) - 1] == '?') {
557 sym_set_choice_value(sym
, child
->sym
);
558 for (child
= child
->list
; child
; child
= child
->next
) {
567 static void conf(struct menu
*menu
)
570 struct property
*prop
;
573 if (!menu_is_visible(menu
))
581 switch (prop
->type
) {
584 * Except in oldaskconfig mode, we show only menus that
585 * contain new symbols.
587 if (input_mode
!= oldaskconfig
&& rootEntry
!= menu
) {
593 prompt
= menu_get_prompt(menu
);
595 printf("%*c\n%*c %s\n%*c\n",
607 if (sym_is_choice(sym
)) {
609 if (sym
->curr
.tri
!= mod
)
628 for (child
= menu
->list
; child
; child
= child
->next
)
634 static void check_conf(struct menu
*menu
)
639 if (!menu_is_visible(menu
))
643 if (sym
&& !sym_has_value(sym
) &&
644 (sym_is_changeable(sym
) ||
645 (sym_is_choice(sym
) && sym_get_tristate_value(sym
) == yes
))) {
647 switch (input_mode
) {
652 if (sym
->type
== S_STRING
) {
653 str
= sym_get_string_value(sym
);
654 str
= sym_escape_string_value(str
);
655 printf("%s%s=%s\n", CONFIG_
, sym
->name
, str
);
658 str
= sym_get_string_value(sym
);
659 printf("%s%s=%s\n", CONFIG_
, sym
->name
, str
);
670 printf("*\n* Restart config...\n*\n");
671 rootEntry
= menu_get_parent_menu(menu
);
677 for (child
= menu
->list
; child
; child
= child
->next
)
681 static const struct option long_opts
[] = {
682 {"help", no_argument
, NULL
, 'h'},
683 {"silent", no_argument
, NULL
, 's'},
684 {"oldaskconfig", no_argument
, &input_mode_opt
, oldaskconfig
},
685 {"oldconfig", no_argument
, &input_mode_opt
, oldconfig
},
686 {"syncconfig", no_argument
, &input_mode_opt
, syncconfig
},
687 {"defconfig", required_argument
, &input_mode_opt
, defconfig
},
688 {"savedefconfig", required_argument
, &input_mode_opt
, savedefconfig
},
689 {"allnoconfig", no_argument
, &input_mode_opt
, allnoconfig
},
690 {"allyesconfig", no_argument
, &input_mode_opt
, allyesconfig
},
691 {"allmodconfig", no_argument
, &input_mode_opt
, allmodconfig
},
692 {"alldefconfig", no_argument
, &input_mode_opt
, alldefconfig
},
693 {"randconfig", no_argument
, &input_mode_opt
, randconfig
},
694 {"listnewconfig", no_argument
, &input_mode_opt
, listnewconfig
},
695 {"helpnewconfig", no_argument
, &input_mode_opt
, helpnewconfig
},
696 {"olddefconfig", no_argument
, &input_mode_opt
, olddefconfig
},
697 {"yes2modconfig", no_argument
, &input_mode_opt
, yes2modconfig
},
698 {"mod2yesconfig", no_argument
, &input_mode_opt
, mod2yesconfig
},
702 static void conf_usage(const char *progname
)
704 printf("Usage: %s [options] <kconfig-file>\n", progname
);
706 printf("Generic options:\n");
707 printf(" -h, --help Print this message and exit.\n");
708 printf(" -s, --silent Do not print log.\n");
710 printf("Mode options:\n");
711 printf(" --listnewconfig List new options\n");
712 printf(" --helpnewconfig List new options and help text\n");
713 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
714 printf(" --oldconfig Update a configuration using a provided .config as base\n");
715 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
716 " include/{generated/,config/}\n");
717 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
718 printf(" --defconfig <file> New config with default defined in <file>\n");
719 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
720 printf(" --allnoconfig New config where all options are answered with no\n");
721 printf(" --allyesconfig New config where all options are answered with yes\n");
722 printf(" --allmodconfig New config where all options are answered with mod\n");
723 printf(" --alldefconfig New config with all symbols set to default\n");
724 printf(" --randconfig New config with random answer to all options\n");
725 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
726 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
727 printf(" (If none of the above is given, --oldaskconfig is the default)\n");
730 int main(int ac
, char **av
)
732 const char *progname
= av
[0];
734 const char *name
, *defconfig_file
= NULL
/* gcc uninit */;
735 int no_conf_write
= 0;
737 tty_stdio
= isatty(0) && isatty(1);
739 while ((opt
= getopt_long(ac
, av
, "hs", long_opts
, NULL
)) != -1) {
742 conf_usage(progname
);
746 conf_set_message_callback(NULL
);
749 input_mode
= input_mode_opt
;
750 switch (input_mode
) {
753 * syncconfig is invoked during the build stage.
754 * Suppress distracting
755 * "configuration written to ..."
757 conf_set_message_callback(NULL
);
762 defconfig_file
= optarg
;
765 set_randconfig_seed();
775 fprintf(stderr
, "%s: Kconfig file missing\n", av
[0]);
776 conf_usage(progname
);
779 conf_parse(av
[optind
]);
782 switch (input_mode
) {
784 if (conf_read(defconfig_file
)) {
787 "*** Can't find default configuration \"%s\"!\n"
809 name
= getenv("KCONFIG_ALLCONFIG");
812 if ((strcmp(name
, "") != 0) && (strcmp(name
, "1") != 0)) {
813 if (conf_read_simple(name
, S_DEF_USER
)) {
815 "*** Can't read seed configuration \"%s\"!\n",
821 switch (input_mode
) {
822 case allnoconfig
: name
= "allno.config"; break;
823 case allyesconfig
: name
= "allyes.config"; break;
824 case allmodconfig
: name
= "allmod.config"; break;
825 case alldefconfig
: name
= "alldef.config"; break;
826 case randconfig
: name
= "allrandom.config"; break;
829 if (conf_read_simple(name
, S_DEF_USER
) &&
830 conf_read_simple("all.config", S_DEF_USER
)) {
832 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
842 name
= getenv("KCONFIG_NOSILENTUPDATE");
844 if (conf_get_changed()) {
846 "\n*** The configuration requires explicit update.\n\n");
853 switch (input_mode
) {
855 conf_set_all_new_symbols(def_no
);
858 conf_set_all_new_symbols(def_yes
);
861 conf_set_all_new_symbols(def_mod
);
864 conf_set_all_new_symbols(def_default
);
867 /* Really nothing to do in this loop */
868 while (conf_set_all_new_symbols(def_random
)) ;
871 conf_set_all_new_symbols(def_default
);
876 conf_rewrite_mod_or_yes(def_y2m
);
879 conf_rewrite_mod_or_yes(def_m2y
);
882 rootEntry
= &rootmenu
;
884 input_mode
= oldconfig
;
890 /* Update until a loop caused no more changes */
893 check_conf(&rootmenu
);
901 if (input_mode
== savedefconfig
) {
902 if (conf_write_defconfig(defconfig_file
)) {
903 fprintf(stderr
, "n*** Error while saving defconfig to: %s\n\n",
907 } else if (input_mode
!= listnewconfig
&& input_mode
!= helpnewconfig
) {
908 if (!no_conf_write
&& conf_write(NULL
)) {
909 fprintf(stderr
, "\n*** Error during writing of the configuration.\n\n");
914 * Create auto.conf if it does not exist.
915 * This prevents GNU Make 4.1 or older from emitting
916 * "include/config/auto.conf: No such file or directory"
917 * in the top-level Makefile
919 * syncconfig always creates or updates auto.conf because it is
920 * used during the build.
922 if (conf_write_autoconf(sync_kconfig
) && sync_kconfig
) {
924 "\n*** Error during sync of the configuration.\n\n");