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