1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org> 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 20 static void conf(struct menu *menu); 21 static void check_conf(struct menu *menu); 22 23 enum 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 }; 41 static enum input_mode input_mode = oldaskconfig; 42 static int input_mode_opt; 43 static int indent = 1; 44 static int tty_stdio; 45 static int sync_kconfig; 46 static int conf_cnt; 47 static char line[PATH_MAX]; 48 static struct menu *rootEntry; 49 50 static 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 60 static 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. */ 78 static 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 87 static 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 */ 122 static 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 167 enum conf_def_mode { 168 def_default, 169 def_yes, 170 def_mod, 171 def_no, 172 def_random 173 }; 174 175 static 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 279 static 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 291 static 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 321 static 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 354 static int conf_sym(struct menu *menu) 355 { 356 struct symbol *sym = menu->sym; 357 tristate oldval, newval; 358 359 while (1) { 360 printf("%*s%s ", indent - 1, "", menu->prompt->text); 361 if (sym->name) 362 printf("(%s) ", sym->name); 363 putchar('['); 364 oldval = sym_get_tristate_value(sym); 365 switch (oldval) { 366 case no: 367 putchar('N'); 368 break; 369 case mod: 370 putchar('M'); 371 break; 372 case yes: 373 putchar('Y'); 374 break; 375 } 376 if (oldval != no && sym_tristate_within_range(sym, no)) 377 printf("/n"); 378 if (oldval != mod && sym_tristate_within_range(sym, mod)) 379 printf("/m"); 380 if (oldval != yes && sym_tristate_within_range(sym, yes)) 381 printf("/y"); 382 printf("/?] "); 383 if (!conf_askvalue(sym, sym_get_string_value(sym))) 384 return 0; 385 strip(line); 386 387 switch (line[0]) { 388 case 'n': 389 case 'N': 390 newval = no; 391 if (!line[1] || !strcmp(&line[1], "o")) 392 break; 393 continue; 394 case 'm': 395 case 'M': 396 newval = mod; 397 if (!line[1]) 398 break; 399 continue; 400 case 'y': 401 case 'Y': 402 newval = yes; 403 if (!line[1] || !strcmp(&line[1], "es")) 404 break; 405 continue; 406 case 0: 407 newval = oldval; 408 break; 409 case '?': 410 goto help; 411 default: 412 continue; 413 } 414 if (sym_set_tristate_value(sym, newval)) 415 return 0; 416 help: 417 print_help(menu); 418 } 419 } 420 421 static void conf_choice(struct menu *menu) 422 { 423 struct symbol *def_sym; 424 struct menu *child; 425 bool is_new = false; 426 427 while (1) { 428 int cnt, def; 429 430 printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu)); 431 def_sym = sym_calc_choice(menu); 432 cnt = def = 0; 433 line[0] = 0; 434 for (child = menu->list; child; child = child->next) { 435 if (!menu_is_visible(child)) 436 continue; 437 if (!child->sym) { 438 printf("%*c %s\n", indent, '*', menu_get_prompt(child)); 439 continue; 440 } 441 cnt++; 442 if (child->sym == def_sym) { 443 def = cnt; 444 printf("%*c", indent, '>'); 445 } else 446 printf("%*c", indent, ' '); 447 printf(" %d. %s (%s)", cnt, menu_get_prompt(child), 448 child->sym->name); 449 if (!sym_has_value(child->sym)) { 450 is_new = true; 451 printf(" (NEW)"); 452 } 453 printf("\n"); 454 } 455 printf("%*schoice", indent - 1, ""); 456 if (cnt == 1) { 457 printf("[1]: 1\n"); 458 goto conf_childs; 459 } 460 printf("[1-%d?]: ", cnt); 461 switch (input_mode) { 462 case oldconfig: 463 case syncconfig: 464 if (!is_new) { 465 cnt = def; 466 printf("%d\n", cnt); 467 break; 468 } 469 /* fall through */ 470 case oldaskconfig: 471 fflush(stdout); 472 xfgets(line, sizeof(line), stdin); 473 strip(line); 474 if (line[0] == '?') { 475 print_help(menu); 476 continue; 477 } 478 if (!line[0]) 479 cnt = def; 480 else if (isdigit(line[0])) 481 cnt = atoi(line); 482 else 483 continue; 484 break; 485 default: 486 break; 487 } 488 489 conf_childs: 490 for (child = menu->list; child; child = child->next) { 491 if (!child->sym || !menu_is_visible(child)) 492 continue; 493 if (!--cnt) 494 break; 495 } 496 if (!child) 497 continue; 498 if (line[0] && line[strlen(line) - 1] == '?') { 499 print_help(child); 500 continue; 501 } 502 choice_set_value(menu, child->sym); 503 return; 504 } 505 } 506 507 static void conf(struct menu *menu) 508 { 509 struct symbol *sym; 510 struct property *prop; 511 struct menu *child; 512 513 if (!menu_is_visible(menu)) 514 return; 515 516 sym = menu->sym; 517 prop = menu->prompt; 518 if (prop) { 519 const char *prompt; 520 521 switch (prop->type) { 522 case P_MENU: 523 /* 524 * Except in oldaskconfig mode, we show only menus that 525 * contain new symbols. 526 */ 527 if (input_mode != oldaskconfig && rootEntry != menu) { 528 check_conf(menu); 529 return; 530 } 531 /* fall through */ 532 case P_COMMENT: 533 prompt = menu_get_prompt(menu); 534 if (prompt) 535 printf("%*c\n%*c %s\n%*c\n", 536 indent, '*', 537 indent, '*', prompt, 538 indent, '*'); 539 default: 540 ; 541 } 542 } 543 544 if (!sym) 545 goto conf_childs; 546 547 if (sym_is_choice(sym)) { 548 conf_choice(menu); 549 return; 550 } 551 552 switch (sym->type) { 553 case S_INT: 554 case S_HEX: 555 case S_STRING: 556 conf_string(menu); 557 break; 558 default: 559 conf_sym(menu); 560 break; 561 } 562 563 conf_childs: 564 if (sym) 565 indent += 2; 566 for (child = menu->list; child; child = child->next) 567 conf(child); 568 if (sym) 569 indent -= 2; 570 } 571 572 static void check_conf(struct menu *menu) 573 { 574 struct symbol *sym; 575 struct menu *child; 576 577 if (!menu_is_visible(menu)) 578 return; 579 580 sym = menu->sym; 581 if (sym && !sym_has_value(sym) && sym_is_changeable(sym)) { 582 switch (input_mode) { 583 case listnewconfig: 584 if (sym->name) 585 print_symbol_for_listconfig(sym); 586 break; 587 case helpnewconfig: 588 printf("-----\n"); 589 print_help(menu); 590 printf("-----\n"); 591 break; 592 default: 593 if (!conf_cnt++) 594 printf("*\n* Restart config...\n*\n"); 595 rootEntry = menu_get_menu_or_parent_menu(menu); 596 conf(rootEntry); 597 break; 598 } 599 } 600 601 for (child = menu->list; child; child = child->next) 602 check_conf(child); 603 } 604 605 static const struct option long_opts[] = { 606 {"help", no_argument, NULL, 'h'}, 607 {"silent", no_argument, NULL, 's'}, 608 {"oldaskconfig", no_argument, &input_mode_opt, oldaskconfig}, 609 {"oldconfig", no_argument, &input_mode_opt, oldconfig}, 610 {"syncconfig", no_argument, &input_mode_opt, syncconfig}, 611 {"defconfig", required_argument, &input_mode_opt, defconfig}, 612 {"savedefconfig", required_argument, &input_mode_opt, savedefconfig}, 613 {"allnoconfig", no_argument, &input_mode_opt, allnoconfig}, 614 {"allyesconfig", no_argument, &input_mode_opt, allyesconfig}, 615 {"allmodconfig", no_argument, &input_mode_opt, allmodconfig}, 616 {"alldefconfig", no_argument, &input_mode_opt, alldefconfig}, 617 {"randconfig", no_argument, &input_mode_opt, randconfig}, 618 {"listnewconfig", no_argument, &input_mode_opt, listnewconfig}, 619 {"helpnewconfig", no_argument, &input_mode_opt, helpnewconfig}, 620 {"olddefconfig", no_argument, &input_mode_opt, olddefconfig}, 621 {"yes2modconfig", no_argument, &input_mode_opt, yes2modconfig}, 622 {"mod2yesconfig", no_argument, &input_mode_opt, mod2yesconfig}, 623 {"mod2noconfig", no_argument, &input_mode_opt, mod2noconfig}, 624 {NULL, 0, NULL, 0} 625 }; 626 627 static void conf_usage(const char *progname) 628 { 629 printf("Usage: %s [options] kconfig_file\n", progname); 630 printf("\n"); 631 printf("Generic options:\n"); 632 printf(" -h, --help Print this message and exit.\n"); 633 printf(" -s, --silent Do not print log.\n"); 634 printf("\n"); 635 printf("Mode options:\n"); 636 printf(" --listnewconfig List new options\n"); 637 printf(" --helpnewconfig List new options and help text\n"); 638 printf(" --oldaskconfig Start a new configuration using a line-oriented program\n"); 639 printf(" --oldconfig Update a configuration using a provided .config as base\n"); 640 printf(" --syncconfig Similar to oldconfig but generates configuration in\n" 641 " include/{generated/,config/}\n"); 642 printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n"); 643 printf(" --defconfig <file> New config with default defined in <file>\n"); 644 printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n"); 645 printf(" --allnoconfig New config where all options are answered with no\n"); 646 printf(" --allyesconfig New config where all options are answered with yes\n"); 647 printf(" --allmodconfig New config where all options are answered with mod\n"); 648 printf(" --alldefconfig New config with all symbols set to default\n"); 649 printf(" --randconfig New config with random answer to all options\n"); 650 printf(" --yes2modconfig Change answers from yes to mod if possible\n"); 651 printf(" --mod2yesconfig Change answers from mod to yes if possible\n"); 652 printf(" --mod2noconfig Change answers from mod to no if possible\n"); 653 printf(" (If none of the above is given, --oldaskconfig is the default)\n"); 654 printf("\n"); 655 printf("Arguments:\n"); 656 printf(" kconfig_file Top-level Kconfig file.\n"); 657 } 658 659 int main(int ac, char **av) 660 { 661 const char *progname = av[0]; 662 int opt; 663 const char *name, *defconfig_file = NULL /* gcc uninit */; 664 int no_conf_write = 0; 665 666 tty_stdio = isatty(0) && isatty(1); 667 668 while ((opt = getopt_long(ac, av, "hs", long_opts, NULL)) != -1) { 669 switch (opt) { 670 case 'h': 671 conf_usage(progname); 672 exit(1); 673 break; 674 case 's': 675 conf_set_message_callback(NULL); 676 break; 677 case 0: 678 input_mode = input_mode_opt; 679 switch (input_mode) { 680 case syncconfig: 681 /* 682 * syncconfig is invoked during the build stage. 683 * Suppress distracting 684 * "configuration written to ..." 685 */ 686 conf_set_message_callback(NULL); 687 sync_kconfig = 1; 688 break; 689 case defconfig: 690 case savedefconfig: 691 defconfig_file = optarg; 692 break; 693 case randconfig: 694 set_randconfig_seed(); 695 break; 696 default: 697 break; 698 } 699 default: 700 break; 701 } 702 } 703 if (ac == optind) { 704 fprintf(stderr, "%s: Kconfig file missing\n", av[0]); 705 conf_usage(progname); 706 exit(1); 707 } 708 conf_parse(av[optind]); 709 //zconfdump(stdout); 710 711 switch (input_mode) { 712 case defconfig: 713 if (conf_read(defconfig_file)) { 714 fprintf(stderr, 715 "***\n" 716 "*** Can't find default configuration \"%s\"!\n" 717 "***\n", 718 defconfig_file); 719 exit(1); 720 } 721 break; 722 case savedefconfig: 723 case syncconfig: 724 case oldaskconfig: 725 case oldconfig: 726 case listnewconfig: 727 case helpnewconfig: 728 case olddefconfig: 729 case yes2modconfig: 730 case mod2yesconfig: 731 case mod2noconfig: 732 conf_read(NULL); 733 break; 734 case allnoconfig: 735 case allyesconfig: 736 case allmodconfig: 737 case alldefconfig: 738 case randconfig: 739 name = getenv("KCONFIG_ALLCONFIG"); 740 if (!name) 741 break; 742 if ((strcmp(name, "") != 0) && (strcmp(name, "1") != 0)) { 743 if (conf_read_simple(name, S_DEF_USER)) { 744 fprintf(stderr, 745 "*** Can't read seed configuration \"%s\"!\n", 746 name); 747 exit(1); 748 } 749 break; 750 } 751 switch (input_mode) { 752 case allnoconfig: name = "allno.config"; break; 753 case allyesconfig: name = "allyes.config"; break; 754 case allmodconfig: name = "allmod.config"; break; 755 case alldefconfig: name = "alldef.config"; break; 756 case randconfig: name = "allrandom.config"; break; 757 default: break; 758 } 759 if (conf_read_simple(name, S_DEF_USER) && 760 conf_read_simple("all.config", S_DEF_USER)) { 761 fprintf(stderr, 762 "*** KCONFIG_ALLCONFIG set, but no \"%s\" or \"all.config\" file found\n", 763 name); 764 exit(1); 765 } 766 break; 767 default: 768 break; 769 } 770 771 if (conf_errors()) 772 exit(1); 773 774 if (sync_kconfig) { 775 name = getenv("KCONFIG_NOSILENTUPDATE"); 776 if (name && *name) { 777 if (conf_get_changed()) { 778 fprintf(stderr, 779 "\n*** The configuration requires explicit update.\n\n"); 780 return 1; 781 } 782 no_conf_write = 1; 783 } 784 } 785 786 switch (input_mode) { 787 case allnoconfig: 788 conf_set_all_new_symbols(def_no); 789 break; 790 case allyesconfig: 791 conf_set_all_new_symbols(def_yes); 792 break; 793 case allmodconfig: 794 conf_set_all_new_symbols(def_mod); 795 break; 796 case alldefconfig: 797 conf_set_all_new_symbols(def_default); 798 break; 799 case randconfig: 800 conf_set_all_new_symbols(def_random); 801 break; 802 case defconfig: 803 conf_set_all_new_symbols(def_default); 804 break; 805 case savedefconfig: 806 break; 807 case yes2modconfig: 808 conf_rewrite_tristates(yes, mod); 809 break; 810 case mod2yesconfig: 811 conf_rewrite_tristates(mod, yes); 812 break; 813 case mod2noconfig: 814 conf_rewrite_tristates(mod, no); 815 break; 816 case oldaskconfig: 817 rootEntry = &rootmenu; 818 conf(&rootmenu); 819 input_mode = oldconfig; 820 /* fall through */ 821 case oldconfig: 822 case listnewconfig: 823 case helpnewconfig: 824 case syncconfig: 825 /* Update until a loop caused no more changes */ 826 do { 827 conf_cnt = 0; 828 check_conf(&rootmenu); 829 } while (conf_cnt); 830 break; 831 case olddefconfig: 832 default: 833 break; 834 } 835 836 if (sym_dep_errors()) 837 exit(1); 838 839 if (input_mode == savedefconfig) { 840 if (conf_write_defconfig(defconfig_file)) { 841 fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n", 842 defconfig_file); 843 return 1; 844 } 845 } else if (input_mode != listnewconfig && input_mode != helpnewconfig) { 846 if (!no_conf_write && conf_write(NULL)) { 847 fprintf(stderr, "\n*** Error during writing of the configuration.\n\n"); 848 exit(1); 849 } 850 851 /* 852 * Create auto.conf if it does not exist. 853 * This prevents GNU Make 4.1 or older from emitting 854 * "include/config/auto.conf: No such file or directory" 855 * in the top-level Makefile 856 * 857 * syncconfig always creates or updates auto.conf because it is 858 * used during the build. 859 */ 860 if (conf_write_autoconf(sync_kconfig) && sync_kconfig) { 861 fprintf(stderr, 862 "\n*** Error during sync of the configuration.\n\n"); 863 return 1; 864 } 865 } 866 return 0; 867 } 868