projects/linux·blob master · refs

884 lines · 19.1 KiB

1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2002 Roman Zippel <[email protected]>
4 */
5
6#include <ctype.h>
7#include <limits.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <time.h>
12#include <unistd.h>
13#include <getopt.h>
14#include <sys/time.h>
15#include <errno.h>
16
17#include "internal.h"
18#include "lkc.h"
19
20static void conf(struct menu *menu);
21static void check_conf(struct menu *menu);
22
23enum input_mode {
24 oldaskconfig,
25 syncconfig,
26 oldconfig,
27 allnoconfig,
28 allyesconfig,
29 allmodconfig,
30 alldefconfig,
31 randconfig,
32 defconfig,
33 savedefconfig,
34 listnewconfig,
35 helpnewconfig,
36 olddefconfig,
37 yes2modconfig,
38 mod2yesconfig,
39 mod2noconfig,
40};
41static enum input_mode input_mode = oldaskconfig;
42static int input_mode_opt;
43static int indent = 1;
44static int tty_stdio;
45static int sync_kconfig;
46static int conf_cnt;
47static char line[PATH_MAX];
48static struct menu *rootEntry;
49
50static void print_help(struct menu *menu)
51{
52 struct gstr help = str_new();
53
54 menu_get_ext_help(menu, &help);
55
56 printf("\n%s\n", str_get(&help));
57 str_free(&help);
58}
59
60static void strip(char *str)
61{
62 char *p = str;
63 int l;
64
65 while ((isspace(*p)))
66 p++;
67 l = strlen(p);
68 if (p != str)
69 memmove(str, p, l + 1);
70 if (!l)
71 return;
72 p = str + l - 1;
73 while ((isspace(*p)))
74 *p-- = 0;
75}
76
77/* Helper function to facilitate fgets() by Jean Sacren. */
78static void xfgets(char *str, int size, FILE *in)
79{
80 if (!fgets(str, size, in))
81 fprintf(stderr, "\nError in reading or end of file.\n");
82
83 if (!tty_stdio)
84 printf("%s", str);
85}
86
87static void set_randconfig_seed(void)
88{
89 unsigned int seed;
90 char *env;
91 bool seed_set = false;
92
93 env = getenv("KCONFIG_SEED");
94 if (env && *env) {
95 char *endp;
96
97 seed = strtol(env, &endp, 0);
98 if (*endp == '\0')
99 seed_set = true;
100 }
101
102 if (!seed_set) {
103 struct timeval now;
104
105 /*
106 * Use microseconds derived seed, compensate for systems where it may
107 * be zero.
108 */
109 gettimeofday(&now, NULL);
110 seed = (now.tv_sec + 1) * (now.tv_usec + 1);
111 }
112
113 printf("KCONFIG_SEED=0x%X\n", seed);
114 srand(seed);
115}
116
117/**
118 * randomize_choice_values - randomize choice block
119 *
120 * @choice: menu entry for the choice
121 */
122static void randomize_choice_values(struct menu *choice)
123{
124 struct menu *menu;
125 int x;
126 int cnt = 0;
127
128 /*
129 * First, count the number of symbols to randomize. If sym_has_value()
130 * is true, it was specified by KCONFIG_ALLCONFIG. It needs to be
131 * respected.
132 */
133 menu_for_each_sub_entry(menu, choice) {
134 struct symbol *sym = menu->sym;
135
136 if (sym && !sym_has_value(sym))
137 cnt++;
138 }
139
140 while (cnt > 0) {
141 x = rand() % cnt;
142
143 menu_for_each_sub_entry(menu, choice) {
144 struct symbol *sym = menu->sym;
145
146 if (sym && !sym_has_value(sym))
147 x--;
148
149 if (x < 0) {
150 sym->def[S_DEF_USER].tri = yes;
151 sym->flags |= SYMBOL_DEF_USER;
152 /*
153 * Move the selected item to the _tail_ because
154 * this needs to have a lower priority than the
155 * user input from KCONFIG_ALLCONFIG.
156 */
157 list_move_tail(&sym->choice_link,
158 &choice->choice_members);
159
160 break;
161 }
162 }
163 cnt--;
164 }
165}
166
167enum conf_def_mode {
168 def_default,
169 def_yes,
170 def_mod,
171 def_no,
172 def_random
173};
174
175static void conf_set_all_new_symbols(enum conf_def_mode mode)
176{
177 struct menu *menu;
178 int cnt;
179 /*
180 * can't go as the default in switch-case below, otherwise gcc whines
181 * about -Wmaybe-uninitialized
182 */
183 int pby = 50; /* probability of bool = y */
184 int pty = 33; /* probability of tristate = y */
185 int ptm = 33; /* probability of tristate = m */
186
187 if (mode == def_random) {
188 int n, p[3];
189 char *env = getenv("KCONFIG_PROBABILITY");
190
191 n = 0;
192 while (env && *env) {
193 char *endp;
194 int tmp = strtol(env, &endp, 10);
195
196 if (tmp >= 0 && tmp <= 100) {
197 p[n++] = tmp;
198 } else {
199 errno = ERANGE;
200 perror("KCONFIG_PROBABILITY");
201 exit(1);
202 }
203 env = (*endp == ':') ? endp + 1 : endp;
204 if (n >= 3)
205 break;
206 }
207 switch (n) {
208 case 1:
209 pby = p[0];
210 ptm = pby / 2;
211 pty = pby - ptm;
212 break;
213 case 2:
214 pty = p[0];
215 ptm = p[1];
216 pby = pty + ptm;
217 break;
218 case 3:
219 pby = p[0];
220 pty = p[1];
221 ptm = p[2];
222 break;
223 }
224
225 if (pty + ptm > 100) {
226 errno = ERANGE;
227 perror("KCONFIG_PROBABILITY");
228 exit(1);
229 }
230 }
231
232 menu_for_each_entry(menu) {
233 struct symbol *sym = menu->sym;
234 tristate val;
235
236 if (!sym || !menu->prompt || sym_has_value(sym) ||
237 (sym->type != S_BOOLEAN && sym->type != S_TRISTATE) ||
238 sym_is_choice_value(sym))
239 continue;
240
241 if (sym_is_choice(sym)) {
242 if (mode == def_random)
243 randomize_choice_values(menu);
244 continue;
245 }
246
247 switch (mode) {
248 case def_yes:
249 val = yes;
250 break;
251 case def_mod:
252 val = mod;
253 break;
254 case def_no:
255 val = no;
256 break;
257 case def_random:
258 val = no;
259 cnt = rand() % 100;
260 if (sym->type == S_TRISTATE) {
261 if (cnt < pty)
262 val = yes;
263 else if (cnt < pty + ptm)
264 val = mod;
265 } else if (cnt < pby) {
266 val = yes;
267 }
268 break;
269 default:
270 continue;
271 }
272 sym->def[S_DEF_USER].tri = val;
273 sym->flags |= SYMBOL_DEF_USER;
274 }
275
276 sym_clear_all_valid();
277}
278
279static void conf_rewrite_tristates(tristate old_val, tristate new_val)
280{
281 struct symbol *sym;
282
283 for_all_symbols(sym) {
284 if (sym_get_type(sym) == S_TRISTATE &&
285 sym->def[S_DEF_USER].tri == old_val)
286 sym->def[S_DEF_USER].tri = new_val;
287 }
288 sym_clear_all_valid();
289}
290
291static int conf_askvalue(struct symbol *sym, const char *def)
292{
293 if (!sym_has_value(sym))
294 printf("(NEW) ");
295
296 line[0] = '\n';
297 line[1] = 0;
298
299 if (!sym_is_changeable(sym)) {
300 printf("%s\n", def ?: "");
301 return 0;
302 }
303
304 switch (input_mode) {
305 case oldconfig:
306 case syncconfig:
307 if (sym_has_value(sym)) {
308 printf("%s\n", def ?: "");
309 return 0;
310 }
311 /* fall through */
312 default:
313 fflush(stdout);
314 xfgets(line, sizeof(line), stdin);
315 break;
316 }
317
318 return 1;
319}
320
321static int conf_string(struct menu *menu)
322{
323 struct symbol *sym = menu->sym;
324 const char *def;
325
326 while (1) {
327 printf("%*s%s ", indent - 1, "", menu->prompt->text);
328 printf("(%s) ", sym->name);
329 def = sym_get_string_value(sym);
330 if (def)
331 printf("[%s] ", def);
332 if (!conf_askvalue(sym, def))
333 return 0;
334 switch (line[0]) {
335 case '\n':
336 break;
337 case '?':
338 /* print help */
339 if (line[1] == '\n') {
340 print_help(menu);
341 def = NULL;
342 break;
343 }
344 /* fall through */
345 default:
346 line[strlen(line)-1] = 0;
347 def = line;
348 }
349 if (def && sym_set_string_value(sym, def))
350 return 0;
351
352 /*
353 * A new int or hex symbol whose default fails validation
354 * cannot be set from an empty answer. When standard input is
355 * exhausted, as it is for a non-interactive oldconfig or
356 * syncconfig, re-asking would loop forever and grow the output
357 * until it exhausts memory. Stop with an error that names the
358 * symbol instead. String symbols accept any text, and bool and
359 * tristate symbols (conf_sym()) and choices (conf_choice())
360 * accept the default on an empty line, so they are unaffected.
361 */
362 if (feof(stdin)) {
363 fprintf(stderr,
364 "\nerror: no value for new symbol '%s' at end of input\n",
365 sym->name);
366 exit(1);
367 }
368 }
369}
370
371static int conf_sym(struct menu *menu)
372{
373 struct symbol *sym = menu->sym;
374 tristate oldval, newval;
375
376 while (1) {
377 printf("%*s%s ", indent - 1, "", menu->prompt->text);
378 if (sym->name)
379 printf("(%s) ", sym->name);
380 putchar('[');
381 oldval = sym_get_tristate_value(sym);
382 switch (oldval) {
383 case no:
384 putchar('N');
385 break;
386 case mod:
387 putchar('M');
388 break;
389 case yes:
390 putchar('Y');
391 break;
392 }
393 if (oldval != no && sym_tristate_within_range(sym, no))
394 printf("/n");
395 if (oldval != mod && sym_tristate_within_range(sym, mod))
396 printf("/m");
397 if (oldval != yes && sym_tristate_within_range(sym, yes))
398 printf("/y");
399 printf("/?] ");
400 if (!conf_askvalue(sym, sym_get_string_value(sym)))
401 return 0;
402 strip(line);
403
404 switch (line[0]) {
405 case 'n':
406 case 'N':
407 newval = no;
408 if (!line[1] || !strcmp(&line[1], "o"))
409 break;
410 continue;
411 case 'm':
412 case 'M':
413 newval = mod;
414 if (!line[1])
415 break;
416 continue;
417 case 'y':
418 case 'Y':
419 newval = yes;
420 if (!line[1] || !strcmp(&line[1], "es"))
421 break;
422 continue;
423 case 0:
424 newval = oldval;
425 break;
426 case '?':
427 goto help;
428 default:
429 continue;
430 }
431 if (sym_set_tristate_value(sym, newval))
432 return 0;
433help:
434 print_help(menu);
435 }
436}
437
438static void conf_choice(struct menu *menu)
439{
440 struct symbol *def_sym;
441 struct menu *child;
442 bool is_new = false;
443
444 while (1) {
445 int cnt, def;
446
447 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu));
448 def_sym = sym_calc_choice(menu);
449 cnt = def = 0;
450 line[0] = 0;
451 for (child = menu->list; child; child = child->next) {
452 if (!menu_is_visible(child))
453 continue;
454 if (!child->sym) {
455 printf("%*c %s\n", indent, '*', menu_get_prompt(child));
456 continue;
457 }
458 cnt++;
459 if (child->sym == def_sym) {
460 def = cnt;
461 printf("%*c", indent, '>');
462 } else
463 printf("%*c", indent, ' ');
464 printf(" %d. %s (%s)", cnt, menu_get_prompt(child),
465 child->sym->name);
466 if (!sym_has_value(child->sym)) {
467 is_new = true;
468 printf(" (NEW)");
469 }
470 printf("\n");
471 }
472 printf("%*schoice", indent - 1, "");
473 if (cnt == 1) {
474 printf("[1]: 1\n");
475 goto conf_childs;
476 }
477 printf("[1-%d?]: ", cnt);
478 switch (input_mode) {
479 case oldconfig:
480 case syncconfig:
481 if (!is_new) {
482 cnt = def;
483 printf("%d\n", cnt);
484 break;
485 }
486 /* fall through */
487 case oldaskconfig:
488 fflush(stdout);
489 xfgets(line, sizeof(line), stdin);
490 strip(line);
491 if (line[0] == '?') {
492 print_help(menu);
493 continue;
494 }
495 if (!line[0])
496 cnt = def;
497 else if (isdigit(line[0]))
498 cnt = atoi(line);
499 else
500 continue;
501 break;
502 default:
503 break;
504 }
505
506 conf_childs:
507 for (child = menu->list; child; child = child->next) {
508 if (!child->sym || !menu_is_visible(child))
509 continue;
510 if (!--cnt)
511 break;
512 }
513 if (!child)
514 continue;
515 if (line[0] && line[strlen(line) - 1] == '?') {
516 print_help(child);
517 continue;
518 }
519 choice_set_value(menu, child->sym);
520 return;
521 }
522}
523
524static void conf(struct menu *menu)
525{
526 struct symbol *sym;
527 struct property *prop;
528 struct menu *child;
529
530 if (!menu_is_visible(menu))
531 return;
532
533 sym = menu->sym;
534 prop = menu->prompt;
535 if (prop) {
536 const char *prompt;
537
538 switch (prop->type) {
539 case P_MENU:
540 /*
541 * Except in oldaskconfig mode, we show only menus that
542 * contain new symbols.
543 */
544 if (input_mode != oldaskconfig && rootEntry != menu) {
545 check_conf(menu);
546 return;
547 }
548 /* fall through */
549 case P_COMMENT:
550 prompt = menu_get_prompt(menu);
551 if (prompt)
552 printf("%*c\n%*c %s\n%*c\n",
553 indent, '*',
554 indent, '*', prompt,
555 indent, '*');
556 default:
557 ;
558 }
559 }
560
561 if (!sym)
562 goto conf_childs;
563
564 if (sym_is_choice(sym)) {
565 conf_choice(menu);
566 return;
567 }
568
569 switch (sym->type) {
570 case S_INT:
571 case S_HEX:
572 case S_STRING:
573 conf_string(menu);
574 break;
575 default:
576 conf_sym(menu);
577 break;
578 }
579
580conf_childs:
581 if (sym)
582 indent += 2;
583 for (child = menu->list; child; child = child->next)
584 conf(child);
585 if (sym)
586 indent -= 2;
587}
588
589static void check_conf(struct menu *menu)
590{
591 struct symbol *sym;
592 struct menu *child;
593
594 if (!menu_is_visible(menu))
595 return;
596
597 sym = menu->sym;
598 if (sym && !sym_has_value(sym) && sym_is_changeable(sym)) {
599 switch (input_mode) {
600 case listnewconfig:
601 if (sym->name)
602 print_symbol_for_listconfig(sym);
603 break;
604 case helpnewconfig:
605 printf("-----\n");
606 print_help(menu);
607 printf("-----\n");
608 break;
609 default:
610 if (!conf_cnt++)
611 printf("*\n* Restart config...\n*\n");
612 rootEntry = menu_get_menu_or_parent_menu(menu);
613 conf(rootEntry);
614 break;
615 }
616 }
617
618 for (child = menu->list; child; child = child->next)
619 check_conf(child);
620}
621
622static const struct option long_opts[] = {
623 {"help", no_argument, NULL, 'h'},
624 {"silent", no_argument, NULL, 's'},
625 {"oldaskconfig", no_argument, &input_mode_opt, oldaskconfig},
626 {"oldconfig", no_argument, &input_mode_opt, oldconfig},
627 {"syncconfig", no_argument, &input_mode_opt, syncconfig},
628 {"defconfig", required_argument, &input_mode_opt, defconfig},
629 {"savedefconfig", required_argument, &input_mode_opt, savedefconfig},
630 {"allnoconfig", no_argument, &input_mode_opt, allnoconfig},
631 {"allyesconfig", no_argument, &input_mode_opt, allyesconfig},
632 {"allmodconfig", no_argument, &input_mode_opt, allmodconfig},
633 {"alldefconfig", no_argument, &input_mode_opt, alldefconfig},
634 {"randconfig", no_argument, &input_mode_opt, randconfig},
635 {"listnewconfig", no_argument, &input_mode_opt, listnewconfig},
636 {"helpnewconfig", no_argument, &input_mode_opt, helpnewconfig},
637 {"olddefconfig", no_argument, &input_mode_opt, olddefconfig},
638 {"yes2modconfig", no_argument, &input_mode_opt, yes2modconfig},
639 {"mod2yesconfig", no_argument, &input_mode_opt, mod2yesconfig},
640 {"mod2noconfig", no_argument, &input_mode_opt, mod2noconfig},
641 {NULL, 0, NULL, 0}
642};
643
644static void conf_usage(const char *progname)
645{
646 printf("Usage: %s [options] kconfig_file\n", progname);
647 printf("\n");
648 printf("Generic options:\n");
649 printf(" -h, --help Print this message and exit.\n");
650 printf(" -s, --silent Do not print log.\n");
651 printf("\n");
652 printf("Mode options:\n");
653 printf(" --listnewconfig List new options\n");
654 printf(" --helpnewconfig List new options and help text\n");
655 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n");
656 printf(" --oldconfig Update a configuration using a provided .config as base\n");
657 printf(" --syncconfig Similar to oldconfig but generates configuration in\n"
658 " include/{generated/,config/}\n");
659 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n");
660 printf(" --defconfig <file> New config with default defined in <file>\n");
661 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n");
662 printf(" --allnoconfig New config where all options are answered with no\n");
663 printf(" --allyesconfig New config where all options are answered with yes\n");
664 printf(" --allmodconfig New config where all options are answered with mod\n");
665 printf(" --alldefconfig New config with all symbols set to default\n");
666 printf(" --randconfig New config with random answer to all options\n");
667 printf(" --yes2modconfig Change answers from yes to mod if possible\n");
668 printf(" --mod2yesconfig Change answers from mod to yes if possible\n");
669 printf(" --mod2noconfig Change answers from mod to no if possible\n");
670 printf(" (If none of the above is given, --oldaskconfig is the default)\n");
671 printf("\n");
672 printf("Arguments:\n");
673 printf(" kconfig_file Top-level Kconfig file.\n");
674}
675
676int main(int ac, char **av)
677{
678 const char *progname = av[0];
679 int opt;
680 const char *name, *defconfig_file = NULL /* gcc uninit */;
681 int no_conf_write = 0;
682
683 tty_stdio = isatty(0) && isatty(1);
684
685 while ((opt = getopt_long(ac, av, "hs", long_opts, NULL)) != -1) {
686 switch (opt) {
687 case 'h':
688 conf_usage(progname);
689 exit(1);
690 break;
691 case 's':
692 conf_set_message_callback(NULL);
693 break;
694 case 0:
695 input_mode = input_mode_opt;
696 switch (input_mode) {
697 case syncconfig:
698 /*
699 * syncconfig is invoked during the build stage.
700 * Suppress distracting
701 * "configuration written to ..."
702 */
703 conf_set_message_callback(NULL);
704 sync_kconfig = 1;
705 break;
706 case defconfig:
707 case savedefconfig:
708 defconfig_file = optarg;
709 break;
710 case randconfig:
711 set_randconfig_seed();
712 break;
713 default:
714 break;
715 }
716 default:
717 break;
718 }
719 }
720 if (ac == optind) {
721 fprintf(stderr, "%s: Kconfig file missing\n", av[0]);
722 conf_usage(progname);
723 exit(1);
724 }
725 conf_parse(av[optind]);
726 //zconfdump(stdout);
727
728 switch (input_mode) {
729 case defconfig:
730 if (conf_read(defconfig_file)) {
731 fprintf(stderr,
732 "***\n"
733 "*** Can't find default configuration \"%s\"!\n"
734 "***\n",
735 defconfig_file);
736 exit(1);
737 }
738 break;
739 case savedefconfig:
740 case syncconfig:
741 case oldaskconfig:
742 case oldconfig:
743 case listnewconfig:
744 case helpnewconfig:
745 case olddefconfig:
746 case yes2modconfig:
747 case mod2yesconfig:
748 case mod2noconfig:
749 conf_read(NULL);
750 break;
751 case allnoconfig:
752 case allyesconfig:
753 case allmodconfig:
754 case alldefconfig:
755 case randconfig:
756 name = getenv("KCONFIG_ALLCONFIG");
757 if (!name)
758 break;
759 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) {
760 if (conf_read_simple(name, S_DEF_USER)) {
761 fprintf(stderr,
762 "*** Can't read seed configuration \"%s\"!\n",
763 name);
764 exit(1);
765 }
766 break;
767 }
768 switch (input_mode) {
769 case allnoconfig: name = "allno.config"; break;
770 case allyesconfig: name = "allyes.config"; break;
771 case allmodconfig: name = "allmod.config"; break;
772 case alldefconfig: name = "alldef.config"; break;
773 case randconfig: name = "allrandom.config"; break;
774 default: break;
775 }
776 if (conf_read_simple(name, S_DEF_USER) &&
777 conf_read_simple("all.config", S_DEF_USER)) {
778 fprintf(stderr,
779 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n",
780 name);
781 exit(1);
782 }
783 break;
784 default:
785 break;
786 }
787
788 if (conf_errors())
789 exit(1);
790
791 if (sync_kconfig) {
792 name = getenv("KCONFIG_NOSILENTUPDATE");
793 if (name && *name) {
794 if (conf_get_changed()) {
795 fprintf(stderr,
796 "\n*** The configuration requires explicit update.\n\n");
797 return 1;
798 }
799 no_conf_write = 1;
800 }
801 }
802
803 switch (input_mode) {
804 case allnoconfig:
805 conf_set_all_new_symbols(def_no);
806 break;
807 case allyesconfig:
808 conf_set_all_new_symbols(def_yes);
809 break;
810 case allmodconfig:
811 conf_set_all_new_symbols(def_mod);
812 break;
813 case alldefconfig:
814 conf_set_all_new_symbols(def_default);
815 break;
816 case randconfig:
817 conf_set_all_new_symbols(def_random);
818 break;
819 case defconfig:
820 conf_set_all_new_symbols(def_default);
821 break;
822 case savedefconfig:
823 break;
824 case yes2modconfig:
825 conf_rewrite_tristates(yes, mod);
826 break;
827 case mod2yesconfig:
828 conf_rewrite_tristates(mod, yes);
829 break;
830 case mod2noconfig:
831 conf_rewrite_tristates(mod, no);
832 break;
833 case oldaskconfig:
834 rootEntry = &rootmenu;
835 conf(&rootmenu);
836 input_mode = oldconfig;
837 /* fall through */
838 case oldconfig:
839 case listnewconfig:
840 case helpnewconfig:
841 case syncconfig:
842 /* Update until a loop caused no more changes */
843 do {
844 conf_cnt = 0;
845 check_conf(&rootmenu);
846 } while (conf_cnt);
847 break;
848 case olddefconfig:
849 default:
850 break;
851 }
852
853 if (sym_dep_errors())
854 exit(1);
855
856 if (input_mode == savedefconfig) {
857 if (conf_write_defconfig(defconfig_file)) {
858 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n",
859 defconfig_file);
860 return 1;
861 }
862 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) {
863 if (!no_conf_write && conf_write(NULL)) {
864 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n");
865 exit(1);
866 }
867
868 /*
869 * Create auto.conf if it does not exist.
870 * This prevents GNU Make 4.1 or older from emitting
871 * "include/config/auto.conf: No such file or directory"
872 * in the top-level Makefile
873 *
874 * syncconfig always creates or updates auto.conf because it is
875 * used during the build.
876 */
877 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) {
878 fprintf(stderr,
879 "\n*** Error during sync of the configuration.\n\n");
880 return 1;
881 }
882 }
883 return 0;
884}