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
;
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 sym_clear_all_valid();
233 for_all_symbols(i
, sym
) {
234 if (sym_has_value(sym
) || sym
->flags
& SYMBOL_VALID
)
236 switch (sym_get_type(sym
)) {
242 sym
->def
[S_DEF_USER
].tri
= yes
;
245 sym
->def
[S_DEF_USER
].tri
= mod
;
248 sym
->def
[S_DEF_USER
].tri
= no
;
251 sym
->def
[S_DEF_USER
].tri
= no
;
253 if (sym
->type
== S_TRISTATE
) {
255 sym
->def
[S_DEF_USER
].tri
= yes
;
256 else if (cnt
< pty
+ ptm
)
257 sym
->def
[S_DEF_USER
].tri
= mod
;
258 } else if (cnt
< pby
)
259 sym
->def
[S_DEF_USER
].tri
= yes
;
264 if (!(sym_is_choice(sym
) && mode
== def_random
))
265 sym
->flags
|= SYMBOL_DEF_USER
;
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_mod_or_yes(enum conf_def_mode mode
)
310 tristate old_val
= (mode
== def_y2m
) ? yes
: mod
;
311 tristate new_val
= (mode
== def_y2m
) ? mod
: yes
;
313 for_all_symbols(i
, sym
) {
314 if (sym_get_type(sym
) == S_TRISTATE
&&
315 sym
->def
[S_DEF_USER
].tri
== old_val
)
316 sym
->def
[S_DEF_USER
].tri
= new_val
;
318 sym_clear_all_valid();
321 static int conf_askvalue(struct symbol
*sym
, const char *def
)
323 if (!sym_has_value(sym
))
329 if (!sym_is_changeable(sym
)) {
336 switch (input_mode
) {
339 if (sym_has_value(sym
)) {
346 xfgets(line
, sizeof(line
), stdin
);
353 static int conf_string(struct menu
*menu
)
355 struct symbol
*sym
= menu
->sym
;
359 printf("%*s%s ", indent
- 1, "", menu
->prompt
->text
);
360 printf("(%s) ", sym
->name
);
361 def
= sym_get_string_value(sym
);
363 printf("[%s] ", def
);
364 if (!conf_askvalue(sym
, def
))
371 if (line
[1] == '\n') {
378 line
[strlen(line
)-1] = 0;
381 if (def
&& sym_set_string_value(sym
, def
))
386 static int conf_sym(struct menu
*menu
)
388 struct symbol
*sym
= menu
->sym
;
389 tristate oldval
, newval
;
392 printf("%*s%s ", indent
- 1, "", menu
->prompt
->text
);
394 printf("(%s) ", sym
->name
);
396 oldval
= sym_get_tristate_value(sym
);
408 if (oldval
!= no
&& sym_tristate_within_range(sym
, no
))
410 if (oldval
!= mod
&& sym_tristate_within_range(sym
, mod
))
412 if (oldval
!= yes
&& sym_tristate_within_range(sym
, yes
))
415 if (!conf_askvalue(sym
, sym_get_string_value(sym
)))
423 if (!line
[1] || !strcmp(&line
[1], "o"))
435 if (!line
[1] || !strcmp(&line
[1], "es"))
446 if (sym_set_tristate_value(sym
, newval
))
453 static int conf_choice(struct menu
*menu
)
455 struct symbol
*sym
, *def_sym
;
460 is_new
= !sym_has_value(sym
);
461 if (sym_is_changeable(sym
)) {
464 switch (sym_get_tristate_value(sym
)) {
473 switch (sym_get_tristate_value(sym
)) {
477 printf("%*s%s\n", indent
- 1, "", menu_get_prompt(menu
));
487 printf("%*s%s\n", indent
- 1, "", menu_get_prompt(menu
));
488 def_sym
= sym_get_choice_value(sym
);
491 for (child
= menu
->list
; child
; child
= child
->next
) {
492 if (!menu_is_visible(child
))
495 printf("%*c %s\n", indent
, '*', menu_get_prompt(child
));
499 if (child
->sym
== def_sym
) {
501 printf("%*c", indent
, '>');
503 printf("%*c", indent
, ' ');
504 printf(" %d. %s", cnt
, menu_get_prompt(child
));
505 if (child
->sym
->name
)
506 printf(" (%s)", child
->sym
->name
);
507 if (!sym_has_value(child
->sym
))
511 printf("%*schoice", indent
- 1, "");
516 printf("[1-%d?]: ", cnt
);
517 switch (input_mode
) {
528 xfgets(line
, sizeof(line
), stdin
);
530 if (line
[0] == '?') {
536 else if (isdigit(line
[0]))
546 for (child
= menu
->list
; child
; child
= child
->next
) {
547 if (!child
->sym
|| !menu_is_visible(child
))
554 if (line
[0] && line
[strlen(line
) - 1] == '?') {
558 sym_set_choice_value(sym
, child
->sym
);
559 for (child
= child
->list
; child
; child
= child
->next
) {
568 static void conf(struct menu
*menu
)
571 struct property
*prop
;
574 if (!menu_is_visible(menu
))
582 switch (prop
->type
) {
585 * Except in oldaskconfig mode, we show only menus that
586 * contain new symbols.
588 if (input_mode
!= oldaskconfig
&& rootEntry
!= menu
) {
594 prompt
= menu_get_prompt(menu
);
596 printf("%*c\n%*c %s\n%*c\n",
608 if (sym_is_choice(sym
)) {
610 if (sym
->curr
.tri
!= mod
)
629 for (child
= menu
->list
; child
; child
= child
->next
)
635 static void check_conf(struct menu
*menu
)
640 if (!menu_is_visible(menu
))
644 if (sym
&& !sym_has_value(sym
) &&
645 (sym_is_changeable(sym
) ||
646 (sym_is_choice(sym
) && sym_get_tristate_value(sym
) == yes
))) {
648 switch (input_mode
) {
653 if (sym
->type
== S_STRING
) {
654 str
= sym_get_string_value(sym
);
655 str
= sym_escape_string_value(str
);
656 printf("%s%s=%s\n", CONFIG_
, sym
->name
, str
);
659 str
= sym_get_string_value(sym
);
660 printf("%s%s=%s\n", CONFIG_
, sym
->name
, str
);
671 printf("*\n* Restart config...\n*\n");
672 rootEntry
= menu_get_parent_menu(menu
);
678 for (child
= menu
->list
; child
; child
= child
->next
)
682 static const struct option long_opts
[] = {
683 {"help", no_argument
, NULL
, 'h'},
684 {"silent", no_argument
, NULL
, 's'},
685 {"oldaskconfig", no_argument
, &input_mode_opt
, oldaskconfig
},
686 {"oldconfig", no_argument
, &input_mode_opt
, oldconfig
},
687 {"syncconfig", no_argument
, &input_mode_opt
, syncconfig
},
688 {"defconfig", required_argument
, &input_mode_opt
, defconfig
},
689 {"savedefconfig", required_argument
, &input_mode_opt
, savedefconfig
},
690 {"allnoconfig", no_argument
, &input_mode_opt
, allnoconfig
},
691 {"allyesconfig", no_argument
, &input_mode_opt
, allyesconfig
},
692 {"allmodconfig", no_argument
, &input_mode_opt
, allmodconfig
},
693 {"alldefconfig", no_argument
, &input_mode_opt
, alldefconfig
},
694 {"randconfig", no_argument
, &input_mode_opt
, randconfig
},
695 {"listnewconfig", no_argument
, &input_mode_opt
, listnewconfig
},
696 {"helpnewconfig", no_argument
, &input_mode_opt
, helpnewconfig
},
697 {"olddefconfig", no_argument
, &input_mode_opt
, olddefconfig
},
698 {"yes2modconfig", no_argument
, &input_mode_opt
, yes2modconfig
},
699 {"mod2yesconfig", no_argument
, &input_mode_opt
, mod2yesconfig
},
700 {"fatalrecursive",no_argument
, NULL
, fatalrecursive
},
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");
711 printf(" --fatalrecursive Treat recursive depenendencies as a fatal error\n");
713 printf("Mode options:\n");
714 printf(" --listnewconfig List new options\n");
715 printf(" --helpnewconfig List new options and help text\n");
716 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
717 printf(" --oldconfig Update a configuration using a provided .config as base\n");
718 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
719 " include/{generated/,config/}\n");
720 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
721 printf(" --defconfig <file> New config with default defined in <file>\n");
722 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
723 printf(" --allnoconfig New config where all options are answered with no\n");
724 printf(" --allyesconfig New config where all options are answered with yes\n");
725 printf(" --allmodconfig New config where all options are answered with mod\n");
726 printf(" --alldefconfig New config with all symbols set to default\n");
727 printf(" --randconfig New config with random answer to all options\n");
728 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
729 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
730 printf(" (If none of the above is given, --oldaskconfig is the default)\n");
733 int main(int ac
, char **av
)
735 const char *progname
= av
[0];
737 const char *name
, *defconfig_file
= NULL
/* gcc uninit */;
738 const char *input_file
= NULL
, *output_file
= NULL
;
739 int no_conf_write
= 0;
741 tty_stdio
= isatty(0) && isatty(1);
743 while ((opt
= getopt_long(ac
, av
, "hr:sw:", long_opts
, NULL
)) != -1) {
746 conf_usage(progname
);
750 conf_set_message_callback(NULL
);
753 recursive_is_error
= 1;
759 output_file
= optarg
;
762 input_mode
= input_mode_opt
;
763 switch (input_mode
) {
766 * syncconfig is invoked during the build stage.
767 * Suppress distracting
768 * "configuration written to ..."
770 conf_set_message_callback(NULL
);
775 defconfig_file
= optarg
;
778 set_randconfig_seed();
788 fprintf(stderr
, "%s: Kconfig file missing\n", av
[0]);
789 conf_usage(progname
);
792 conf_parse(av
[optind
]);
795 switch (input_mode
) {
797 if (conf_read(defconfig_file
)) {
800 "*** Can't find default configuration \"%s\"!\n"
820 conf_read(input_file
);
827 name
= getenv("KCONFIG_NOSILENTUPDATE");
829 if (conf_get_changed()) {
831 "\n*** The configuration requires explicit update.\n\n");
838 switch (input_mode
) {
840 conf_set_all_new_symbols(def_no
);
843 conf_set_all_new_symbols(def_yes
);
846 conf_set_all_new_symbols(def_mod
);
849 conf_set_all_new_symbols(def_default
);
852 /* Really nothing to do in this loop */
853 while (conf_set_all_new_symbols(def_random
)) ;
856 conf_set_all_new_symbols(def_default
);
861 conf_rewrite_mod_or_yes(def_y2m
);
864 conf_rewrite_mod_or_yes(def_m2y
);
867 rootEntry
= &rootmenu
;
869 input_mode
= oldconfig
;
875 /* Update until a loop caused no more changes */
878 check_conf(&rootmenu
);
886 if (input_mode
== savedefconfig
) {
887 if (conf_write_defconfig(defconfig_file
)) {
888 fprintf(stderr
, "n*** Error while saving defconfig to: %s\n\n",
892 } else if (input_mode
!= listnewconfig
&& input_mode
!= helpnewconfig
) {
893 if ((output_file
|| !no_conf_write
) &&
894 conf_write(output_file
)) {
895 fprintf(stderr
, "\n*** Error during writing of the configuration.\n\n");
900 * Create auto.conf if it does not exist.
901 * This prevents GNU Make 4.1 or older from emitting
902 * "include/config/auto.conf: No such file or directory"
903 * in the top-level Makefile
905 * syncconfig always creates or updates auto.conf because it is
906 * used during the build.
908 if (conf_write_autoconf(sync_kconfig
) && sync_kconfig
) {
910 "\n*** Error during sync of the configuration.\n\n");