1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org> 4 */ 5 6 #include <sys/types.h> 7 #include <ctype.h> 8 #include <errno.h> 9 #include <stdlib.h> 10 #include <string.h> 11 #include <regex.h> 12 13 #include <hash.h> 14 #include <xalloc.h> 15 #include "internal.h" 16 #include "lkc.h" 17 18 struct symbol symbol_yes = { 19 .name = "y", 20 .type = S_TRISTATE, 21 .curr = { "y", yes }, 22 .menus = LIST_HEAD_INIT(symbol_yes.menus), 23 .flags = SYMBOL_CONST|SYMBOL_VALID, 24 }; 25 26 struct symbol symbol_mod = { 27 .name = "m", 28 .type = S_TRISTATE, 29 .curr = { "m", mod }, 30 .menus = LIST_HEAD_INIT(symbol_mod.menus), 31 .flags = SYMBOL_CONST|SYMBOL_VALID, 32 }; 33 34 struct symbol symbol_no = { 35 .name = "n", 36 .type = S_TRISTATE, 37 .curr = { "n", no }, 38 .menus = LIST_HEAD_INIT(symbol_no.menus), 39 .flags = SYMBOL_CONST|SYMBOL_VALID, 40 }; 41 42 struct symbol *modules_sym; 43 static tristate modules_val; 44 static int sym_warnings; 45 46 enum symbol_type sym_get_type(const struct symbol *sym) 47 { 48 enum symbol_type type = sym->type; 49 50 if (type == S_TRISTATE && modules_val == no) 51 type = S_BOOLEAN; 52 return type; 53 } 54 55 const char *sym_type_name(enum symbol_type type) 56 { 57 switch (type) { 58 case S_BOOLEAN: 59 return "bool"; 60 case S_TRISTATE: 61 return "tristate"; 62 case S_INT: 63 return "integer"; 64 case S_HEX: 65 return "hex"; 66 case S_STRING: 67 return "string"; 68 case S_UNKNOWN: 69 return "unknown"; 70 } 71 return "???"; 72 } 73 74 /** 75 * sym_get_prompt_menu - get the menu entry with a prompt 76 * 77 * @sym: a symbol pointer 78 * 79 * Return: the menu entry with a prompt. 80 */ 81 struct menu *sym_get_prompt_menu(const struct symbol *sym) 82 { 83 struct menu *m; 84 85 list_for_each_entry(m, &sym->menus, link) 86 if (m->prompt) 87 return m; 88 89 return NULL; 90 } 91 92 /** 93 * sym_get_choice_menu - get the parent choice menu if present 94 * 95 * @sym: a symbol pointer 96 * 97 * Return: a choice menu if this function is called against a choice member. 98 */ 99 struct menu *sym_get_choice_menu(const struct symbol *sym) 100 { 101 struct menu *menu = NULL; 102 103 /* 104 * Choice members must have a prompt. Find a menu entry with a prompt, 105 * and assume it resides inside a choice block. 106 */ 107 menu = sym_get_prompt_menu(sym); 108 if (!menu) 109 return NULL; 110 111 do { 112 menu = menu->parent; 113 } while (menu && !menu->sym); 114 115 if (menu && menu->sym && sym_is_choice(menu->sym)) 116 return menu; 117 118 return NULL; 119 } 120 121 static struct property *sym_get_default_prop(struct symbol *sym) 122 { 123 struct property *prop; 124 125 for_all_defaults(sym, prop) { 126 prop->visible.tri = expr_calc_value(prop->visible.expr); 127 if (prop->visible.tri != no) 128 return prop; 129 } 130 return NULL; 131 } 132 133 struct property *sym_get_range_prop(struct symbol *sym) 134 { 135 struct property *prop; 136 137 for_all_properties(sym, prop, P_RANGE) { 138 prop->visible.tri = expr_calc_value(prop->visible.expr); 139 if (prop->visible.tri != no) 140 return prop; 141 } 142 return NULL; 143 } 144 145 union range_value { 146 long long s; 147 unsigned long long u; 148 }; 149 150 static union range_value sym_get_range_val(struct symbol *sym, int base) 151 { 152 union range_value val; 153 154 sym_calc_value(sym); 155 switch (sym->type) { 156 case S_INT: 157 base = 10; 158 break; 159 case S_HEX: 160 base = 16; 161 break; 162 default: 163 break; 164 } 165 if (base == 10) 166 val.s = strtoll(sym->curr.val, NULL, base); 167 else 168 /* HEX */ 169 val.u = strtoull(sym->curr.val, NULL, base); 170 return val; 171 } 172 173 /* 174 * Return the nearest range bound for an out-of-range default value. 175 * Return NULL if the value is valid or the symbol has no active range. 176 */ 177 static struct symbol *sym_get_near_range_bound(struct symbol *sym, 178 const char *value) 179 { 180 struct property *prop; 181 struct symbol *range_sym; 182 int base; 183 union range_value val, val2; 184 185 switch (sym->type) { 186 case S_INT: 187 base = 10; 188 break; 189 case S_HEX: 190 base = 16; 191 break; 192 default: 193 return NULL; 194 } 195 prop = sym_get_range_prop(sym); 196 if (!prop) 197 return NULL; 198 if (base == 10) 199 val.s = strtoll(value, NULL, base); 200 else 201 val.u = strtoull(value, NULL, base); 202 range_sym = prop->expr->left.sym; 203 val2 = sym_get_range_val(range_sym, base); 204 205 if (base == 10 && val.s >= val2.s) { 206 range_sym = prop->expr->right.sym; 207 val2 = sym_get_range_val(range_sym, base); 208 if (val.s <= val2.s) 209 return NULL; 210 } else if (base == 16 && val.u >= val2.u) { 211 range_sym = prop->expr->right.sym; 212 val2 = sym_get_range_val(range_sym, base); 213 if (val.u <= val2.u) 214 return NULL; 215 } 216 217 return range_sym; 218 } 219 220 static void sym_validate_range(struct symbol *sym) 221 { 222 struct symbol *range_sym; 223 224 range_sym = sym_get_near_range_bound(sym, sym->curr.val); 225 if (range_sym) 226 sym->curr.val = range_sym->curr.val; 227 } 228 229 static void sym_set_changed(struct symbol *sym) 230 { 231 struct menu *menu; 232 233 list_for_each_entry(menu, &sym->menus, link) 234 menu->flags |= MENU_CHANGED; 235 236 menu = sym_get_choice_menu(sym); 237 if (menu) 238 menu->flags |= MENU_CHANGED; 239 } 240 241 static void sym_set_all_changed(void) 242 { 243 struct symbol *sym; 244 245 for_all_symbols(sym) 246 sym_set_changed(sym); 247 } 248 249 static void sym_calc_visibility(struct symbol *sym) 250 { 251 struct property *prop; 252 tristate tri; 253 254 if (sym->flags & SYMBOL_TRANS) { 255 sym->visible = yes; 256 return; 257 } 258 259 /* any prompt visible? */ 260 tri = no; 261 for_all_prompts(sym, prop) { 262 prop->visible.tri = expr_calc_value(prop->visible.expr); 263 tri = EXPR_OR(tri, prop->visible.tri); 264 } 265 if (tri == mod && (sym->type != S_TRISTATE || modules_val == no)) 266 tri = yes; 267 if (sym->visible != tri) { 268 sym->visible = tri; 269 sym_set_changed(sym); 270 } 271 if (sym_is_choice_value(sym)) 272 return; 273 /* defaulting to "yes" if no explicit "depends on" are given */ 274 tri = yes; 275 if (sym->dir_dep.expr) 276 tri = expr_calc_value(sym->dir_dep.expr); 277 if (tri == mod && sym_get_type(sym) == S_BOOLEAN) 278 tri = yes; 279 if (sym->dir_dep.tri != tri) { 280 sym->dir_dep.tri = tri; 281 sym_set_changed(sym); 282 } 283 tri = no; 284 if (sym->rev_dep.expr) 285 tri = expr_calc_value(sym->rev_dep.expr); 286 if (tri == mod && sym_get_type(sym) == S_BOOLEAN) 287 tri = yes; 288 if (sym->rev_dep.tri != tri) { 289 sym->rev_dep.tri = tri; 290 sym_set_changed(sym); 291 } 292 tri = no; 293 if (sym->implied.expr) 294 tri = expr_calc_value(sym->implied.expr); 295 if (tri == mod && sym_get_type(sym) == S_BOOLEAN) 296 tri = yes; 297 if (sym->implied.tri != tri) { 298 sym->implied.tri = tri; 299 sym_set_changed(sym); 300 } 301 } 302 303 /* 304 * Find the default symbol for a choice. 305 * First try the default values for the choice symbol 306 * Next locate the first visible choice value 307 * Return NULL if none was found 308 */ 309 struct symbol *sym_choice_default(struct menu *choice) 310 { 311 struct menu *menu; 312 struct symbol *def_sym; 313 struct property *prop; 314 315 /* any of the defaults visible? */ 316 for_all_defaults(choice->sym, prop) { 317 prop->visible.tri = expr_calc_value(prop->visible.expr); 318 if (prop->visible.tri == no) 319 continue; 320 def_sym = prop_get_symbol(prop); 321 if (def_sym->visible != no) 322 return def_sym; 323 } 324 325 /* just get the first visible value */ 326 menu_for_each_sub_entry(menu, choice) 327 if (menu->sym && menu->sym->visible != no) 328 return menu->sym; 329 330 /* failed to locate any defaults */ 331 return NULL; 332 } 333 334 /* 335 * sym_calc_choice - calculate symbol values in a choice 336 * 337 * @choice: a menu of the choice 338 * 339 * Return: a chosen symbol 340 */ 341 struct symbol *sym_calc_choice(struct menu *choice) 342 { 343 struct symbol *res = NULL; 344 struct symbol *sym; 345 struct menu *menu; 346 347 /* Traverse the list of choice members in the priority order. */ 348 list_for_each_entry(sym, &choice->choice_members, choice_link) { 349 sym_calc_visibility(sym); 350 if (sym->visible == no) 351 continue; 352 353 /* The first visible symble with the user value 'y'. */ 354 if (sym_has_value(sym) && sym->def[S_DEF_USER].tri == yes) { 355 res = sym; 356 break; 357 } 358 } 359 360 /* 361 * If 'y' is not found in the user input, use the default, unless it is 362 * explicitly set to 'n'. 363 */ 364 if (!res) { 365 res = sym_choice_default(choice); 366 if (res && sym_has_value(res) && res->def[S_DEF_USER].tri == no) 367 res = NULL; 368 } 369 370 /* Still not found. Pick up the first visible, user-unspecified symbol. */ 371 if (!res) { 372 menu_for_each_sub_entry(menu, choice) { 373 sym = menu->sym; 374 375 if (!sym || sym->visible == no || sym_has_value(sym)) 376 continue; 377 378 res = sym; 379 break; 380 } 381 } 382 383 /* 384 * Still not found. Traverse the linked list in the _reverse_ order to 385 * pick up the least prioritized 'n'. 386 */ 387 if (!res) { 388 list_for_each_entry_reverse(sym, &choice->choice_members, 389 choice_link) { 390 if (sym->visible == no) 391 continue; 392 393 res = sym; 394 break; 395 } 396 } 397 398 menu_for_each_sub_entry(menu, choice) { 399 tristate val; 400 401 sym = menu->sym; 402 403 if (!sym || sym->visible == no) 404 continue; 405 406 val = sym == res ? yes : no; 407 408 if (sym->curr.tri != val) 409 sym_set_changed(sym); 410 411 sym->curr.tri = val; 412 sym->flags |= SYMBOL_VALID | SYMBOL_WRITE; 413 } 414 415 return res; 416 } 417 418 static void sym_warn_unmet_dep(const struct symbol *sym) 419 { 420 struct gstr gs = str_new(); 421 422 str_printf(&gs, 423 "\nWARNING: unmet direct dependencies detected for %s\n", 424 sym->name); 425 str_printf(&gs, 426 " Depends on [%c]: ", 427 sym->dir_dep.tri == mod ? 'm' : 'n'); 428 expr_gstr_print(sym->dir_dep.expr, &gs); 429 str_printf(&gs, "\n"); 430 431 expr_gstr_print_revdep(sym->rev_dep.expr, &gs, yes, 432 " Selected by [y]:\n"); 433 expr_gstr_print_revdep(sym->rev_dep.expr, &gs, mod, 434 " Selected by [m]:\n"); 435 436 fputs(str_get(&gs), stderr); 437 str_free(&gs); 438 sym_warnings++; 439 } 440 441 bool sym_dep_errors(void) 442 { 443 if (sym_warnings) 444 return getenv("KCONFIG_WERROR"); 445 return false; 446 } 447 448 void sym_calc_value(struct symbol *sym) 449 { 450 struct symbol_value newval, oldval; 451 struct property *prop = NULL; 452 struct menu *choice_menu; 453 454 if (!sym) 455 return; 456 457 if (sym->flags & SYMBOL_VALID) 458 return; 459 460 sym->flags |= SYMBOL_VALID; 461 462 oldval = sym->curr; 463 464 newval.tri = no; 465 466 switch (sym->type) { 467 case S_INT: 468 newval.val = "0"; 469 break; 470 case S_HEX: 471 newval.val = "0x0"; 472 break; 473 case S_STRING: 474 newval.val = ""; 475 break; 476 case S_BOOLEAN: 477 case S_TRISTATE: 478 newval.val = "n"; 479 break; 480 default: 481 sym->curr.val = sym->name; 482 sym->curr.tri = no; 483 return; 484 } 485 sym->flags &= ~SYMBOL_WRITE; 486 487 sym_calc_visibility(sym); 488 489 if (sym->visible != no) 490 sym->flags |= SYMBOL_WRITE; 491 492 /* set default if recursively called */ 493 sym->curr = newval; 494 495 switch (sym_get_type(sym)) { 496 case S_BOOLEAN: 497 case S_TRISTATE: 498 choice_menu = sym_get_choice_menu(sym); 499 500 if (choice_menu) { 501 sym_calc_choice(choice_menu); 502 newval.tri = sym->curr.tri; 503 } else { 504 if (sym->visible != no) { 505 /* if the symbol is visible use the user value 506 * if available, otherwise try the default value 507 */ 508 if (sym_has_value(sym)) { 509 newval.tri = EXPR_AND(sym->def[S_DEF_USER].tri, 510 sym->visible); 511 goto calc_newval; 512 } 513 } 514 if (sym->rev_dep.tri != no) 515 sym->flags |= SYMBOL_WRITE; 516 if (!sym_is_choice(sym)) { 517 prop = sym_get_default_prop(sym); 518 if (prop) { 519 newval.tri = EXPR_AND(expr_calc_value(prop->expr), 520 prop->visible.tri); 521 if (newval.tri != no) 522 sym->flags |= SYMBOL_WRITE; 523 } 524 if (sym->implied.tri != no) { 525 sym->flags |= SYMBOL_WRITE; 526 newval.tri = EXPR_OR(newval.tri, sym->implied.tri); 527 newval.tri = EXPR_AND(newval.tri, 528 sym->dir_dep.tri); 529 } 530 } 531 calc_newval: 532 if (sym->dir_dep.tri < sym->rev_dep.tri) 533 sym_warn_unmet_dep(sym); 534 newval.tri = EXPR_OR(newval.tri, sym->rev_dep.tri); 535 } 536 if (newval.tri == mod && sym_get_type(sym) == S_BOOLEAN) 537 newval.tri = yes; 538 break; 539 case S_STRING: 540 case S_HEX: 541 case S_INT: 542 if (sym->visible != no && sym_has_value(sym)) { 543 newval.val = sym->def[S_DEF_USER].val; 544 break; 545 } 546 prop = sym_get_default_prop(sym); 547 if (prop) { 548 struct symbol *ds = prop_get_symbol(prop); 549 if (ds) { 550 sym->flags |= SYMBOL_WRITE; 551 sym_calc_value(ds); 552 newval.val = ds->curr.val; 553 } 554 } 555 break; 556 default: 557 ; 558 } 559 560 /* 561 * If the symbol lacks a user value but its value comes from a 562 * single transitional symbol with an existing user value, mark 563 * this symbol as having a user value to avoid prompting. 564 */ 565 if (prop && !sym_has_value(sym)) { 566 struct symbol *ds = prop_get_symbol(prop); 567 if (ds && (ds->flags & SYMBOL_TRANS) && sym_has_value(ds)) { 568 sym->def[S_DEF_USER] = newval; 569 sym->flags |= SYMBOL_DEF_USER; 570 } 571 } 572 573 sym->curr = newval; 574 sym_validate_range(sym); 575 576 if (memcmp(&oldval, &sym->curr, sizeof(oldval))) { 577 sym_set_changed(sym); 578 if (modules_sym == sym) { 579 sym_set_all_changed(); 580 modules_val = modules_sym->curr.tri; 581 } 582 } 583 584 if (sym_is_choice(sym) || sym->flags & SYMBOL_TRANS) 585 sym->flags &= ~SYMBOL_WRITE; 586 } 587 588 void sym_clear_all_valid(void) 589 { 590 struct symbol *sym; 591 592 for_all_symbols(sym) 593 sym->flags &= ~SYMBOL_VALID; 594 expr_invalidate_all(); 595 conf_set_changed(true); 596 sym_calc_value(modules_sym); 597 } 598 599 bool sym_tristate_within_range(const struct symbol *sym, tristate val) 600 { 601 int type = sym_get_type(sym); 602 603 if (sym->visible == no) 604 return false; 605 606 if (type != S_BOOLEAN && type != S_TRISTATE) 607 return false; 608 609 if (type == S_BOOLEAN && val == mod) 610 return false; 611 if (sym->visible <= sym->rev_dep.tri) 612 return false; 613 return val >= sym->rev_dep.tri && val <= sym->visible; 614 } 615 616 bool sym_set_tristate_value(struct symbol *sym, tristate val) 617 { 618 tristate oldval = sym_get_tristate_value(sym); 619 620 if (!sym_tristate_within_range(sym, val)) 621 return false; 622 623 if (!(sym->flags & SYMBOL_DEF_USER) || sym->def[S_DEF_USER].tri != val) { 624 sym->def[S_DEF_USER].tri = val; 625 sym->flags |= SYMBOL_DEF_USER; 626 sym_set_changed(sym); 627 } 628 629 if (oldval != val) 630 sym_clear_all_valid(); 631 632 return true; 633 } 634 635 /** 636 * choice_set_value - set the user input to a choice 637 * 638 * @choice: menu entry for the choice 639 * @sym: selected symbol 640 */ 641 void choice_set_value(struct menu *choice, struct symbol *sym) 642 { 643 struct menu *menu; 644 bool changed = false; 645 646 menu_for_each_sub_entry(menu, choice) { 647 tristate val; 648 649 if (!menu->sym) 650 continue; 651 652 if (menu->sym->visible == no) 653 continue; 654 655 val = menu->sym == sym ? yes : no; 656 657 if (menu->sym->curr.tri != val) 658 changed = true; 659 660 menu->sym->def[S_DEF_USER].tri = val; 661 menu->sym->flags |= SYMBOL_DEF_USER; 662 663 /* 664 * Now, the user has explicitly enabled or disabled this symbol, 665 * it should be given the highest priority. We are possibly 666 * setting multiple symbols to 'n', where the first symbol is 667 * given the least prioritized 'n'. This works well when the 668 * choice block ends up with selecting 'n' symbol. 669 * (see sym_calc_choice()) 670 */ 671 list_move(&menu->sym->choice_link, &choice->choice_members); 672 } 673 674 if (changed) 675 sym_clear_all_valid(); 676 } 677 678 tristate sym_toggle_tristate_value(struct symbol *sym) 679 { 680 struct menu *choice; 681 tristate oldval, newval; 682 683 choice = sym_get_choice_menu(sym); 684 if (choice) { 685 choice_set_value(choice, sym); 686 return yes; 687 } 688 689 oldval = newval = sym_get_tristate_value(sym); 690 do { 691 switch (newval) { 692 case no: 693 newval = mod; 694 break; 695 case mod: 696 newval = yes; 697 break; 698 case yes: 699 newval = no; 700 break; 701 } 702 if (sym_set_tristate_value(sym, newval)) 703 break; 704 } while (oldval != newval); 705 return newval; 706 } 707 708 bool sym_string_valid(struct symbol *sym, const char *str) 709 { 710 signed char ch; 711 712 switch (sym->type) { 713 case S_STRING: 714 return true; 715 case S_INT: 716 ch = *str++; 717 if (ch == '-') 718 ch = *str++; 719 if (!isdigit(ch)) 720 return false; 721 if (ch == '0' && *str != 0) 722 return false; 723 while ((ch = *str++)) { 724 if (!isdigit(ch)) 725 return false; 726 } 727 return true; 728 case S_HEX: 729 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X')) 730 str += 2; 731 ch = *str++; 732 do { 733 if (!isxdigit(ch)) 734 return false; 735 } while ((ch = *str++)); 736 return true; 737 case S_BOOLEAN: 738 case S_TRISTATE: 739 switch (str[0]) { 740 case 'y': case 'Y': 741 case 'm': case 'M': 742 case 'n': case 'N': 743 return true; 744 } 745 return false; 746 default: 747 return false; 748 } 749 } 750 751 bool sym_string_check_bounds(struct symbol *sym, const char *str) 752 { 753 errno = 0; 754 755 if (sym->type == S_INT) 756 strtoll(str, NULL, 10); 757 else if (sym->type == S_HEX) 758 strtoull(str, NULL, 16); 759 else 760 /* string */ 761 return true; 762 763 return errno != ERANGE; 764 } 765 766 bool sym_string_within_range(struct symbol *sym, const char *str) 767 { 768 struct property *prop; 769 long long val; 770 unsigned long long uval; 771 772 switch (sym->type) { 773 case S_STRING: 774 return sym_string_valid(sym, str); 775 case S_INT: 776 if (!sym_string_valid(sym, str)) 777 return false; 778 if (!sym_string_check_bounds(sym, str)) 779 return false; 780 prop = sym_get_range_prop(sym); 781 if (!prop) 782 return true; 783 val = strtoll(str, NULL, 10); 784 return val >= sym_get_range_val(prop->expr->left.sym, 10).s && 785 val <= sym_get_range_val(prop->expr->right.sym, 10).s; 786 case S_HEX: 787 if (!sym_string_valid(sym, str)) 788 return false; 789 if (!sym_string_check_bounds(sym, str)) 790 return false; 791 prop = sym_get_range_prop(sym); 792 if (!prop) 793 return true; 794 uval = strtoull(str, NULL, 16); 795 return uval >= sym_get_range_val(prop->expr->left.sym, 16).u && 796 uval <= sym_get_range_val(prop->expr->right.sym, 16).u; 797 case S_BOOLEAN: 798 case S_TRISTATE: 799 switch (str[0]) { 800 case 'y': case 'Y': 801 return sym_tristate_within_range(sym, yes); 802 case 'm': case 'M': 803 return sym_tristate_within_range(sym, mod); 804 case 'n': case 'N': 805 return sym_tristate_within_range(sym, no); 806 } 807 return false; 808 default: 809 return false; 810 } 811 } 812 813 bool sym_set_string_value(struct symbol *sym, const char *newval) 814 { 815 const char *oldval; 816 char *val; 817 int size; 818 819 switch (sym->type) { 820 case S_BOOLEAN: 821 case S_TRISTATE: 822 switch (newval[0]) { 823 case 'y': case 'Y': 824 return sym_set_tristate_value(sym, yes); 825 case 'm': case 'M': 826 return sym_set_tristate_value(sym, mod); 827 case 'n': case 'N': 828 return sym_set_tristate_value(sym, no); 829 } 830 return false; 831 default: 832 ; 833 } 834 835 if (!sym_string_within_range(sym, newval)) 836 return false; 837 838 if (!(sym->flags & SYMBOL_DEF_USER)) { 839 sym->flags |= SYMBOL_DEF_USER; 840 sym_set_changed(sym); 841 } 842 843 oldval = sym->def[S_DEF_USER].val; 844 size = strlen(newval) + 1; 845 if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) { 846 size += 2; 847 sym->def[S_DEF_USER].val = val = xmalloc(size); 848 *val++ = '0'; 849 *val++ = 'x'; 850 } else if (!oldval || strcmp(oldval, newval)) 851 sym->def[S_DEF_USER].val = val = xmalloc(size); 852 else 853 return true; 854 855 strcpy(val, newval); 856 free((void *)oldval); 857 sym_clear_all_valid(); 858 859 return true; 860 } 861 862 /* 863 * Find the default value associated to a symbol. 864 * For tristate symbol handle the modules=n case 865 * in which case "m" becomes "y". 866 * If the symbol does not have any default then fallback 867 * to the fixed default values. 868 */ 869 const char *sym_get_string_default(struct symbol *sym) 870 { 871 struct property *prop; 872 struct symbol *ds, *range_sym; 873 const char *str = ""; 874 tristate val; 875 876 sym_calc_visibility(sym); 877 sym_calc_value(modules_sym); 878 val = symbol_no.curr.tri; 879 880 /* If symbol has a default value look it up */ 881 prop = sym_get_default_prop(sym); 882 if (prop != NULL) { 883 switch (sym->type) { 884 case S_BOOLEAN: 885 case S_TRISTATE: 886 /* The visibility may limit the value from yes => mod */ 887 val = EXPR_AND(expr_calc_value(prop->expr), prop->visible.tri); 888 break; 889 default: 890 ds = prop_get_symbol(prop); 891 if (ds != NULL) { 892 sym_calc_value(ds); 893 str = (const char *)ds->curr.val; 894 } 895 } 896 } 897 898 /* Handle select statements */ 899 val = EXPR_OR(val, sym->rev_dep.tri); 900 901 /* transpose mod to yes if modules are not enabled */ 902 if (val == mod) 903 if (!sym_is_choice_value(sym) && modules_sym->curr.tri == no) 904 val = yes; 905 906 /* transpose mod to yes if type is bool */ 907 if (sym->type == S_BOOLEAN && val == mod) 908 val = yes; 909 910 /* adjust the default value if this symbol is implied by another */ 911 if (val < sym->implied.tri) 912 val = sym->implied.tri; 913 914 switch (sym->type) { 915 case S_BOOLEAN: 916 case S_TRISTATE: 917 switch (val) { 918 case no: return "n"; 919 case mod: return "m"; 920 case yes: return "y"; 921 } 922 case S_INT: 923 if (!str[0]) 924 str = "0"; 925 break; 926 case S_HEX: 927 if (!str[0]) 928 str = "0x0"; 929 break; 930 default: 931 break; 932 } 933 934 range_sym = sym_get_near_range_bound(sym, str); 935 if (range_sym) 936 str = range_sym->curr.val; 937 938 return str; 939 } 940 941 const char *sym_get_string_value(struct symbol *sym) 942 { 943 tristate val; 944 945 switch (sym->type) { 946 case S_BOOLEAN: 947 case S_TRISTATE: 948 val = sym_get_tristate_value(sym); 949 switch (val) { 950 case no: 951 return "n"; 952 case mod: 953 return "m"; 954 case yes: 955 return "y"; 956 } 957 break; 958 default: 959 ; 960 } 961 return sym->curr.val; 962 } 963 964 bool sym_is_changeable(const struct symbol *sym) 965 { 966 return !sym_is_choice(sym) && sym->visible > sym->rev_dep.tri; 967 } 968 969 bool sym_is_choice_value(const struct symbol *sym) 970 { 971 return !list_empty(&sym->choice_link); 972 } 973 974 HASHTABLE_DEFINE(sym_hashtable, SYMBOL_HASHSIZE); 975 976 struct symbol *sym_lookup(const char *name, int flags) 977 { 978 struct symbol *symbol; 979 char *new_name; 980 int hash; 981 982 if (name) { 983 if (name[0] && !name[1]) { 984 switch (name[0]) { 985 case 'y': return &symbol_yes; 986 case 'm': return &symbol_mod; 987 case 'n': return &symbol_no; 988 } 989 } 990 hash = hash_str(name); 991 992 hash_for_each_possible(sym_hashtable, symbol, node, hash) { 993 if (symbol->name && 994 !strcmp(symbol->name, name) && 995 (flags ? symbol->flags & flags 996 : !(symbol->flags & SYMBOL_CONST))) 997 return symbol; 998 } 999 new_name = xstrdup(name); 1000 } else { 1001 new_name = NULL; 1002 hash = 0; 1003 } 1004 1005 symbol = xmalloc(sizeof(*symbol)); 1006 memset(symbol, 0, sizeof(*symbol)); 1007 symbol->name = new_name; 1008 symbol->type = S_UNKNOWN; 1009 symbol->flags = flags; 1010 INIT_LIST_HEAD(&symbol->menus); 1011 INIT_LIST_HEAD(&symbol->choice_link); 1012 1013 hash_add(sym_hashtable, &symbol->node, hash); 1014 1015 return symbol; 1016 } 1017 1018 struct symbol *sym_find(const char *name) 1019 { 1020 struct symbol *symbol = NULL; 1021 int hash = 0; 1022 1023 if (!name) 1024 return NULL; 1025 1026 if (name[0] && !name[1]) { 1027 switch (name[0]) { 1028 case 'y': return &symbol_yes; 1029 case 'm': return &symbol_mod; 1030 case 'n': return &symbol_no; 1031 } 1032 } 1033 hash = hash_str(name); 1034 1035 hash_for_each_possible(sym_hashtable, symbol, node, hash) { 1036 if (symbol->name && 1037 !strcmp(symbol->name, name) && 1038 !(symbol->flags & SYMBOL_CONST)) 1039 break; 1040 } 1041 1042 return symbol; 1043 } 1044 1045 struct sym_match { 1046 struct symbol *sym; 1047 off_t so, eo; 1048 }; 1049 1050 /* Compare matched symbols as thus: 1051 * - first, symbols that match exactly 1052 * - then, alphabetical sort 1053 */ 1054 static int sym_rel_comp(const void *sym1, const void *sym2) 1055 { 1056 const struct sym_match *s1 = sym1; 1057 const struct sym_match *s2 = sym2; 1058 int exact1, exact2; 1059 1060 /* Exact match: 1061 * - if matched length on symbol s1 is the length of that symbol, 1062 * then this symbol should come first; 1063 * - if matched length on symbol s2 is the length of that symbol, 1064 * then this symbol should come first. 1065 * Note: since the search can be a regexp, both symbols may match 1066 * exactly; if this is the case, we can't decide which comes first, 1067 * and we fallback to sorting alphabetically. 1068 */ 1069 exact1 = (s1->eo - s1->so) == strlen(s1->sym->name); 1070 exact2 = (s2->eo - s2->so) == strlen(s2->sym->name); 1071 if (exact1 && !exact2) 1072 return -1; 1073 if (!exact1 && exact2) 1074 return 1; 1075 1076 /* As a fallback, sort symbols alphabetically */ 1077 return strcmp(s1->sym->name, s2->sym->name); 1078 } 1079 1080 struct symbol **sym_re_search(const char *pattern) 1081 { 1082 struct symbol *sym, **sym_arr = NULL; 1083 struct sym_match *sym_match_arr = NULL; 1084 int i, cnt, size; 1085 regex_t re; 1086 regmatch_t match[1]; 1087 1088 cnt = size = 0; 1089 /* Skip if empty */ 1090 if (strlen(pattern) == 0) 1091 return NULL; 1092 if (regcomp(&re, pattern, REG_EXTENDED|REG_ICASE)) 1093 return NULL; 1094 1095 for_all_symbols(sym) { 1096 if (sym->flags & SYMBOL_CONST || !sym->name) 1097 continue; 1098 if (regexec(&re, sym->name, 1, match, 0)) 1099 continue; 1100 if (cnt >= size) { 1101 void *tmp; 1102 size += 16; 1103 tmp = realloc(sym_match_arr, size * sizeof(struct sym_match)); 1104 if (!tmp) 1105 goto sym_re_search_free; 1106 sym_match_arr = tmp; 1107 } 1108 sym_calc_value(sym); 1109 /* As regexec returned 0, we know we have a match, so 1110 * we can use match[0].rm_[se]o without further checks 1111 */ 1112 sym_match_arr[cnt].so = match[0].rm_so; 1113 sym_match_arr[cnt].eo = match[0].rm_eo; 1114 sym_match_arr[cnt++].sym = sym; 1115 } 1116 if (sym_match_arr) { 1117 qsort(sym_match_arr, cnt, sizeof(struct sym_match), sym_rel_comp); 1118 sym_arr = malloc((cnt+1) * sizeof(struct symbol *)); 1119 if (!sym_arr) 1120 goto sym_re_search_free; 1121 for (i = 0; i < cnt; i++) 1122 sym_arr[i] = sym_match_arr[i].sym; 1123 sym_arr[cnt] = NULL; 1124 } 1125 sym_re_search_free: 1126 /* sym_match_arr can be NULL if no match, but free(NULL) is OK */ 1127 free(sym_match_arr); 1128 regfree(&re); 1129 1130 return sym_arr; 1131 } 1132 1133 /* 1134 * When we check for recursive dependencies we use a stack to save 1135 * current state so we can print out relevant info to user. 1136 * The entries are located on the call stack so no need to free memory. 1137 * Note insert() remove() must always match to properly clear the stack. 1138 */ 1139 static struct dep_stack { 1140 struct dep_stack *prev, *next; 1141 struct symbol *sym; 1142 struct property *prop; 1143 struct expr **expr; 1144 } *check_top; 1145 1146 static void dep_stack_insert(struct dep_stack *stack, struct symbol *sym) 1147 { 1148 memset(stack, 0, sizeof(*stack)); 1149 if (check_top) 1150 check_top->next = stack; 1151 stack->prev = check_top; 1152 stack->sym = sym; 1153 check_top = stack; 1154 } 1155 1156 static void dep_stack_remove(void) 1157 { 1158 check_top = check_top->prev; 1159 if (check_top) 1160 check_top->next = NULL; 1161 } 1162 1163 /* 1164 * Called when we have detected a recursive dependency. 1165 * check_top point to the top of the stact so we use 1166 * the ->prev pointer to locate the bottom of the stack. 1167 */ 1168 static void sym_check_print_recursive(struct symbol *last_sym) 1169 { 1170 struct dep_stack *stack; 1171 struct symbol *sym, *next_sym; 1172 struct menu *choice; 1173 struct dep_stack cv_stack; 1174 enum prop_type type; 1175 1176 choice = sym_get_choice_menu(last_sym); 1177 if (choice) { 1178 dep_stack_insert(&cv_stack, last_sym); 1179 last_sym = choice->sym; 1180 } 1181 1182 for (stack = check_top; stack != NULL; stack = stack->prev) 1183 if (stack->sym == last_sym) 1184 break; 1185 if (!stack) { 1186 fprintf(stderr, "unexpected recursive dependency error\n"); 1187 return; 1188 } 1189 1190 for (; stack; stack = stack->next) { 1191 sym = stack->sym; 1192 next_sym = stack->next ? stack->next->sym : last_sym; 1193 type = stack->prop ? stack->prop->type : P_UNKNOWN; 1194 1195 if (stack->sym == last_sym) 1196 fprintf(stderr, "error: recursive dependency detected!\n"); 1197 1198 if (sym_is_choice(next_sym)) { 1199 choice = list_first_entry(&next_sym->menus, struct menu, link); 1200 1201 fprintf(stderr, "\tsymbol %s is part of choice block at %s:%d\n", 1202 sym->name ? sym->name : "<choice>", 1203 choice->filename, choice->lineno); 1204 } else if (stack->expr == &sym->dir_dep.expr) { 1205 fprintf(stderr, "\tsymbol %s depends on %s\n", 1206 sym->name ? sym->name : "<choice>", 1207 next_sym->name); 1208 } else if (stack->expr == &sym->rev_dep.expr) { 1209 fprintf(stderr, "\tsymbol %s is selected by %s\n", 1210 sym->name, next_sym->name); 1211 } else if (stack->expr == &sym->implied.expr) { 1212 fprintf(stderr, "\tsymbol %s is implied by %s\n", 1213 sym->name, next_sym->name); 1214 } else if (stack->expr) { 1215 fprintf(stderr, "\tsymbol %s %s value contains %s\n", 1216 sym->name ? sym->name : "<choice>", 1217 prop_get_type_name(type), 1218 next_sym->name); 1219 } else { 1220 fprintf(stderr, "\tsymbol %s %s is visible depending on %s\n", 1221 sym->name ? sym->name : "<choice>", 1222 prop_get_type_name(type), 1223 next_sym->name); 1224 } 1225 } 1226 1227 fprintf(stderr, 1228 "For a resolution refer to Documentation/kbuild/kconfig-language.rst\n" 1229 "subsection \"Kconfig recursive dependency limitations\"\n" 1230 "\n"); 1231 1232 if (check_top == &cv_stack) 1233 dep_stack_remove(); 1234 } 1235 1236 static struct symbol *sym_check_expr_deps(const struct expr *e) 1237 { 1238 struct symbol *sym; 1239 1240 if (!e) 1241 return NULL; 1242 switch (e->type) { 1243 case E_OR: 1244 case E_AND: 1245 sym = sym_check_expr_deps(e->left.expr); 1246 if (sym) 1247 return sym; 1248 return sym_check_expr_deps(e->right.expr); 1249 case E_NOT: 1250 return sym_check_expr_deps(e->left.expr); 1251 case E_EQUAL: 1252 case E_GEQ: 1253 case E_GTH: 1254 case E_LEQ: 1255 case E_LTH: 1256 case E_RANGE: 1257 case E_UNEQUAL: 1258 sym = sym_check_deps(e->left.sym); 1259 if (sym) 1260 return sym; 1261 return sym_check_deps(e->right.sym); 1262 case E_SYMBOL: 1263 return sym_check_deps(e->left.sym); 1264 default: 1265 break; 1266 } 1267 fprintf(stderr, "Oops! How to check %d?\n", e->type); 1268 return NULL; 1269 } 1270 1271 /* return NULL when dependencies are OK */ 1272 static struct symbol *sym_check_sym_deps(struct symbol *sym) 1273 { 1274 struct symbol *sym2; 1275 struct property *prop; 1276 struct dep_stack stack; 1277 1278 dep_stack_insert(&stack, sym); 1279 1280 stack.expr = &sym->dir_dep.expr; 1281 sym2 = sym_check_expr_deps(sym->dir_dep.expr); 1282 if (sym2) 1283 goto out; 1284 1285 stack.expr = &sym->rev_dep.expr; 1286 sym2 = sym_check_expr_deps(sym->rev_dep.expr); 1287 if (sym2) 1288 goto out; 1289 1290 stack.expr = &sym->implied.expr; 1291 sym2 = sym_check_expr_deps(sym->implied.expr); 1292 if (sym2) 1293 goto out; 1294 1295 stack.expr = NULL; 1296 1297 for (prop = sym->prop; prop; prop = prop->next) { 1298 if (prop->type == P_SELECT || prop->type == P_IMPLY) 1299 continue; 1300 stack.prop = prop; 1301 sym2 = sym_check_expr_deps(prop->visible.expr); 1302 if (sym2) 1303 break; 1304 if (sym_is_choice(sym)) 1305 continue; 1306 if (prop->type != P_DEFAULT && prop->type != P_RANGE) 1307 continue; 1308 stack.expr = &prop->expr; 1309 sym2 = sym_check_expr_deps(prop->expr); 1310 if (sym2) 1311 break; 1312 stack.expr = NULL; 1313 } 1314 1315 out: 1316 dep_stack_remove(); 1317 1318 return sym2; 1319 } 1320 1321 static struct symbol *sym_check_choice_deps(struct symbol *choice) 1322 { 1323 struct menu *choice_menu, *menu; 1324 struct symbol *sym2; 1325 struct dep_stack stack; 1326 1327 dep_stack_insert(&stack, choice); 1328 1329 choice_menu = list_first_entry(&choice->menus, struct menu, link); 1330 1331 menu_for_each_sub_entry(menu, choice_menu) { 1332 if (menu->sym) 1333 menu->sym->flags |= SYMBOL_CHECK | SYMBOL_CHECKED; 1334 } 1335 1336 choice->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED); 1337 sym2 = sym_check_sym_deps(choice); 1338 choice->flags &= ~SYMBOL_CHECK; 1339 if (sym2) 1340 goto out; 1341 1342 menu_for_each_sub_entry(menu, choice_menu) { 1343 if (!menu->sym) 1344 continue; 1345 sym2 = sym_check_sym_deps(menu->sym); 1346 if (sym2) 1347 break; 1348 } 1349 out: 1350 menu_for_each_sub_entry(menu, choice_menu) 1351 if (menu->sym) 1352 menu->sym->flags &= ~SYMBOL_CHECK; 1353 1354 if (sym2) { 1355 struct menu *choice_menu2; 1356 1357 choice_menu2 = sym_get_choice_menu(sym2); 1358 if (choice_menu2 == choice_menu) 1359 sym2 = choice; 1360 } 1361 1362 dep_stack_remove(); 1363 1364 return sym2; 1365 } 1366 1367 struct symbol *sym_check_deps(struct symbol *sym) 1368 { 1369 struct menu *choice; 1370 struct symbol *sym2; 1371 1372 if (sym->flags & SYMBOL_CHECK) { 1373 sym_check_print_recursive(sym); 1374 return sym; 1375 } 1376 if (sym->flags & SYMBOL_CHECKED) 1377 return NULL; 1378 1379 choice = sym_get_choice_menu(sym); 1380 if (choice) { 1381 struct dep_stack stack; 1382 1383 /* for choice groups start the check with main choice symbol */ 1384 dep_stack_insert(&stack, sym); 1385 sym2 = sym_check_deps(choice->sym); 1386 dep_stack_remove(); 1387 } else if (sym_is_choice(sym)) { 1388 sym2 = sym_check_choice_deps(sym); 1389 } else { 1390 sym->flags |= (SYMBOL_CHECK | SYMBOL_CHECKED); 1391 sym2 = sym_check_sym_deps(sym); 1392 sym->flags &= ~SYMBOL_CHECK; 1393 } 1394 1395 return sym2; 1396 } 1397 1398 struct symbol *prop_get_symbol(const struct property *prop) 1399 { 1400 if (prop->expr && prop->expr->type == E_SYMBOL) 1401 return prop->expr->left.sym; 1402 return NULL; 1403 } 1404 1405 const char *prop_get_type_name(enum prop_type type) 1406 { 1407 switch (type) { 1408 case P_PROMPT: 1409 return "prompt"; 1410 case P_COMMENT: 1411 return "comment"; 1412 case P_MENU: 1413 return "menu"; 1414 case P_DEFAULT: 1415 return "default"; 1416 case P_SELECT: 1417 return "select"; 1418 case P_IMPLY: 1419 return "imply"; 1420 case P_RANGE: 1421 return "range"; 1422 case P_UNKNOWN: 1423 break; 1424 } 1425 return "unknown"; 1426 } 1427