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