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