b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 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 "lkc.h" |
| 18 | |
| 19 | static void conf(struct menu *menu); |
| 20 | static void check_conf(struct menu *menu); |
| 21 | |
| 22 | enum input_mode { |
| 23 | oldaskconfig, |
| 24 | syncconfig, |
| 25 | oldconfig, |
| 26 | allnoconfig, |
| 27 | allyesconfig, |
| 28 | allmodconfig, |
| 29 | alldefconfig, |
| 30 | randconfig, |
| 31 | defconfig, |
| 32 | savedefconfig, |
| 33 | listnewconfig, |
| 34 | helpnewconfig, |
| 35 | olddefconfig, |
| 36 | yes2modconfig, |
| 37 | mod2yesconfig, |
| 38 | mod2noconfig, |
| 39 | fatalrecursive, |
| 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 | static bool randomize_choice_values(struct symbol *csym) |
| 118 | { |
| 119 | struct property *prop; |
| 120 | struct symbol *sym; |
| 121 | struct expr *e; |
| 122 | int cnt, def; |
| 123 | |
| 124 | /* |
| 125 | * If choice is mod then we may have more items selected |
| 126 | * and if no then no-one. |
| 127 | * In both cases stop. |
| 128 | */ |
| 129 | if (csym->curr.tri != yes) |
| 130 | return false; |
| 131 | |
| 132 | prop = sym_get_choice_prop(csym); |
| 133 | |
| 134 | /* count entries in choice block */ |
| 135 | cnt = 0; |
| 136 | expr_list_for_each_sym(prop->expr, e, sym) |
| 137 | cnt++; |
| 138 | |
| 139 | /* |
| 140 | * find a random value and set it to yes, |
| 141 | * set the rest to no so we have only one set |
| 142 | */ |
| 143 | def = rand() % cnt; |
| 144 | |
| 145 | cnt = 0; |
| 146 | expr_list_for_each_sym(prop->expr, e, sym) { |
| 147 | if (def == cnt++) { |
| 148 | sym->def[S_DEF_USER].tri = yes; |
| 149 | csym->def[S_DEF_USER].val = sym; |
| 150 | } else { |
| 151 | sym->def[S_DEF_USER].tri = no; |
| 152 | } |
| 153 | sym->flags |= SYMBOL_DEF_USER; |
| 154 | /* clear VALID to get value calculated */ |
| 155 | sym->flags &= ~SYMBOL_VALID; |
| 156 | } |
| 157 | csym->flags |= SYMBOL_DEF_USER; |
| 158 | /* clear VALID to get value calculated */ |
| 159 | csym->flags &= ~SYMBOL_VALID; |
| 160 | |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | enum conf_def_mode { |
| 165 | def_default, |
| 166 | def_yes, |
| 167 | def_mod, |
| 168 | def_no, |
| 169 | def_random |
| 170 | }; |
| 171 | |
| 172 | static bool conf_set_all_new_symbols(enum conf_def_mode mode) |
| 173 | { |
| 174 | struct symbol *sym, *csym; |
| 175 | int i, cnt; |
| 176 | /* |
| 177 | * can't go as the default in switch-case below, otherwise gcc whines |
| 178 | * about -Wmaybe-uninitialized |
| 179 | */ |
| 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; |
| 184 | |
| 185 | if (mode == def_random) { |
| 186 | int n, p[3]; |
| 187 | char *env = getenv("KCONFIG_PROBABILITY"); |
| 188 | |
| 189 | n = 0; |
| 190 | while (env && *env) { |
| 191 | char *endp; |
| 192 | int tmp = strtol(env, &endp, 10); |
| 193 | |
| 194 | if (tmp >= 0 && tmp <= 100) { |
| 195 | p[n++] = tmp; |
| 196 | } else { |
| 197 | errno = ERANGE; |
| 198 | perror("KCONFIG_PROBABILITY"); |
| 199 | exit(1); |
| 200 | } |
| 201 | env = (*endp == ':') ? endp + 1 : endp; |
| 202 | if (n >= 3) |
| 203 | break; |
| 204 | } |
| 205 | switch (n) { |
| 206 | case 1: |
| 207 | pby = p[0]; |
| 208 | ptm = pby / 2; |
| 209 | pty = pby - ptm; |
| 210 | break; |
| 211 | case 2: |
| 212 | pty = p[0]; |
| 213 | ptm = p[1]; |
| 214 | pby = pty + ptm; |
| 215 | break; |
| 216 | case 3: |
| 217 | pby = p[0]; |
| 218 | pty = p[1]; |
| 219 | ptm = p[2]; |
| 220 | break; |
| 221 | } |
| 222 | |
| 223 | if (pty + ptm > 100) { |
| 224 | errno = ERANGE; |
| 225 | perror("KCONFIG_PROBABILITY"); |
| 226 | exit(1); |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | sym_clear_all_valid(); |
| 231 | |
| 232 | for_all_symbols(i, sym) { |
| 233 | if (sym_has_value(sym) || sym->flags & SYMBOL_VALID) |
| 234 | continue; |
| 235 | switch (sym_get_type(sym)) { |
| 236 | case S_BOOLEAN: |
| 237 | case S_TRISTATE: |
| 238 | has_changed = true; |
| 239 | switch (mode) { |
| 240 | case def_yes: |
| 241 | sym->def[S_DEF_USER].tri = yes; |
| 242 | break; |
| 243 | case def_mod: |
| 244 | sym->def[S_DEF_USER].tri = mod; |
| 245 | break; |
| 246 | case def_no: |
| 247 | sym->def[S_DEF_USER].tri = no; |
| 248 | break; |
| 249 | case def_random: |
| 250 | sym->def[S_DEF_USER].tri = no; |
| 251 | cnt = rand() % 100; |
| 252 | if (sym->type == S_TRISTATE) { |
| 253 | if (cnt < pty) |
| 254 | sym->def[S_DEF_USER].tri = yes; |
| 255 | else if (cnt < pty + ptm) |
| 256 | sym->def[S_DEF_USER].tri = mod; |
| 257 | } else if (cnt < pby) |
| 258 | sym->def[S_DEF_USER].tri = yes; |
| 259 | break; |
| 260 | default: |
| 261 | continue; |
| 262 | } |
| 263 | if (!(sym_is_choice(sym) && mode == def_random)) |
| 264 | sym->flags |= SYMBOL_DEF_USER; |
| 265 | break; |
| 266 | default: |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | } |
| 271 | |
| 272 | /* |
| 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. |
| 280 | */ |
| 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; |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | for_all_symbols(i, csym) { |
| 290 | if (sym_has_value(csym) || !sym_is_choice(csym)) |
| 291 | continue; |
| 292 | |
| 293 | sym_calc_value(csym); |
| 294 | if (mode == def_random) |
| 295 | has_changed |= randomize_choice_values(csym); |
| 296 | else { |
| 297 | set_all_choice_values(csym); |
| 298 | has_changed = true; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | return has_changed; |
| 303 | } |
| 304 | |
| 305 | static void conf_rewrite_tristates(tristate old_val, tristate new_val) |
| 306 | { |
| 307 | struct symbol *sym; |
| 308 | int i; |
| 309 | |
| 310 | for_all_symbols(i, sym) { |
| 311 | if (sym_get_type(sym) == S_TRISTATE && |
| 312 | sym->def[S_DEF_USER].tri == old_val) |
| 313 | sym->def[S_DEF_USER].tri = new_val; |
| 314 | } |
| 315 | sym_clear_all_valid(); |
| 316 | } |
| 317 | |
| 318 | static int conf_askvalue(struct symbol *sym, const char *def) |
| 319 | { |
| 320 | if (!sym_has_value(sym)) |
| 321 | printf("(NEW) "); |
| 322 | |
| 323 | line[0] = '\n'; |
| 324 | line[1] = 0; |
| 325 | |
| 326 | if (!sym_is_changeable(sym)) { |
| 327 | printf("%s\n", def); |
| 328 | line[0] = '\n'; |
| 329 | line[1] = 0; |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | switch (input_mode) { |
| 334 | case oldconfig: |
| 335 | case syncconfig: |
| 336 | if (sym_has_value(sym)) { |
| 337 | printf("%s\n", def); |
| 338 | return 0; |
| 339 | } |
| 340 | /* fall through */ |
| 341 | default: |
| 342 | fflush(stdout); |
| 343 | xfgets(line, sizeof(line), stdin); |
| 344 | break; |
| 345 | } |
| 346 | |
| 347 | return 1; |
| 348 | } |
| 349 | |
| 350 | static int conf_string(struct menu *menu) |
| 351 | { |
| 352 | struct symbol *sym = menu->sym; |
| 353 | const char *def; |
| 354 | |
| 355 | while (1) { |
| 356 | printf("%*s%s ", indent - 1, "", menu->prompt->text); |
| 357 | printf("(%s) ", sym->name); |
| 358 | def = sym_get_string_value(sym); |
| 359 | if (def) |
| 360 | printf("[%s] ", def); |
| 361 | if (!conf_askvalue(sym, def)) |
| 362 | return 0; |
| 363 | switch (line[0]) { |
| 364 | case '\n': |
| 365 | break; |
| 366 | case '?': |
| 367 | /* print help */ |
| 368 | if (line[1] == '\n') { |
| 369 | print_help(menu); |
| 370 | def = NULL; |
| 371 | break; |
| 372 | } |
| 373 | /* fall through */ |
| 374 | default: |
| 375 | line[strlen(line)-1] = 0; |
| 376 | def = line; |
| 377 | } |
| 378 | if (def && sym_set_string_value(sym, def)) |
| 379 | return 0; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | static int conf_sym(struct menu *menu) |
| 384 | { |
| 385 | struct symbol *sym = menu->sym; |
| 386 | tristate oldval, newval; |
| 387 | |
| 388 | while (1) { |
| 389 | printf("%*s%s ", indent - 1, "", menu->prompt->text); |
| 390 | if (sym->name) |
| 391 | printf("(%s) ", sym->name); |
| 392 | putchar('['); |
| 393 | oldval = sym_get_tristate_value(sym); |
| 394 | switch (oldval) { |
| 395 | case no: |
| 396 | putchar('N'); |
| 397 | break; |
| 398 | case mod: |
| 399 | putchar('M'); |
| 400 | break; |
| 401 | case yes: |
| 402 | putchar('Y'); |
| 403 | break; |
| 404 | } |
| 405 | if (oldval != no && sym_tristate_within_range(sym, no)) |
| 406 | printf("/n"); |
| 407 | if (oldval != mod && sym_tristate_within_range(sym, mod)) |
| 408 | printf("/m"); |
| 409 | if (oldval != yes && sym_tristate_within_range(sym, yes)) |
| 410 | printf("/y"); |
| 411 | printf("/?] "); |
| 412 | if (!conf_askvalue(sym, sym_get_string_value(sym))) |
| 413 | return 0; |
| 414 | strip(line); |
| 415 | |
| 416 | switch (line[0]) { |
| 417 | case 'n': |
| 418 | case 'N': |
| 419 | newval = no; |
| 420 | if (!line[1] || !strcmp(&line[1], "o")) |
| 421 | break; |
| 422 | continue; |
| 423 | case 'm': |
| 424 | case 'M': |
| 425 | newval = mod; |
| 426 | if (!line[1]) |
| 427 | break; |
| 428 | continue; |
| 429 | case 'y': |
| 430 | case 'Y': |
| 431 | newval = yes; |
| 432 | if (!line[1] || !strcmp(&line[1], "es")) |
| 433 | break; |
| 434 | continue; |
| 435 | case 0: |
| 436 | newval = oldval; |
| 437 | break; |
| 438 | case '?': |
| 439 | goto help; |
| 440 | default: |
| 441 | continue; |
| 442 | } |
| 443 | if (sym_set_tristate_value(sym, newval)) |
| 444 | return 0; |
| 445 | help: |
| 446 | print_help(menu); |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | static int conf_choice(struct menu *menu) |
| 451 | { |
| 452 | struct symbol *sym, *def_sym; |
| 453 | struct menu *child; |
| 454 | bool is_new; |
| 455 | |
| 456 | sym = menu->sym; |
| 457 | is_new = !sym_has_value(sym); |
| 458 | if (sym_is_changeable(sym)) { |
| 459 | conf_sym(menu); |
| 460 | sym_calc_value(sym); |
| 461 | switch (sym_get_tristate_value(sym)) { |
| 462 | case no: |
| 463 | return 1; |
| 464 | case mod: |
| 465 | return 0; |
| 466 | case yes: |
| 467 | break; |
| 468 | } |
| 469 | } else { |
| 470 | switch (sym_get_tristate_value(sym)) { |
| 471 | case no: |
| 472 | return 1; |
| 473 | case mod: |
| 474 | printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu)); |
| 475 | return 0; |
| 476 | case yes: |
| 477 | break; |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | while (1) { |
| 482 | int cnt, def; |
| 483 | |
| 484 | printf("%*s%s\n", indent - 1, "", menu_get_prompt(menu)); |
| 485 | def_sym = sym_get_choice_value(sym); |
| 486 | cnt = def = 0; |
| 487 | line[0] = 0; |
| 488 | for (child = menu->list; child; child = child->next) { |
| 489 | if (!menu_is_visible(child)) |
| 490 | continue; |
| 491 | if (!child->sym) { |
| 492 | printf("%*c %s\n", indent, '*', menu_get_prompt(child)); |
| 493 | continue; |
| 494 | } |
| 495 | cnt++; |
| 496 | if (child->sym == def_sym) { |
| 497 | def = cnt; |
| 498 | printf("%*c", indent, '>'); |
| 499 | } else |
| 500 | printf("%*c", indent, ' '); |
| 501 | printf(" %d. %s", cnt, menu_get_prompt(child)); |
| 502 | if (child->sym->name) |
| 503 | printf(" (%s)", child->sym->name); |
| 504 | if (!sym_has_value(child->sym)) |
| 505 | printf(" (NEW)"); |
| 506 | printf("\n"); |
| 507 | } |
| 508 | printf("%*schoice", indent - 1, ""); |
| 509 | if (cnt == 1) { |
| 510 | printf("[1]: 1\n"); |
| 511 | goto conf_childs; |
| 512 | } |
| 513 | printf("[1-%d?]: ", cnt); |
| 514 | switch (input_mode) { |
| 515 | case oldconfig: |
| 516 | case syncconfig: |
| 517 | if (!is_new) { |
| 518 | cnt = def; |
| 519 | printf("%d\n", cnt); |
| 520 | break; |
| 521 | } |
| 522 | /* fall through */ |
| 523 | case oldaskconfig: |
| 524 | fflush(stdout); |
| 525 | xfgets(line, sizeof(line), stdin); |
| 526 | strip(line); |
| 527 | if (line[0] == '?') { |
| 528 | print_help(menu); |
| 529 | continue; |
| 530 | } |
| 531 | if (!line[0]) |
| 532 | cnt = def; |
| 533 | else if (isdigit(line[0])) |
| 534 | cnt = atoi(line); |
| 535 | else |
| 536 | continue; |
| 537 | break; |
| 538 | default: |
| 539 | break; |
| 540 | } |
| 541 | |
| 542 | conf_childs: |
| 543 | for (child = menu->list; child; child = child->next) { |
| 544 | if (!child->sym || !menu_is_visible(child)) |
| 545 | continue; |
| 546 | if (!--cnt) |
| 547 | break; |
| 548 | } |
| 549 | if (!child) |
| 550 | continue; |
| 551 | if (line[0] && line[strlen(line) - 1] == '?') { |
| 552 | print_help(child); |
| 553 | continue; |
| 554 | } |
| 555 | sym_set_tristate_value(child->sym, yes); |
| 556 | for (child = child->list; child; child = child->next) { |
| 557 | indent += 2; |
| 558 | conf(child); |
| 559 | indent -= 2; |
| 560 | } |
| 561 | return 1; |
| 562 | } |
| 563 | } |
| 564 | |
| 565 | static void conf(struct menu *menu) |
| 566 | { |
| 567 | struct symbol *sym; |
| 568 | struct property *prop; |
| 569 | struct menu *child; |
| 570 | |
| 571 | if (!menu_is_visible(menu)) |
| 572 | return; |
| 573 | |
| 574 | sym = menu->sym; |
| 575 | prop = menu->prompt; |
| 576 | if (prop) { |
| 577 | const char *prompt; |
| 578 | |
| 579 | switch (prop->type) { |
| 580 | case P_MENU: |
| 581 | /* |
| 582 | * Except in oldaskconfig mode, we show only menus that |
| 583 | * contain new symbols. |
| 584 | */ |
| 585 | if (input_mode != oldaskconfig && rootEntry != menu) { |
| 586 | check_conf(menu); |
| 587 | return; |
| 588 | } |
| 589 | /* fall through */ |
| 590 | case P_COMMENT: |
| 591 | prompt = menu_get_prompt(menu); |
| 592 | if (prompt) |
| 593 | printf("%*c\n%*c %s\n%*c\n", |
| 594 | indent, '*', |
| 595 | indent, '*', prompt, |
| 596 | indent, '*'); |
| 597 | default: |
| 598 | ; |
| 599 | } |
| 600 | } |
| 601 | |
| 602 | if (!sym) |
| 603 | goto conf_childs; |
| 604 | |
| 605 | if (sym_is_choice(sym)) { |
| 606 | conf_choice(menu); |
| 607 | if (sym->curr.tri != mod) |
| 608 | return; |
| 609 | goto conf_childs; |
| 610 | } |
| 611 | |
| 612 | switch (sym->type) { |
| 613 | case S_INT: |
| 614 | case S_HEX: |
| 615 | case S_STRING: |
| 616 | conf_string(menu); |
| 617 | break; |
| 618 | default: |
| 619 | conf_sym(menu); |
| 620 | break; |
| 621 | } |
| 622 | |
| 623 | conf_childs: |
| 624 | if (sym) |
| 625 | indent += 2; |
| 626 | for (child = menu->list; child; child = child->next) |
| 627 | conf(child); |
| 628 | if (sym) |
| 629 | indent -= 2; |
| 630 | } |
| 631 | |
| 632 | static void check_conf(struct menu *menu) |
| 633 | { |
| 634 | struct symbol *sym; |
| 635 | struct menu *child; |
| 636 | |
| 637 | if (!menu_is_visible(menu)) |
| 638 | return; |
| 639 | |
| 640 | sym = menu->sym; |
| 641 | if (sym && !sym_has_value(sym) && |
| 642 | (sym_is_changeable(sym) || |
| 643 | (sym_is_choice(sym) && sym_get_tristate_value(sym) == yes))) { |
| 644 | |
| 645 | switch (input_mode) { |
| 646 | case listnewconfig: |
| 647 | if (sym->name) |
| 648 | print_symbol_for_listconfig(sym); |
| 649 | break; |
| 650 | case helpnewconfig: |
| 651 | printf("-----\n"); |
| 652 | print_help(menu); |
| 653 | printf("-----\n"); |
| 654 | break; |
| 655 | default: |
| 656 | if (!conf_cnt++) |
| 657 | printf("*\n* Restart config...\n*\n"); |
| 658 | rootEntry = menu_get_parent_menu(menu); |
| 659 | conf(rootEntry); |
| 660 | break; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | for (child = menu->list; child; child = child->next) |
| 665 | check_conf(child); |
| 666 | } |
| 667 | |
| 668 | static const struct option long_opts[] = { |
| 669 | {"help", no_argument, NULL, 'h'}, |
| 670 | {"silent", no_argument, NULL, 's'}, |
| 671 | {"oldaskconfig", no_argument, &input_mode_opt, oldaskconfig}, |
| 672 | {"oldconfig", no_argument, &input_mode_opt, oldconfig}, |
| 673 | {"syncconfig", no_argument, &input_mode_opt, syncconfig}, |
| 674 | {"defconfig", required_argument, &input_mode_opt, defconfig}, |
| 675 | {"savedefconfig", required_argument, &input_mode_opt, savedefconfig}, |
| 676 | {"allnoconfig", no_argument, &input_mode_opt, allnoconfig}, |
| 677 | {"allyesconfig", no_argument, &input_mode_opt, allyesconfig}, |
| 678 | {"allmodconfig", no_argument, &input_mode_opt, allmodconfig}, |
| 679 | {"alldefconfig", no_argument, &input_mode_opt, alldefconfig}, |
| 680 | {"randconfig", no_argument, &input_mode_opt, randconfig}, |
| 681 | {"listnewconfig", no_argument, &input_mode_opt, listnewconfig}, |
| 682 | {"helpnewconfig", no_argument, &input_mode_opt, helpnewconfig}, |
| 683 | {"olddefconfig", no_argument, &input_mode_opt, olddefconfig}, |
| 684 | {"yes2modconfig", no_argument, &input_mode_opt, yes2modconfig}, |
| 685 | {"mod2yesconfig", no_argument, &input_mode_opt, mod2yesconfig}, |
| 686 | {"mod2noconfig", no_argument, &input_mode_opt, mod2noconfig}, |
| 687 | {"fatalrecursive",no_argument, &input_mode_opt, fatalrecursive}, |
| 688 | {NULL, 0, NULL, 0} |
| 689 | }; |
| 690 | |
| 691 | static void conf_usage(const char *progname) |
| 692 | { |
| 693 | printf("Usage: %s [options] <kconfig-file>\n", progname); |
| 694 | printf("\n"); |
| 695 | printf("Generic options:\n"); |
| 696 | printf(" -h, --help Print this message and exit.\n"); |
| 697 | printf(" -r <file> Read <file> as input.\n"); |
| 698 | printf(" -s, --silent Do not print log.\n"); |
| 699 | printf(" -w <file> Write config to <file>.\n"); |
| 700 | printf(" --fatalrecursive Treat recursive dependency as error.\n"); |
| 701 | printf("\n"); |
| 702 | printf("Mode options:\n"); |
| 703 | printf(" --listnewconfig List new options\n"); |
| 704 | printf(" --helpnewconfig List new options and help text\n"); |
| 705 | printf(" --oldaskconfig Start a new configuration using a line-oriented program\n"); |
| 706 | printf(" --oldconfig Update a configuration using a provided .config as base\n"); |
| 707 | printf(" --syncconfig Similar to oldconfig but generates configuration in\n" |
| 708 | " include/{generated/,config/}\n"); |
| 709 | printf(" --olddefconfig Same as oldconfig but sets new symbols to their default value\n"); |
| 710 | printf(" --defconfig <file> New config with default defined in <file>\n"); |
| 711 | printf(" --savedefconfig <file> Save the minimal current configuration to <file>\n"); |
| 712 | printf(" --allnoconfig New config where all options are answered with no\n"); |
| 713 | printf(" --allyesconfig New config where all options are answered with yes\n"); |
| 714 | printf(" --allmodconfig New config where all options are answered with mod\n"); |
| 715 | printf(" --alldefconfig New config with all symbols set to default\n"); |
| 716 | printf(" --randconfig New config with random answer to all options\n"); |
| 717 | printf(" --yes2modconfig Change answers from yes to mod if possible\n"); |
| 718 | printf(" --mod2yesconfig Change answers from mod to yes if possible\n"); |
| 719 | printf(" --mod2noconfig Change answers from mod to no if possible\n"); |
| 720 | printf(" (If none of the above is given, --oldaskconfig is the default)\n"); |
| 721 | } |
| 722 | |
| 723 | int main(int ac, char **av) |
| 724 | { |
| 725 | const char *progname = av[0]; |
| 726 | int opt; |
| 727 | const char *name, *defconfig_file = NULL /* gcc uninit */; |
| 728 | const char *input_file = NULL, *output_file = NULL; |
| 729 | int no_conf_write = 0; |
| 730 | |
| 731 | tty_stdio = isatty(0) && isatty(1); |
| 732 | |
| 733 | while ((opt = getopt_long(ac, av, "hr:w:s", long_opts, NULL)) != -1) { |
| 734 | switch (opt) { |
| 735 | case 'h': |
| 736 | conf_usage(progname); |
| 737 | exit(1); |
| 738 | break; |
| 739 | case 'r': |
| 740 | input_file = optarg; |
| 741 | break; |
| 742 | case 's': |
| 743 | conf_set_message_callback(NULL); |
| 744 | break; |
| 745 | case 'w': |
| 746 | output_file = optarg; |
| 747 | break; |
| 748 | case 0: |
| 749 | switch (input_mode_opt) { |
| 750 | case syncconfig: |
| 751 | /* |
| 752 | * syncconfig is invoked during the build stage. |
| 753 | * Suppress distracting |
| 754 | * "configuration written to ..." |
| 755 | */ |
| 756 | conf_set_message_callback(NULL); |
| 757 | sync_kconfig = 1; |
| 758 | break; |
| 759 | case defconfig: |
| 760 | case savedefconfig: |
| 761 | defconfig_file = optarg; |
| 762 | break; |
| 763 | case randconfig: |
| 764 | set_randconfig_seed(); |
| 765 | break; |
| 766 | case fatalrecursive: |
| 767 | recursive_is_error = 1; |
| 768 | continue; |
| 769 | default: |
| 770 | break; |
| 771 | } |
| 772 | input_mode = input_mode_opt; |
| 773 | default: |
| 774 | break; |
| 775 | } |
| 776 | } |
| 777 | if (ac == optind) { |
| 778 | fprintf(stderr, "%s: Kconfig file missing\n", av[0]); |
| 779 | conf_usage(progname); |
| 780 | exit(1); |
| 781 | } |
| 782 | conf_parse(av[optind]); |
| 783 | //zconfdump(stdout); |
| 784 | |
| 785 | switch (input_mode) { |
| 786 | case defconfig: |
| 787 | if (conf_read(defconfig_file)) { |
| 788 | fprintf(stderr, |
| 789 | "***\n" |
| 790 | "*** Can't find default configuration \"%s\"!\n" |
| 791 | "***\n", |
| 792 | defconfig_file); |
| 793 | exit(1); |
| 794 | } |
| 795 | break; |
| 796 | case savedefconfig: |
| 797 | case syncconfig: |
| 798 | case oldaskconfig: |
| 799 | case oldconfig: |
| 800 | case listnewconfig: |
| 801 | case helpnewconfig: |
| 802 | case olddefconfig: |
| 803 | case yes2modconfig: |
| 804 | case mod2yesconfig: |
| 805 | case mod2noconfig: |
| 806 | case allnoconfig: |
| 807 | case allyesconfig: |
| 808 | case allmodconfig: |
| 809 | case alldefconfig: |
| 810 | case randconfig: |
| 811 | conf_read(input_file); |
| 812 | break; |
| 813 | default: |
| 814 | break; |
| 815 | } |
| 816 | |
| 817 | if (sync_kconfig) { |
| 818 | name = getenv("KCONFIG_NOSILENTUPDATE"); |
| 819 | if (name && *name) { |
| 820 | if (conf_get_changed()) { |
| 821 | fprintf(stderr, |
| 822 | "\n*** The configuration requires explicit update.\n\n"); |
| 823 | return 1; |
| 824 | } |
| 825 | no_conf_write = 1; |
| 826 | } |
| 827 | } |
| 828 | |
| 829 | switch (input_mode) { |
| 830 | case allnoconfig: |
| 831 | conf_set_all_new_symbols(def_no); |
| 832 | break; |
| 833 | case allyesconfig: |
| 834 | conf_set_all_new_symbols(def_yes); |
| 835 | break; |
| 836 | case allmodconfig: |
| 837 | conf_set_all_new_symbols(def_mod); |
| 838 | break; |
| 839 | case alldefconfig: |
| 840 | conf_set_all_new_symbols(def_default); |
| 841 | break; |
| 842 | case randconfig: |
| 843 | /* Really nothing to do in this loop */ |
| 844 | while (conf_set_all_new_symbols(def_random)) ; |
| 845 | break; |
| 846 | case defconfig: |
| 847 | conf_set_all_new_symbols(def_default); |
| 848 | break; |
| 849 | case savedefconfig: |
| 850 | break; |
| 851 | case yes2modconfig: |
| 852 | conf_rewrite_tristates(yes, mod); |
| 853 | break; |
| 854 | case mod2yesconfig: |
| 855 | conf_rewrite_tristates(mod, yes); |
| 856 | break; |
| 857 | case mod2noconfig: |
| 858 | conf_rewrite_tristates(mod, no); |
| 859 | break; |
| 860 | case oldaskconfig: |
| 861 | rootEntry = &rootmenu; |
| 862 | conf(&rootmenu); |
| 863 | input_mode = oldconfig; |
| 864 | /* fall through */ |
| 865 | case oldconfig: |
| 866 | case listnewconfig: |
| 867 | case helpnewconfig: |
| 868 | case syncconfig: |
| 869 | /* Update until a loop caused no more changes */ |
| 870 | do { |
| 871 | conf_cnt = 0; |
| 872 | check_conf(&rootmenu); |
| 873 | } while (conf_cnt); |
| 874 | break; |
| 875 | case olddefconfig: |
| 876 | default: |
| 877 | break; |
| 878 | } |
| 879 | |
| 880 | if (input_mode == savedefconfig) { |
| 881 | if (conf_write_defconfig(defconfig_file)) { |
| 882 | fprintf(stderr, "n*** Error while saving defconfig to: %s\n\n", |
| 883 | defconfig_file); |
| 884 | return 1; |
| 885 | } |
| 886 | } else if (input_mode != listnewconfig && input_mode != helpnewconfig) { |
| 887 | if ((output_file || !no_conf_write) && |
| 888 | conf_write(output_file)) { |
| 889 | fprintf(stderr, "\n*** Error during writing of the configuration.\n\n"); |
| 890 | exit(1); |
| 891 | } |
| 892 | |
| 893 | /* |
| 894 | * Create auto.conf if it does not exist. |
| 895 | * This prevents GNU Make 4.1 or older from emitting |
| 896 | * "include/config/auto.conf: No such file or directory" |
| 897 | * in the top-level Makefile |
| 898 | * |
| 899 | * syncconfig always creates or updates auto.conf because it is |
| 900 | * used during the build. |
| 901 | */ |
| 902 | if (conf_write_autoconf(sync_kconfig) && sync_kconfig) { |
| 903 | fprintf(stderr, |
| 904 | "\n*** Error during sync of the configuration.\n\n"); |
| 905 | return 1; |
| 906 | } |
| 907 | } |
| 908 | return 0; |
| 909 | } |