1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169
|
/*****************************************************************************/
/* Copyright (C) 2020 NORMAN MEGILL nm at alum.mit.edu */
/* License terms: GNU General Public License */
/*****************************************************************************/
/*34567890123456 (79-character line to adjust editor window) 2345678901234567*/
/*
mmdata.c
*/
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <ctype.h>
#include "mmvstr.h"
#include "mmdata.h"
#include "mminou.h"
#include "mmpars.h"
#include "mmcmdl.h" /* Needed for g_logFileName */
#include "mmpfas.h" /* Needed for g_proveStatement */
#include "mmwtex.h" /* Needed for SMALL_DECORATION etc. */
#include <limits.h>
#include <setjmp.h>
/*E*/long db=0,db0=0,db2=0,db3=0,db4=0,db5=0,db6=0,db7=0,db8=0,db9=0;
flag g_listMode = 0; /* 0 = metamath, 1 = list utility */
flag g_toolsMode = 0; /* In metamath: 0 = metamath, 1 = text tools utility */
/* 4-May-2015 nm */
/* For use by getMarkupFlag() */
vstring g_proofDiscouragedMarkup = "";
vstring g_usageDiscouragedMarkup = "";
flag g_globalDiscouragement = 1; /* SET DISCOURAGEMENT ON */
/* 14-May-2017 nm */
vstring g_contributorName = "";
/* Global variables related to current statement */
int g_currentScope = 0;
/*long beginScopeStatementNum = 0;*/
long g_MAX_STATEMENTS = 1;
long g_MAX_MATHTOKENS = 1;
long g_MAX_INCLUDECALLS = 2; /* Must be at least 2 (the single-file case) !!!
(A dummy extra top entry is used by parseKeywords().) */
struct statement_struct *g_Statement = NULL;
long *g_labelKey = NULL; /* 4-May-2017 Ari Ferrera - added "= NULL" */
struct mathToken_struct *g_MathToken;
long *g_mathKey = NULL;
long g_statements = 0, labels = 0, g_mathTokens = 0;
/*long maxMathTokenLength = 0;*/ /* 15-Aug-2020 nm Not used */
struct includeCall_struct *g_IncludeCall = NULL; /* 4-May-2017 Ari Ferrera
- added "= NULL" */
long g_includeCalls = -1; /* For eraseSouce() in mmcmds.c */
char *g_sourcePtr = NULL; /* 4-May-2017 Ari Ferrera - added "= NULL" */
long g_sourceLen;
/* 18-Jan-05 nm The structs below, and several other places, were changed
from hard-coded byte lengths to 'sizeof's by Waldek Hebisch
(hebisch at math dot uni dot wroc dot pl) so this will work on the
AMD64. */
/* Null numString */
struct nullNmbrStruct g_NmbrNull = {-1, sizeof(long), sizeof(long), -1};
/* Null ptrString */
struct nullPntrStruct g_PntrNull = {-1, sizeof(long), sizeof(long), NULL};
nmbrString *nmbrTempAlloc(long size);
/* nmbrString memory allocation/deallocation */
void nmbrCpy(nmbrString *sout, nmbrString *sin);
void nmbrNCpy(nmbrString *s, nmbrString *t, long n);
pntrString *pntrTempAlloc(long size);
/* pntrString memory allocation/deallocation */
void pntrCpy(pntrString *sout, pntrString *sin);
void pntrNCpy(pntrString *s, pntrString *t, long n);
vstring g_qsortKey; /* Used by qsortStringCmp; pointer only, do not deallocate */
/* Memory pools are used to reduce the number of malloc and alloc calls that
allocate arrays (strings or nmbr/pntrStrings typically). The "free" pool
contains previously allocated arrays that are no longer used but that we
have not freed yet. A call to allocate a new array fetches one from here
first. The "used"
pool contains arrays that are partially used; each array has some free space
at the end that can be deallocated if memory gets low. Any array that is
totally used (no free space) is not in any pool. */
/* Each pool array has 3 "hidden" long elements before it, used by these
procedures.
Element -1: actual size (bytes) of array, excluding the 3 "hidden"
long elements.
Element -2: allocated size. If all elements are used, allocated = actual.
Element -3: location of array in memUsedPool. If -1, it means that
actual = allocated and storage in memUsedPool is therefore not nec.
The pointer to an array always points to element 0 (recast to right size).
*/
#define MEM_POOL_GROW 1000 /* Amount that a pool grows when it overflows. */
/*??? Let user set this from menu. */
long poolAbsoluteMax = /*2000000*/1000000; /* Pools will be purged when this is reached */
long poolTotalFree = 0; /* Total amount of free space allocated in pool */
/*E*/long i1,j1_,k1; /* 11-Sep-2009 nm Fix "built-in function 'j1'" warning */
void **memUsedPool = NULL;
long memUsedPoolSize = 0; /* Current # of partially filled arrays in use */
long memUsedPoolMax = 0; /* Maximum # of entries in 'in use' table (grows
as nec.) */
void **memFreePool = NULL;
long memFreePoolSize = 0; /* Current # of available, allocated arrays */
long memFreePoolMax = 0; /* Maximum # of entries in 'free' table (grows
as nec.) */
/* poolFixedMalloc should be called when the allocated array will rarely be
changed; a malloc or realloc with no unused array bytes will be done. */
void *poolFixedMalloc(long size /* bytes */)
{
void *ptr;
void *ptr2;
/*E*/ /* 11-Jul-2014 nm Don't call print2() if db9 is set, since it will */
/*E*/ /* recursively call the pool stuff causing a crash. I changed */
/*E*/ /* 41 cases of print2() to printf() below to resolve this. */
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("a0: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
if (!memFreePoolSize) { /* The pool is empty; we must allocate memory */
ptr = malloc( 3 * sizeof(long) + (size_t)size);
if (!ptr) outOfMemory(
cat("#25 (poolFixedMalloc ", str((double)size), ")", NULL));
ptr = (long *)ptr + 3;
((long *)ptr)[-1] = size; /* Actual size */
((long *)ptr)[-2] = size; /* Allocated size */
((long *)ptr)[-3] = -1; /* Location in memUsedPool (-1 = none) */
return (ptr);
} else {
memFreePoolSize--;
ptr = memFreePool[memFreePoolSize];
poolTotalFree = poolTotalFree - ((long *)ptr)[-2];
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("a: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
if (size <= ((long *)ptr)[-2]) { /* We have enough space already */
ptr2 = realloc( (long *)ptr - 3, 3 * sizeof(long) + (size_t)size);
/* Reallocation cannot fail, since we are shrinking space */
if (!ptr2) bug(1382);
ptr = ptr2;
} else { /* The pool's last entry is too small; free and allocate new */
free((long *)ptr - 3);
ptr = malloc( 3 * sizeof(long) + (size_t)size);
}
if (!ptr) {
/* Try freeing space */
print2("Memory is low. Deallocating storage pool...\n");
memFreePoolPurge(0);
ptr = malloc( 3 * sizeof(long) + (size_t)size);
if (!ptr) outOfMemory(
cat("#26 (poolMalloc ", str((double)size), ")", NULL));
/* Nothing more can be done */
}
ptr = (long *)ptr + 3;
((long *)ptr)[-1] = size; /* Actual size */
((long *)ptr)[-2] = size; /* Allocated size */
((long *)ptr)[-3] = -1; /* Location in memUsedPool (-1 = none) */
return (ptr);
}
}
/* poolMalloc tries first to use an array in the memFreePool before actually
malloc'ing */
void *poolMalloc(long size /* bytes */)
{
void *ptr;
long memUsedPoolTmpMax;
void *memUsedPoolTmpPtr;
/* Check to see if the pool total exceeds max. */
if (poolTotalFree > poolAbsoluteMax) {
memFreePoolPurge(1);
}
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("b0: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
if (!memFreePoolSize) { /* The pool is empty; we must allocate memory */
ptr = malloc( 3 * sizeof(long) + (size_t)size);
if (!ptr) {
outOfMemory(cat("#27 (poolMalloc ", str((double)size), ")", NULL));
}
ptr = (long *)ptr + 3;
((long *)ptr)[-1] = size; /* Actual size */
((long *)ptr)[-2] = size; /* Allocated size */
((long *)ptr)[-3] = -1; /* Location in memUsedPool (-1 = none) */
return (ptr);
} else {
memFreePoolSize--;
ptr = memFreePool[memFreePoolSize];
poolTotalFree = poolTotalFree - ((long *)ptr)[-2];
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("b: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
if (size <= ((long *)ptr)[-2]) { /* We have enough space already */
((long *)ptr)[-1] = size; /* Actual size */
((long *)ptr)[-3] = -1; /* Not in storage pool yet */
} else { /* We must reallocate */
free((long *)ptr - 3);
ptr = malloc( 3 * sizeof(long) + (size_t)size);
if (!ptr) {
/* Try freeing space */
print2("Memory is low. Deallocating storage pool...\n");
memFreePoolPurge(0);
ptr = malloc( 3 * sizeof(long) + (size_t)size);
if (!ptr) outOfMemory(
cat("#28 (poolMalloc ", str((double)size), ")", NULL));
/* Nothing more can be done */
}
ptr = (long *)ptr + 3;
((long *)ptr)[-1] = size; /* Actual size */
((long *)ptr)[-2] = size; /* Allocated size */
((long *)ptr)[-3] = -1; /* Location in memUsedPool (-1 = none) */
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("bb: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
return (ptr);
}
}
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("bc: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
if (((long *)ptr)[-1] == ((long *)ptr)[-2]) return (ptr);
/* Allocated and actual sizes are different, so add this array to used pool */
poolTotalFree = poolTotalFree + ((long *)ptr)[-2] - ((long *)ptr)[-1];
if (memUsedPoolSize >= memUsedPoolMax) { /* Increase size of used pool */
memUsedPoolTmpMax = memUsedPoolMax + MEM_POOL_GROW;
/*E*/if(db9)printf("Growing used pool to %ld\n",memUsedPoolTmpMax);
if (!memUsedPoolMax) {
/* The program has just started; initialize */
memUsedPoolTmpPtr = malloc((size_t)memUsedPoolTmpMax
* sizeof(void *));
if (!memUsedPoolTmpPtr) bug(1303); /* Shouldn't have allocation problems
when program first starts */
} else {
/* Normal reallocation */
memUsedPoolTmpPtr = realloc(memUsedPool,
(size_t)memUsedPoolTmpMax * sizeof(void *));
}
if (!memUsedPoolTmpPtr) {
outOfMemory(cat("#29 (poolMalloc ", str((double)memUsedPoolTmpMax), ")", NULL));
} else {
/* Reallocation successful */
memUsedPool = memUsedPoolTmpPtr;
memUsedPoolMax = memUsedPoolTmpMax;
}
}
memUsedPool[memUsedPoolSize] = ptr;
((long *)ptr)[-3] = memUsedPoolSize;
memUsedPoolSize++;
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("c: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
return (ptr);
}
/* poolFree puts freed up space in memFreePool. */
void poolFree(void *ptr)
{
void *ptr1;
long usedLoc;
long memFreePoolTmpMax;
void *memFreePoolTmpPtr;
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("c0: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
/* First, see if the array is in memUsedPool; if so, remove it. */
usedLoc = ((long *)ptr)[-3];
if (usedLoc >= 0) { /* It is */
poolTotalFree = poolTotalFree - ((long *)ptr)[-2] + ((long *)ptr)[-1];
memUsedPoolSize--;
/* 11-Jul-2014 WL old code deleted */
/*
memUsedPool[usedLoc] = memUsedPool[memUsedPoolSize];
ptr1 = memUsedPool[usedLoc];
((long @)ptr1)[-3] = usedLoc;
/@E@/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("d: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
*/
/* 11-Jul-2014 WL new code */
if (usedLoc < memUsedPoolSize) {
memUsedPool[usedLoc] = memUsedPool[memUsedPoolSize];
ptr1 = memUsedPool[usedLoc];
((long *)ptr1)[-3] = usedLoc;
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("d: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
}
/* end of 11-Jul-2014 WL new code */
}
/* Next, add the array to the memFreePool */
/* First, allocate more memFreePool pointer space if needed */
if (memFreePoolSize >= memFreePoolMax) { /* Increase size of free pool */
memFreePoolTmpMax = memFreePoolMax + MEM_POOL_GROW;
/*E*/if(db9)printf("Growing free pool to %ld\n",memFreePoolTmpMax);
if (!memFreePoolMax) {
/* The program has just started; initialize */
memFreePoolTmpPtr = malloc((size_t)memFreePoolTmpMax
* sizeof(void *));
if (!memFreePoolTmpPtr) bug(1304); /* Shouldn't have allocation problems
when program first starts */
} else {
/* Normal reallocation */
memFreePoolTmpPtr = realloc(memFreePool,
(size_t)memFreePoolTmpMax * sizeof(void *));
}
if (!memFreePoolTmpPtr) {
/*E*/if(db9)printf("Realloc failed\n");
outOfMemory(cat("#30 (poolFree ", str((double)memFreePoolTmpMax), ")", NULL));
} else {
/* Reallocation successful */
memFreePool = memFreePoolTmpPtr;
memFreePoolMax = memFreePoolTmpMax;
}
}
/* Add the free array to the free pool */
memFreePool[memFreePoolSize] = ptr;
/* In theory, [-3] should never get referenced for an entry in the
memFreePool. However, here we make it a definite (illegal) value in
case it is referenced by code with a bug. */
((long *)ptr)[-3] = -2; /* 11-Jul-2014 WL */
memFreePoolSize++;
poolTotalFree = poolTotalFree + ((long *)ptr)[-2];
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("e: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
return;
}
/* addToUsedPool adds a (partially used) array to the memUsedPool */
void addToUsedPool(void *ptr)
{
long memUsedPoolTmpMax;
void *memUsedPoolTmpPtr;
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("d0: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
if (((long *)ptr)[-1] == ((long *)ptr)[-2]) bug(1305); /* No need to add it
when it's not partially used */
if (((long *)ptr)[-1] == ((long *)ptr)[-2]) return;
/* Allocated and actual sizes are different, so add this array to used pool */
if (memUsedPoolSize >= memUsedPoolMax) { /* Increase size of used pool */
memUsedPoolTmpMax = memUsedPoolMax + MEM_POOL_GROW;
/*E*/if(db9)printf("1Growing used pool to %ld\n",memUsedPoolTmpMax);
if (!memUsedPoolMax) {
/* The program has just started; initialize */
memUsedPoolTmpPtr = malloc((size_t)memUsedPoolTmpMax
* sizeof(void *));
if (!memUsedPoolTmpPtr) bug(1362); /* Shouldn't have allocation problems
when program first starts */
} else {
/* Normal reallocation */
memUsedPoolTmpPtr = realloc(memUsedPool, (size_t)memUsedPoolTmpMax
* sizeof(void *));
}
if (!memUsedPoolTmpPtr) {
outOfMemory("#31 (addToUsedPool)");
} else {
/* Reallocation successful */
memUsedPool = memUsedPoolTmpPtr;
memUsedPoolMax = memUsedPoolTmpMax;
}
}
memUsedPool[memUsedPoolSize] = ptr;
((long *)ptr)[-3] = memUsedPoolSize;
memUsedPoolSize++;
poolTotalFree = poolTotalFree + ((long *)ptr)[-2] - ((long *)ptr)[-1];
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("f: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
return;
}
/* Free all arrays in the free pool. */
void memFreePoolPurge(flag untilOK)
{
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("e0: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
while (memFreePoolSize) {
memFreePoolSize--;
/* Free an array */
poolTotalFree = poolTotalFree -
((long *)(memFreePool[memFreePoolSize]))[-2];
free((long *)(memFreePool[memFreePoolSize]) - 3);
if (untilOK) {
/* If pool size is OK, return. */
if (poolTotalFree <= poolAbsoluteMax) return;
}
}
/* memFreePoolSize = 0 now. */
if (memFreePoolMax != MEM_POOL_GROW) {
/* Reduce size of pool pointer array to minimum growth increment. */
if (memFreePool) free(memFreePool); /* Only when starting program */
memFreePool = malloc(MEM_POOL_GROW
* sizeof(void *)); /* Allocate starting increment */
memFreePoolMax = MEM_POOL_GROW;
}
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("g: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
return;
}
/* Get statistics for SHOW MEMORY command */
void getPoolStats(long *freeAlloc, long *usedAlloc, long *usedActual)
{
long i;
*freeAlloc = 0;
*usedAlloc = 0;
*usedActual = 0;
for (i = 0; i < memFreePoolSize; i++) {
*freeAlloc = *freeAlloc + /*12 +*/ ((long *)(memFreePool[i]))[-2];
}
for (i = 0; i < memUsedPoolSize; i++) {
*usedActual = *usedActual + 12 + ((long *)(memUsedPool[i]))[-1];
*usedAlloc = *usedAlloc + ((long *)(memUsedPool[i]))[-2] -
((long *)(memUsedPool[i]))[-1];
}
/*E*/ if (!db9)print2("poolTotalFree %ld alloc %ld\n", poolTotalFree, *freeAlloc +
/*E*/ *usedAlloc);
}
void initBigArrays(void)
{
/*??? This should all become obsolete. */
g_Statement = malloc((size_t)g_MAX_STATEMENTS * sizeof(struct statement_struct));
/*E*//*db=db+g_MAX_STATEMENTS * sizeof(struct statement_struct);*/
if (!g_Statement) {
print2("*** FATAL *** Could not allocate g_Statement space\n");
bug(1363);
}
g_MathToken = malloc((size_t)g_MAX_MATHTOKENS * sizeof(struct mathToken_struct));
/*E*//*db=db+g_MAX_MATHTOKENS * sizeof(struct mathToken_struct);*/
if (!g_MathToken) {
print2("*** FATAL *** Could not allocate g_MathToken space\n");
bug(1364);
}
g_IncludeCall = malloc((size_t)g_MAX_INCLUDECALLS * sizeof(struct includeCall_struct));
/*E*//*db=db+g_MAX_INCLUDECALLS * sizeof(struct includeCall_struct);*/
if (!g_IncludeCall) {
print2("*** FATAL *** Could not allocate g_IncludeCall space\n");
bug(1365);
}
}
/* Find the number of free memory bytes */
long getFreeSpace(long max)
{
long i , j, k;
char *s;
i = 0;
j = max + 2;
while (i < j - 2) {
k = (i + j) / 2;
s = malloc((size_t)k);
if (s) {
free(s);
i = k;
} else {
j = k;
}
}
return (i);
}
/* Fatal memory allocation error */
void outOfMemory(vstring msg)
{
vstring tmpStr = "";
print2("*** FATAL ERROR: Out of memory.\n");
print2("Internal identifier (for technical support): %s\n",msg);
print2(
"To solve this problem, remove some unnecessary statements or file\n");
print2(
"inclusions to reduce the size of your input source.\n");
print2(
"Monitor memory periodically with SHOW MEMORY.\n");
#ifdef THINK_C
print2(
"You may also increase the \"Application Memory Size\" under \"Get Info\"\n");
print2(
"under \"File\" in the Finder after clicking once on the Metamath\n");
print2("application icon.\n");
#endif
print2("\n");
print2("Press <return> to exit Metamath.\n");
tmpStr = cmdInput1("");
/* let(&tmpStr, ""); */
let(&tmpStr, left(tmpStr, 0)); /* Prevent "not used" compiler warning */
/* Close the log to make sure error log is saved */
if (g_logFileOpenFlag) {
fclose(g_logFilePtr);
g_logFileOpenFlag = 0;
}
exit(1);
}
/* 17-Nov-2015 nm Added abort, skip, ignore options */
/* Bug check */
void bug(int bugNum)
{
vstring tmpStr = "";
flag oldMode;
long wrongAnswerCount = 0;
static flag mode = 0; /* 1 = run to next bug, 2 = continue and ignore bugs */
/* 10/10/02 */
flag saveOutputToString = g_outputToString;
g_outputToString = 0; /* Make sure we print to screen and not to string */
if (mode == 2) {
/* If user chose to ignore bugs, print brief info and return */
print2("?BUG CHECK: *** DETECTED BUG %ld, IGNORING IT...\n", (long)bugNum);
return;
}
print2("?BUG CHECK: *** DETECTED BUG %ld\n", (long)bugNum);
if (mode == 0) { /* Print detailed info for first bug */
print2("\n");
print2(
"To get technical support, please send Norm Megill (%salum.mit.edu) the\n",
"nm@");
print2(
"detailed command sequence or a command file that reproduces this bug,\n");
print2(
"along with the source file that was used. See HELP LOG for help on\n");
print2(
"recording a session. See HELP SUBMIT for help on command files. Search\n");
print2(
"for \"bug(%ld)\" in the m*.c source code to find its origin.\n", bugNum);
/* 15-Oct-2019 nm Added the next 2 info lines */
print2(
"If earlier errors were reported, try fixing them first, because they\n");
print2(
"may occasionally lead to false bug detection\n");
print2("\n");
}
let(&tmpStr, "?");
while (strcmp(tmpStr, "A") && strcmp(tmpStr, "a")
&& strcmp(tmpStr, "S") && strcmp(tmpStr, "s")
&& strcmp(tmpStr, "I") && strcmp(tmpStr, "i")
/* The above is actually useless because of break below, but we'll leave
it in case we want to re-ask after wrong answers in the future */
) {
if (wrongAnswerCount > 6) {
print2(
"Too many wrong answers; program will be aborted to exit scripting loops.\n");
break; /* Added 8-Nov-03 */
}
if (wrongAnswerCount > 0) {
let(&tmpStr, "");
tmpStr = cmdInput1("Please answer I, S, or A: ");
} else {
print2(
"Press S <return> to step to next bug, I <return> to ignore further bugs,\n");
let(&tmpStr, "");
tmpStr = cmdInput1("or A <return> to abort program: ");
}
/******* 8-Nov-03 This loop caused an infinite loop in a cron job when bug
detection was triggered. Now, when the loop breaks above,
the program will abort. *******/
wrongAnswerCount++;
}
oldMode = mode;
mode = 0;
if (!strcmp(tmpStr, "S") || !strcmp(tmpStr, "s")) mode = 1; /* Skip to next bug */
if (!strcmp(tmpStr, "I") || !strcmp(tmpStr, "i")) mode = 2; /* Ignore bugs */
if (oldMode == 0 && mode > 0) {
/* Print dire warning after the first bug only */
print2("\n");
print2(
"Warning!!! A bug was detected, but you are continuing anyway.\n");
print2(
"The program may be corrupted, so you are proceeding at your own risk.\n");
print2("\n");
let(&tmpStr, "");
}
if (mode > 0) {
/* 10/10/02 */
g_outputToString = saveOutputToString; /* Restore for continuation */
return;
}
let(&tmpStr, "");
#ifdef THINK_C
cmdInput1("Program has crashed. Press <return> to leave.");
#endif
print2("\n");
/* Close the log to make sure error log is saved */
if (g_logFileOpenFlag) {
print2("The log file \"%s\" was closed %s %s.\n", g_logFileName,
date(), time_());
fclose(g_logFilePtr);
g_logFileOpenFlag = 0;
}
print2("The program was aborted.\n");
exit(1); /* Use 1 instead of 0 to flag abnormal termination to scripts */
}
#define M_MAX_ALLOC_STACK 100
/* 26-Apr-2008 nm Added */
/* This function returns a 1 if any entry in a comma-separated list
matches using the matches() function. */
flag matchesList(vstring testString, vstring pattern, char wildCard,
char oneCharWildCard) {
long entries, i;
flag matchVal = 0;
vstring entryPattern = "";
/* Done so we can use string functions like left() in call arguments */
long saveTempAllocStack;
saveTempAllocStack = g_startTempAllocStack; /* For let() stack cleanup */
g_startTempAllocStack = g_tempAllocStackTop;
entries = numEntries(pattern);
for (i = 1; i <= entries; i++) {
let(&entryPattern, entry(i, pattern)); /* If we didn't modify
g_startTempAllocStack above, this let() would corrupt string
functions in the matchesList() call arguments */
matchVal = matches(testString, entryPattern, wildCard, oneCharWildCard);
if (matchVal) break;
}
let(&entryPattern, ""); /* Deallocate */ /* 3-Jul-2011 nm Added to fix
memory leak */
g_startTempAllocStack = saveTempAllocStack;
return (matchVal);
}
/* This function returns a 1 if the first argument matches the pattern of
the second argument. The second argument may have wildcard characters.
wildCard matches 0 or more characters; oneCharWildCard matches any
single character. */
/* 30-Jan-06 nm Added single-character-match wildcard argument */
/* 19-Apr-2015 so, nm - Added "=" to match statement being proved */
/* 19-Apr-2015 so, nm - Added "%" to match changed proofs */
/* 8-Mar-2016 nm Added "#1234" to match internal statement number */
/* 18-Jul-2020 nm Added "@1234" to match web statement number */
flag matches(vstring testString, vstring pattern, char wildCard,
char oneCharWildCard) {
long i, ppos, pctr, tpos, s1, s2, s3;
vstring tmpStr = "";
/* 21-Nov-2014 Stefan O'Rear - added label ranges - see HELP SEARCH */
if (wildCard == '*') {
/* Checking for wildCard = * meaning this is only for labels, not
math tokens */
/* The following special chars are handled in this block:
"~" Statement range
"=" Most recent PROVE command statement
"%" List of modified statements
"#" Internal statement number
"@" Web page statement number */
i = instr(1, pattern, "~");
if (i != 0) {
if (i == 1) {
s1 = 1; /* empty string before "~" */
} else {
s1 = lookupLabel(left(pattern, i - 1));
}
s2 = lookupLabel(testString);
if (i == (long)strlen(pattern)) {
s3 = g_statements; /* empty string after "~" */
} else {
s3 = lookupLabel(right(pattern, i + 1));
}
let(&tmpStr, ""); /* Clean up temporary allocations of left and right */
return ((s1 >= 1 && s2 >= 1 && s3 >= 1 && s1 <= s2 && s2 <= s3)
? 1 : 0);
}
/* 8-Mar-2016 nm Added "#12345" to match internal statement number */
if (pattern[0] == '#') {
s1 = (long)val(right(pattern, 2));
if (s1 < 1 || s1 > g_statements)
return 0; /* # arg is out of range */
if (!strcmp(g_Statement[s1].labelName, testString)) {
return 1;
} else {
return 0;
}
}
/* 18-Jul-2020 nm Added "@12345" to match web statement number */
if (pattern[0] == '@') {
s1 = lookupLabel(testString);
if (s1 < 1) return 0;
s2 = (long)val(right(pattern, 2));
if (g_Statement[s1].pinkNumber == s2) {
return 1;
} else {
return 0;
}
}
/* 19-Apr-2015 so, nm - Added "=" to match statement being proved */
if (!strcmp(pattern,"=")) {
s1 = lookupLabel(testString);
/*return (PFASmode && g_proveStatement == s1);*/
/* 18-Jul-2020 nm */
/* We might as well use g_proveStatement outside of MM-PA, so =
can be argument to PROVE command */
return (g_proveStatement == s1);
}
/* 19-Apr-2015 so, nm - Added "%" to match changed proofs */
if (!strcmp(pattern,"%")) {
s1 = lookupLabel(testString); /* Returns -1 if not found or (not
$a and not $p) */
if (s1 > 0) { /* It's a $a or $p statement */
/* (If it's not $p, we don't want to peek at proofSectionPtr[-1]
to prevent bad pointer. */
if (g_Statement[s1].type == (char)p_) { /* $p so it has a proof */
/*
/@ ASCII 1 is flag that proof is not from original source file @/
if (g_Statement[s1].proofSectionPtr[-1] == 1) {
*/
/* 3-May-2017 nm */
/* The proof is not from the original source file */
if (g_Statement[s1].proofSectionChanged == 1) {
return 1;
}
}
}
return 0;
/*
return nmbrElementIn(1, changedStmtNmbr, s1);
*/
} /* if (!strcmp(pattern,"%")) */
} /* if (wildCard == '*') */
/* Get to first wild card character */
ppos = 0;
/*if (wildCard!='*') printf("'%s' vs. '%s'\n", pattern, testString);*/
while ((pattern[ppos] == testString[ppos] ||
(pattern[ppos] == oneCharWildCard && testString[ppos] != 0))
&& pattern[ppos] != 0) ppos++;
if (pattern[ppos] == 0) {
if (testString[ppos] != 0) {
return (0); /* No wildcards; mismatched */
} else {
return (1); /* No wildcards; matched */
}
}
if (pattern[ppos] != wildCard) {
return (0); /* Mismatched */
}
tpos = ppos;
/* Scan remainder of pattern */
pctr = 0;
i = 0;
while (1) {
if (pattern[ppos + 1 + i] == wildCard) { /* Next wildcard found */
tpos = tpos + pctr + i;
ppos = ppos + 1 + i;
i = 0;
pctr = 0;
continue;
}
if (pattern[ppos + 1 + i] != testString[tpos + pctr + i]
&& (pattern[ppos + 1 + i] != oneCharWildCard
|| testString[tpos + pctr + i] == 0)) {
if (testString[tpos + pctr + i] == 0) {
return (0);
}
pctr++;
i = 0;
continue;
}
if (pattern[ppos + 1 + i] == 0) {
return(1); /* Matched */
}
i++;
}
bug(1375);
return (0); /* Dummy return - never used */
}
/*******************************************************************/
/*********** Number string functions *******************************/
/*******************************************************************/
long g_nmbrTempAllocStackTop = 0; /* Top of stack for nmbrTempAlloc functon */
long g_nmbrStartTempAllocStack = 0; /* Where to start freeing temporary allocation
when nmbrLet() is called (normally 0, except in
special nested vstring functions) */
nmbrString *nmbrTempAllocStack[M_MAX_ALLOC_STACK];
nmbrString *nmbrTempAlloc(long size)
/* nmbrString memory allocation/deallocation */
{
/* When "size" is >0, "size" instances of nmbrString are allocated. */
/* When "size" is 0, all memory previously allocated with this */
/* function is deallocated, down to g_nmbrStartTempAllocStack. */
/* int i; */ /* 11-Jul-2014 WL old code deleted */
if (size) {
if (g_nmbrTempAllocStackTop>=(M_MAX_ALLOC_STACK-1)) {
/*??? Fix to allocate more */
outOfMemory("#105 (nmbrString stack array)");
}
if (!(nmbrTempAllocStack[g_nmbrTempAllocStackTop++]=poolMalloc(size
*(long)(sizeof(nmbrString)))))
/* outOfMemory("#106 (nmbrString stack)"); */ /*???Unnec. w/ poolMalloc*/
/*E*/db2=db2+size*(long)(sizeof(nmbrString));
return (nmbrTempAllocStack[g_nmbrTempAllocStackTop-1]);
} else {
/* 11-Jul-2014 WL old code deleted */
/*
for (i=g_nmbrStartTempAllocStack; i < g_nmbrTempAllocStackTop; i++) {
/@E@/db2=db2-(nmbrLen(nmbrTempAllocStack[i])+1)*(long)(sizeof(nmbrString));
poolFree(nmbrTempAllocStack[i]);
}
*/
/* 11-Jul-2014 WL new code */
while(g_nmbrTempAllocStackTop != g_nmbrStartTempAllocStack) {
/*E*/db2=db2-(nmbrLen(nmbrTempAllocStack[g_nmbrTempAllocStackTop-1])+1)
/*E*/ *(long)(sizeof(nmbrString));
poolFree(nmbrTempAllocStack[--g_nmbrTempAllocStackTop]);
}
/* end of 11-Jul-2014 WL new code */
g_nmbrTempAllocStackTop=g_nmbrStartTempAllocStack;
return (0);
}
}
/* Make string have temporary allocation to be released by next nmbrLet() */
/* Warning: after nmbrMakeTempAlloc() is called, the nmbrString may NOT be
assigned again with nmbrLet() */
void nmbrMakeTempAlloc(nmbrString *s)
{
if (g_nmbrTempAllocStackTop>=(M_MAX_ALLOC_STACK-1)) {
printf(
"*** FATAL ERROR *** Temporary nmbrString stack overflow in nmbrMakeTempAlloc()\n");
#if __STDC__
fflush(stdout);
#endif
bug(1368);
}
if (s[0] != -1) { /* End of string */
/* Do it only if nmbrString is not empty */
nmbrTempAllocStack[g_nmbrTempAllocStackTop++] = s;
}
/*E*/db2=db2+(nmbrLen(s)+1)*(long)(sizeof(nmbrString));
/*E*/db3=db3-(nmbrLen(s)+1)*(long)(sizeof(nmbrString));
}
void nmbrLet(nmbrString **target,nmbrString *source)
/* nmbrString assignment */
/* This function must ALWAYS be called to make assignment to */
/* a nmbrString in order for the memory cleanup routines, etc. */
/* to work properly. If a nmbrString has never been assigned before, */
/* it is the user's responsibility to initialize it to NULL_NMBRSTRING (the */
/* null string). */
{
long targetLength,sourceLength;
long targetAllocLen;
long poolDiff;
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
sourceLength=nmbrLen(source); /* Save its actual length */
targetLength=nmbrLen(*target); /* Save its actual length */
targetAllocLen=nmbrAllocLen(*target); /* Save target's allocated length */
/*E*/if (targetLength) {
/*E*/ /* printf("Deleting %s\n",cvtMToVString(*target,0)); */
/*E*/ db3 = db3 - (targetLength+1)*(long)(sizeof(nmbrString));
/*E*/}
/*E*/if (sourceLength) {
/*E*/ /* printf("Adding %s\n",cvtMToVString(source,0)); */
/*E*/ db3 = db3 + (sourceLength+1)*(long)(sizeof(nmbrString));
/*E*/}
if (targetAllocLen) {
if (sourceLength) { /* source and target are both nonzero length */
if (targetAllocLen >= sourceLength) { /* Old string has room for new one */
nmbrCpy(*target,source); /* Re-use the old space to save CPU time */
/* Memory pool handling */
/* Assign actual size of target string */
poolDiff = ((long *)(*target))[-1] - ((long *)source)[-1];
((long *)(*target))[-1] = ((long *)source)[-1];
/* If actual size of target string is less than allocated size, we
may have to add it to the used pool */
if (((long *)(*target))[-1] != ((long *)(*target))[-2]) {
if (((long *)(*target))[-1] > ((long *)(*target))[-2]) bug(1325);
if (((long *)(*target))[-3] == -1) {
/* It's not already in the used pool, so add it */
addToUsedPool(*target);
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0aa: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
} else {
/* Adjust free space independently */
poolTotalFree = poolTotalFree + poolDiff;
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0ab: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
}
} else {
if (((long *)(*target))[-3] != -1) {
/* It's in the pool (but all allocated space coincidentally used) */
/* Adjust free space independently */
poolTotalFree = poolTotalFree + poolDiff;
}
}
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0a: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
} else {
/* Free old string space and allocate new space */
poolFree(*target); /* Free old space */
/* *target=poolMalloc((sourceLength + 1) * sizeof(nmbrString)); */
*target=poolMalloc((sourceLength + 1) * (long)(sizeof(nmbrString)) * 2);
/* Allocate new space --
We are replacing a smaller string with a larger one;
assume it is growing, and allocate twice as much as
needed. */
/*if (!*target) outOfMemory("#107 (nmbrString)");*/ /*???Unnec. w/ poolMalloc*/
nmbrCpy(*target,source);
/* Memory pool handling */
/* Assign actual size of target string */
poolDiff = ((long *)(*target))[-1] - ((long *)source)[-1];
((long *)(*target))[-1] = ((long *)source)[-1];
/* If actual size of target string is less than allocated size, we
may have to add it to the used pool */
/* (The 1st 'if' is redundant with target doubling above) */
if (((long *)(*target))[-1] != ((long *)(*target))[-2]) {
if (((long *)(*target))[-1] > ((long *)(*target))[-2]) bug(1326);
if (((long *)(*target))[-3] == -1) {
/* It's not already in the used pool, so add it */
addToUsedPool(*target);
} else {
/* Adjust free space independently */
poolTotalFree = poolTotalFree + poolDiff;
}
} else {
if (((long *)(*target))[-3] != -1) {
/* It's in the pool (but all allocated space coincidentally used) */
/* Adjust free space independently */
poolTotalFree = poolTotalFree + poolDiff;
}
}
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0b: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
}
} else { /* source is 0 length, target is not */
poolFree(*target);
*target= NULL_NMBRSTRING;
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0c: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
}
} else {
if (sourceLength) { /* target is 0 length, source is not */
*target=poolMalloc((sourceLength + 1) * (long)(sizeof(nmbrString)));
/* Allocate new space */
/* if (!*target) outOfMemory("#108 (nmbrString)"); */ /*???Unnec. w/ poolMalloc*/
nmbrCpy(*target,source);
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0d: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
} else { /* source and target are both 0 length */
/* *target= NULL_NMBRSTRING; */ /* Redundant */
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0e: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
}
}
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k1: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
nmbrTempAlloc(0); /* Free up temporary strings used in expression computation*/
}
nmbrString *nmbrCat(nmbrString *string1,...) /* String concatenation */
#define M_MAX_CAT_ARGS 30
{
va_list ap; /* Declare list incrementer */
nmbrString *arg[M_MAX_CAT_ARGS]; /* Array to store arguments */
long argLength[M_MAX_CAT_ARGS]; /* Array to store argument lengths */
int numArgs=1; /* Define "last argument" */
int i;
long j;
nmbrString *ptr;
arg[0]=string1; /* First argument */
va_start(ap,string1); /* Begin the session */
while ((arg[numArgs++]=va_arg(ap,nmbrString *)))
/* User-provided argument list must terminate with NULL */
if (numArgs>=M_MAX_CAT_ARGS-1) {
printf("*** FATAL ERROR *** Too many cat() arguments\n");
#if __STDC__
fflush(stdout);
#endif
bug(1369);
}
va_end(ap); /* End var args session */
numArgs--; /* The last argument (0) is not a string */
/* Find out the total string length needed */
j = 0;
for (i = 0; i < numArgs; i++) {
argLength[i]=nmbrLen(arg[i]);
j=j+argLength[i];
}
/* Allocate the memory for it */
ptr=nmbrTempAlloc(j+1);
/* Move the strings into the newly allocated area */
j = 0;
for (i = 0; i < numArgs; i++) {
nmbrCpy(ptr+j,arg[i]);
j=j+argLength[i];
}
return (ptr);
}
/* Find out the length of a nmbrString */
long nmbrLen(nmbrString *s)
{
/* Assume it's been allocated with poolMalloc. */
return (((long)(((long *)s)[-1] - (long)(sizeof(nmbrString))))
/ (long)(sizeof(nmbrString)));
}
/* Find out the allocated length of a nmbrString */
long nmbrAllocLen(nmbrString *s)
{
/* Assume it's been allocated with poolMalloc. */
return (((long)(((long *)s)[-2] - (long)(sizeof(nmbrString))))
/ (long)(sizeof(nmbrString)));
}
/* Set the actual size field in a nmbrString allocated with poolFixedMalloc() */
/* Use this if "zapping" a nmbrString element with -1 to reduce its length. */
/* Note that the nmbrString will not be moved to the "used pool", even if
zapping its length results in free space; thus the free space will never
get recovered unless done by the caller or poolFree is called. (This is
done on purpose so the caller can know what free space is left.) */
/* ???Note that nmbrZapLen's not moving string to used pool wastes potential
space when called by the routines in this module. Effect should be minor. */
void nmbrZapLen(nmbrString *s, long length) {
if (((long *)s)[-3] != -1) {
/* It's already in the used pool, so adjust free space tally */
poolTotalFree = poolTotalFree + ((long *)s)[-1]
- (length + 1) * (long)(sizeof(nmbrString));
}
((long *)s)[-1] = (length + 1) * (long)(sizeof(nmbrString));
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("l: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
}
/* Copy a string to another (pre-allocated) string */
/* Dangerous for general purpose use */
void nmbrCpy(nmbrString *s,nmbrString *t)
{
long i;
i = 0;
while (t[i] != -1) { /* End of string -- nmbrRight depends on it!! */
s[i] = t[i];
i++;
}
s[i] = t[i]; /* End of string */
}
/* Copy a string to another (pre-allocated) string */
/* Like strncpy, only the 1st n characters are copied. */
/* Dangerous for general purpose use */
void nmbrNCpy(nmbrString *s,nmbrString *t,long n)
{
long i;
i = 0;
while (t[i] != -1) { /* End of string -- nmbrSeg, nmbrMid depend on it!! */
if (i >= n) break;
s[i] = t[i];
i++;
}
s[i] = t[i]; /* End of string */
}
/* Compare two strings */
/* Unlike strcmp, this returns a 1 if the strings are equal
and 0 otherwise. */
/* Only the token is compared. The whiteSpace string is
ignored. */
int nmbrEq(nmbrString *s,nmbrString *t)
{
long i;
if (nmbrLen(s) != nmbrLen(t)) return 0; /* Speedup */
for (i = 0; s[i] == t[i]; i++)
if (s[i] == -1) /* End of string */
return 1;
return 0;
}
/* Extract sin from character position start to stop into sout */
nmbrString *nmbrSeg(nmbrString *sin, long start, long stop)
{
nmbrString *sout;
long length;
if (start < 1) start = 1;
if (stop < 1) stop = 0;
length=stop - start + 1;
if (length < 0) length = 0;
sout = nmbrTempAlloc(length + 1);
nmbrNCpy(sout, sin + start - 1, length);
sout[length] = *NULL_NMBRSTRING;
return (sout);
}
/* Extract sin from character position start for length len */
nmbrString *nmbrMid(nmbrString *sin, long start, long length)
{
nmbrString *sout;
if (start < 1) start = 1;
if (length < 0) length = 0;
sout = nmbrTempAlloc(length + 1);
nmbrNCpy(sout, sin + start - 1, length);
sout[length] = *NULL_NMBRSTRING;
return (sout);
}
/* Extract leftmost n characters */
nmbrString *nmbrLeft(nmbrString *sin,long n)
{
nmbrString *sout;
if (n < 0) n = 0;
sout=nmbrTempAlloc(n + 1);
nmbrNCpy(sout, sin, n);
sout[n] = *NULL_NMBRSTRING;
return (sout);
}
/* Extract after character n */
nmbrString *nmbrRight(nmbrString *sin,long n)
{
/*??? We could just return &sin[n-1], but this is safer for debugging. */
nmbrString *sout;
long i;
if (n < 1) n = 1;
i = nmbrLen(sin);
if (n > i) return (NULL_NMBRSTRING);
sout = nmbrTempAlloc(i - n + 2);
nmbrCpy(sout, &sin[n - 1]);
return (sout);
}
/* Allocate and return an "empty" string n "characters" long */
nmbrString *nmbrSpace(long n)
{
nmbrString *sout;
long j = 0;
if (n < 0) bug(1327);
sout = nmbrTempAlloc(n + 1);
while (j < n) {
/* Initialize all fields */
sout[j] = 0;
j++;
}
sout[j] = *NULL_NMBRSTRING; /* End of string */
return (sout);
}
/* Search for string2 in string1 starting at start_position */
long nmbrInstr(long start_position,nmbrString *string1,
nmbrString *string2)
{
long ls1, ls2, i, j;
if (start_position < 1) start_position = 1;
ls1 = nmbrLen(string1);
ls2 = nmbrLen(string2);
for (i = start_position - 1; i <= ls1 - ls2; i++) {
for (j = 0; j < ls2; j++) {
if (string1[i+j] != string2[j])
break;
}
if (j == ls2) return (i+1);
}
return (0);
}
/* Search for string2 in string 1 in reverse starting at start_position */
/* (Reverse nmbrInstr) */
/* Warning: This has 'let' inside of it and is not safe for use inside
of 'let' statements. (To make it safe, it must be rewritten to expand
the 'mid' and remove the 'let'.) */
long nmbrRevInstr(long start_position,nmbrString *string1,
nmbrString *string2)
{
long ls1, ls2;
nmbrString *tmp = NULL_NMBRSTRING;
ls1 = nmbrLen(string1);
ls2 = nmbrLen(string2);
if (start_position > ls1 - ls2 + 1) start_position = ls1 - ls2 + 2;
if (start_position<1) return 0;
while (!nmbrEq(string2, nmbrMid(string1, start_position, ls2))) {
start_position--;
nmbrLet(&tmp, NULL_NMBRSTRING);
/* Clear nmbrString buffer to prevent overflow caused by "mid" */
if (start_position < 1) return 0;
}
return (start_position);
}
/* Converts nmbrString to a vstring with one space between tokens */
vstring nmbrCvtMToVString(nmbrString *s)
{
long i, j, outputLen, mstrLen;
vstring tmpStr = "";
vstring ptr;
vstring ptr2;
long saveTempAllocStack;
saveTempAllocStack = g_startTempAllocStack; /* For let() stack cleanup */
g_startTempAllocStack = g_tempAllocStackTop;
mstrLen = nmbrLen(s);
/* Precalculate output length */
outputLen = -1;
for (i = 0; i < mstrLen; i++) {
outputLen = outputLen + (long)strlen(g_MathToken[s[i]].tokenName) + 1;
}
let(&tmpStr, space(outputLen)); /* Preallocate output string */
/* Assign output string */
ptr = tmpStr;
for (i = 0; i < mstrLen; i++) {
ptr2 = g_MathToken[s[i]].tokenName;
j = (long)strlen(ptr2);
memcpy(ptr, ptr2, (size_t)j);
ptr = ptr + j + 1;
}
g_startTempAllocStack = saveTempAllocStack;
if (tmpStr[0]) makeTempAlloc(tmpStr); /* Flag it for deallocation */
return (tmpStr);
}
/* Converts proof to a vstring with one space between tokens */
/* 11-Sep-2016 nm Allow it to tolerate garbage entries for debugging */
vstring nmbrCvtRToVString(nmbrString *proof,
/* 25-Jan-2016 */
flag explicitTargets, /* 1 = "target=source" for /EXPLICIT proof format */
long statemNum) /* used only if explicitTargets=1 */
{
long i, j, plen, maxLabelLen, maxLocalLen, step, stmt;
long maxTargetLabelLen; /* 25-Jan-2016 nm */
vstring proofStr = "";
vstring tmpStr = "";
vstring ptr;
nmbrString *localLabels = NULL_NMBRSTRING;
nmbrString *localLabelNames = NULL_NMBRSTRING;
long nextLocLabNum = 1; /* Next number to be used for a local label */
void *voidPtr; /* bsearch result */
/* 26-Jan-2016 nm */
nmbrString *targetHyps = NULL_NMBRSTRING; /* Targets for /EXPLICIT format */
long saveTempAllocStack;
long nmbrSaveTempAllocStack;
saveTempAllocStack = g_startTempAllocStack; /* For let() stack cleanup */
g_startTempAllocStack = g_tempAllocStackTop;
nmbrSaveTempAllocStack = g_nmbrStartTempAllocStack;
/* For nmbrLet() stack cleanup*/
g_nmbrStartTempAllocStack = g_nmbrTempAllocStackTop;
plen = nmbrLen(proof);
/* 25-Jan-2016 nm */
if (explicitTargets == 1) {
/* Get the list of targets for /EXPLICIT format */
if (statemNum <= 0) bug(1388);
nmbrLet(&targetHyps, nmbrGetTargetHyp(proof, statemNum));
}
/* Find longest local label name */
maxLocalLen = 0;
i = plen;
while (i) {
i = i / 10;
maxLocalLen++;
}
/* Collect local labels */
/* Also, find longest statement label name */
maxLabelLen = 0;
maxTargetLabelLen = 0; /* 25-Jan-2016 nm */
for (step = 0; step < plen; step++) {
stmt = proof[step];
if (stmt <= -1000) {
stmt = -1000 - stmt;
if (!nmbrElementIn(1, localLabels, stmt)) {
nmbrLet(&localLabels, nmbrAddElement(localLabels, stmt));
}
} else {
/* 11-Sep-2016 nm */
if (stmt < 1 || stmt > g_statements) {
maxLabelLen = 100; /* For safety */
maxTargetLabelLen = 100; /* For safety */
continue; /* Ignore bad entry */
}
if (stmt > 0) {
if ((signed)(strlen(g_Statement[stmt].labelName)) > maxLabelLen) {
maxLabelLen = (long)strlen(g_Statement[stmt].labelName);
}
}
}
/* 25-Jan-2016 nm */
if (explicitTargets == 1) {
/* Also consider longest target label name */
stmt = targetHyps[step];
if (stmt <= 0) bug(1390);
if ((signed)(strlen(g_Statement[stmt].labelName)) > maxTargetLabelLen) {
maxTargetLabelLen = (long)strlen(g_Statement[stmt].labelName);
}
}
} /* next step */
/* localLabelNames[] holds an integer which, when converted to string,
is the local label name. */
nmbrLet(&localLabelNames, nmbrSpace(plen));
/* Build the ASCII string */
/* Preallocate the string for speed (the "2" accounts for a space and a
colon). */
let(&proofStr, space(plen * (2 + maxLabelLen
+ ((explicitTargets == 1) ? maxTargetLabelLen + 1 : 0) /* 25-Jan-2016 */
/* The "1" accounts for equal sign */
+ maxLocalLen)));
ptr = proofStr;
for (step = 0; step < plen; step++) {
stmt = proof[step];
if (stmt < 0) {
if (stmt <= -1000) {
stmt = -1000 - stmt;
/* Change stmt to the step number a local label refers to */
let(&tmpStr, cat(
/* 25-Jan-2016 nm */
((explicitTargets == 1) ? g_Statement[targetHyps[step]].labelName : ""),
((explicitTargets == 1) ? "=" : ""),
str((double)(localLabelNames[stmt])), " ", NULL));
/* 11-Sep-2016 nm */
} else if (stmt != -(long)'?') {
let(&tmpStr, cat("??", str((double)stmt), " ", NULL)); /* For safety */
} else {
if (stmt != -(long)'?') bug(1391); /* Must be an unknown step */
let(&tmpStr, cat(
/* 25-Jan-2016 nm */
((explicitTargets == 1) ? g_Statement[targetHyps[step]].labelName : ""),
((explicitTargets == 1) ? "=" : ""),
chr(-stmt), " ", NULL));
}
/* 11-Sep-2016 nm */
} else if (stmt < 1 || stmt > g_statements) {
let(&tmpStr, cat("??", str((double)stmt), " ", NULL)); /* For safety */
} else {
let(&tmpStr,"");
if (nmbrElementIn(1, localLabels, step)) {
/* This statement declares a local label */
/* First, get a name for the local label, using the next integer that
does not match any integer used for a statement label. */
let(&tmpStr, str((double)nextLocLabNum));
while (1) {
voidPtr = (void *)bsearch(tmpStr,
g_allLabelKeyBase, (size_t)g_numAllLabelKeys,
sizeof(long), labelSrchCmp);
if (!voidPtr) break; /* It does not conflict */
nextLocLabNum++; /* Try the next one */
let(&tmpStr, str((double)nextLocLabNum));
}
localLabelNames[step] = nextLocLabNum;
let(&tmpStr, cat(tmpStr, ":", NULL));
nextLocLabNum++; /* Prepare for next local label */
}
let(&tmpStr, cat(tmpStr,
/* 25-Jan-2016 nm */
((explicitTargets == 1) ? g_Statement[targetHyps[step]].labelName : ""),
((explicitTargets == 1) ? "=" : ""),
g_Statement[stmt].labelName, " ", NULL));
}
j = (long)strlen(tmpStr);
memcpy(ptr, tmpStr, (size_t)j);
ptr = ptr + j;
} /* Next step */
if (ptr - proofStr) {
/* Deallocate large pool and trim trailing space */
let(&proofStr, left(proofStr, ptr - proofStr - 1));
} else {
let(&proofStr, "");
}
let(&tmpStr, "");
nmbrLet(&localLabels, NULL_NMBRSTRING);
nmbrLet(&localLabelNames, NULL_NMBRSTRING);
g_startTempAllocStack = saveTempAllocStack;
g_nmbrStartTempAllocStack = nmbrSaveTempAllocStack;
if (proofStr[0]) makeTempAlloc(proofStr); /* Flag it for deallocation */
return (proofStr);
}
nmbrString *nmbrGetProofStepNumbs(nmbrString *reason)
{
/* This function returns a nmbrString of length of reason with
step numbers assigned to tokens which are steps, and 0 otherwise.
The returned string is allocated; THE CALLER MUST DEALLOCATE IT. */
nmbrString *stepNumbs = NULL_NMBRSTRING;
long rlen, start, end, i, step;
rlen = nmbrLen(reason);
nmbrLet(&stepNumbs,nmbrSpace(rlen)); /* All stepNumbs[] are initialized
to 0 by nmbrSpace() */
if (!rlen) return (stepNumbs);
if (reason[1] == -(long)'=') {
/* The proof is in "internal" format, with "g_proveStatement = (...)" added */
start = 2; /* 2, not 3, so empty proof '?' will be seen */
if (rlen == 3) {
end = rlen; /* Empty proof case */
} else {
end = rlen - 1; /* Trim off trailing ')' */
}
} else {
start = 1;
end = rlen;
}
step = 0;
for (i = start; i < end; i++) {
if (i == 0) {
/* i = 0 must be handled separately to prevent a reference to
a field outside of the nmbrString */
step++;
stepNumbs[0] = step;
continue;
}
if (reason[i] < 0 && reason[i] != -(long)'?') continue;
if (reason[i - 1] == -(long)'('
|| reason[i - 1] == -(long)'{'
|| reason[i - 1] == -(long)'=') {
step++;
stepNumbs[i] = step;
}
}
return (stepNumbs);
}
/* Converts any nmbrString to an ASCII string of numbers
-- used for debugging only. */
vstring nmbrCvtAnyToVString(nmbrString *s)
{
long i;
vstring tmpStr = "";
long saveTempAllocStack;
saveTempAllocStack = g_startTempAllocStack; /* For let() stack cleanup */
g_startTempAllocStack = g_tempAllocStackTop;
for (i = 1; i <= nmbrLen(s); i++) {
let(&tmpStr,cat(tmpStr," ", str((double)(s[i-1])),NULL));
}
g_startTempAllocStack = saveTempAllocStack;
if (tmpStr[0]) makeTempAlloc(tmpStr); /* Flag it for deallocation */
return (tmpStr);
}
/* Extract variables from a math token string */
nmbrString *nmbrExtractVars(nmbrString *m)
{
long i, j, length;
nmbrString *v;
length = nmbrLen(m);
v=nmbrTempAlloc(length + 1); /* Pre-allocate maximum possible space */
v[0] = *NULL_NMBRSTRING;
j = 0; /* Length of output string */
for (i = 0; i < length; i++) {
/*if (m[i] < 0 || m[i] >= g_mathTokens) {*/
/* Changed >= to > because tokenNum=g_mathTokens is used by mmveri.c for
dummy token */
if (m[i] < 0 || m[i] > g_mathTokens) bug(1328);
if (g_MathToken[m[i]].tokenType == (char)var_) {
if (!nmbrElementIn(1, v, m[i])) { /* Don't duplicate variable */
v[j] = m[i];
j++;
v[j] = *NULL_NMBRSTRING; /* Add temp. end-of-string for getElementOf() */
}
}
}
nmbrZapLen(v, j); /* Zap mem pool fields */
/*E*/db2=db2-(length-nmbrLen(v))*(long)(sizeof(nmbrString));
return v;
}
/* Determine if an element (after start) is in a nmbrString; return position
if it is. Like nmbrInstr(), but faster. Warning: start must NOT
be greater than length, otherwise results are unpredictable!! This
is not checked in order to speed up search. */
long nmbrElementIn(long start, nmbrString *g, long element)
{
long i = start - 1;
while (g[i] != -1) { /* End of string */
if (g[i] == element) return(i + 1);
i++;
}
return(0);
}
/* Add a single number to end of a nmbrString - faster than nmbrCat */
nmbrString *nmbrAddElement(nmbrString *g, long element)
{
long length;
nmbrString *v;
length = nmbrLen(g);
v = nmbrTempAlloc(length + 2); /* Allow for end of string */
nmbrCpy(v, g);
v[length] = element;
v[length + 1] = *NULL_NMBRSTRING; /* End of string */
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("bbg2: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
return(v);
}
/* Get the set union of two math token strings (presumably
variable lists) */
nmbrString *nmbrUnion(nmbrString *m1, nmbrString *m2)
{
long i,j,len1,len2;
nmbrString *v;
len1 = nmbrLen(m1);
len2 = nmbrLen(m2);
v=nmbrTempAlloc(len1+len2+1); /* Pre-allocate maximum possible space */
nmbrCpy(v,m1);
nmbrZapLen(v, len1);
j = 0;
for (i = 0; i < len2; i++) {
if (!nmbrElementIn(1, v, m2[i])) {
nmbrZapLen(v, len1 + j + 1);
v[len1 + j] = m2[i];
j++;
v[len1 + j] = *NULL_NMBRSTRING;
}
}
v[len1 + j] = *NULL_NMBRSTRING;
nmbrZapLen(v, len1 + j);
/*E*/db2=db2-(len1+len2-nmbrLen(v))*(long)(sizeof(nmbrString));
return(v);
}
/* Get the set intersection of two math token strings (presumably
variable lists) */
nmbrString *nmbrIntersection(nmbrString *m1,nmbrString *m2)
{
long i,j,len2;
nmbrString *v;
len2 = nmbrLen(m2);
v=nmbrTempAlloc(len2+1); /* Pre-allocate maximum possible space */
j = 0;
for (i = 0; i < len2; i++) {
if (nmbrElementIn(1,m1,m2[i])) {
v[j] = m2[i];
j++;
}
}
/* Add end-of-string */
v[j] = *NULL_NMBRSTRING;
nmbrZapLen(v, j);
/*E*/db2=db2-(len2-nmbrLen(v))*(long)(sizeof(nmbrString));
return v;
}
/* Get the set difference m1-m2 of two math token strings (presumably
variable lists) */
nmbrString *nmbrSetMinus(nmbrString *m1,nmbrString *m2)
{
long i,j,len1;
nmbrString *v;
len1 = nmbrLen(m1);
v=nmbrTempAlloc(len1+1); /* Pre-allocate maximum possible space */
j = 0;
for (i = 0; i < len1; i++) {
if (!nmbrElementIn(1,m2,m1[i])) {
v[j] = m1[i];
j++;
}
}
/* Add end-of-string */
v[j] = *NULL_NMBRSTRING;
nmbrZapLen(v, j);
/*E*/db2=db2-(len1-nmbrLen(v))*(long)(sizeof(nmbrString));
return v;
}
/* This is a utility function that returns the length of a subproof that
ends at step */
/* 22-Aug-2012 nm - this doesn't seem to be used outside of mmdata.c -
should we replace it with subproofLen() in mmpfas.c? */
long nmbrGetSubproofLen(nmbrString *proof, long step)
{
long stmt, hyps, pos, i;
char type;
if (step < 0) bug(1329);
stmt = proof[step];
if (stmt < 0) return (1); /* Unknown or label ref */
type = g_Statement[stmt].type;
if (type == f_ || type == e_) return (1); /* Hypothesis */
hyps = g_Statement[stmt].numReqHyp;
pos = step - 1;
for (i = 0; i < hyps; i++) {
pos = pos - nmbrGetSubproofLen(proof, pos);
}
return (step - pos);
}
/* This function returns a packed or "squished" proof, putting in local label
references to previous subproofs. */
nmbrString *nmbrSquishProof(nmbrString *proof)
{
nmbrString *newProof = NULL_NMBRSTRING;
nmbrString *dummyProof = NULL_NMBRSTRING;
nmbrString *subProof = NULL_NMBRSTRING;
long step, dummyStep, subPrfLen, matchStep, plen;
flag foundFlag;
nmbrLet(&newProof,proof); /* In case of temp. alloc. of proof */
plen = nmbrLen(newProof);
dummyStep = 0;
nmbrLet(&dummyProof, newProof); /* Parallel proof with test subproof replaced
with a reference to itself, for matching. */
for (step = 0; step < plen; step++) {
subPrfLen = nmbrGetSubproofLen(dummyProof, dummyStep);
if (subPrfLen <= 1) {
dummyStep++;
continue;
}
nmbrLet(&subProof, nmbrSeg(dummyProof, dummyStep - subPrfLen + 2,
dummyStep + 1));
matchStep = step + 1;
foundFlag = 0;
while (1) {
matchStep = nmbrInstr(matchStep + 1, newProof, subProof);
if (!matchStep) break; /* No more occurrences */
foundFlag = 1;
/* Replace the found subproof with a reference to this subproof */
nmbrLet(&newProof,
nmbrCat(nmbrAddElement(nmbrLeft(newProof, matchStep - 1),
-1000 - step), nmbrRight(newProof, matchStep + subPrfLen), NULL));
matchStep = matchStep - subPrfLen + 1;
}
if (foundFlag) {
plen = nmbrLen(newProof); /* Update the new proof length */
/* Replace this subproof with a reference to itself, for later matching */
/* and add on rest of real proof. */
dummyStep = dummyStep + 1 - subPrfLen;
nmbrLet(&dummyProof,
nmbrCat(nmbrAddElement(nmbrLeft(dummyProof, dummyStep),
-1000 - step), nmbrRight(newProof, step + 2), NULL));
}
dummyStep++;
} /* Next step */
nmbrLet(&subProof, NULL_NMBRSTRING);
nmbrLet(&dummyProof, NULL_NMBRSTRING);
nmbrMakeTempAlloc(newProof); /* Flag it for deallocation */
return (newProof);
}
/* This function unpacks a "squished" proof, replacing local label references
to previous subproofs by the subproofs themselves. */
nmbrString *nmbrUnsquishProof(nmbrString *proof)
{
nmbrString *newProof = NULL_NMBRSTRING;
nmbrString *subProof = NULL_NMBRSTRING;
long step, plen, subPrfLen, stmt;
nmbrLet(&newProof, proof);
plen = nmbrLen(newProof);
for (step = plen - 1; step >= 0; step--) {
stmt = newProof[step];
if (stmt > -1000) continue;
/* It's a local label reference */
stmt = -1000 - stmt;
subPrfLen = nmbrGetSubproofLen(newProof, stmt);
nmbrLet(&newProof, nmbrCat(nmbrLeft(newProof, step),
nmbrSeg(newProof, stmt - subPrfLen + 2, stmt + 1),
nmbrRight(newProof, step + 2), NULL));
step = step + subPrfLen - 1;
}
nmbrLet(&subProof, NULL_NMBRSTRING);
nmbrMakeTempAlloc(newProof); /* Flag it for deallocation */
return (newProof);
}
/* This function returns the indentation level vs. step number of a proof
string. This information is used for formatting proof displays. The
function calls itself recursively, but the first call should be with
startingLevel = 0. */
/* ???Optimization: remove nmbrString calls and use static variables
to communicate to recursive calls */
nmbrString *nmbrGetIndentation(nmbrString *proof,
long startingLevel)
{
long plen, stmt, pos, splen, hyps, i, j;
char type;
nmbrString *indentationLevel = NULL_NMBRSTRING;
nmbrString *subProof = NULL_NMBRSTRING;
nmbrString *nmbrTmp = NULL_NMBRSTRING;
plen = nmbrLen(proof);
stmt = proof[plen - 1];
nmbrLet(&indentationLevel, nmbrSpace(plen));
indentationLevel[plen - 1] = startingLevel;
if (stmt < 0) { /* A local label reference or unknown */
if (plen != 1) bug(1330);
nmbrMakeTempAlloc(indentationLevel); /* Flag it for deallocation */
return (indentationLevel);
}
type = g_Statement[stmt].type;
if (type == f_ || type == e_) { /* A hypothesis */
if (plen != 1) bug(1331);
nmbrMakeTempAlloc(indentationLevel); /* Flag it for deallocation */
return (indentationLevel);
}
/* An assertion */
if (type != a_ && type != p_) bug(1332);
hyps = g_Statement[stmt].numReqHyp;
pos = plen - 2;
for (i = 0; i < hyps; i++) {
splen = nmbrGetSubproofLen(proof, pos);
nmbrLet(&subProof, nmbrSeg(proof, pos - splen + 2, pos + 1));
nmbrLet(&nmbrTmp, nmbrGetIndentation(subProof, startingLevel + 1));
for (j = 0; j < splen; j++) {
indentationLevel[j + pos - splen + 1] = nmbrTmp[j];
}
pos = pos - splen;
}
if (pos != -1) bug (333);
nmbrLet(&subProof,NULL_NMBRSTRING); /* Deallocate */
nmbrLet(&nmbrTmp, NULL_NMBRSTRING); /* Deallocate */
nmbrMakeTempAlloc(indentationLevel); /* Flag it for deallocation */
return (indentationLevel);
} /* nmbrGetIndentation */
/* This function returns essential (1) or floating (0) vs. step number of a
proof string. This information is used for formatting proof displays. The
function calls itself recursively. */
/* ???Optimization: remove nmbrString calls and use static variables
to communicate to recursive calls */
nmbrString *nmbrGetEssential(nmbrString *proof)
{
long plen, stmt, pos, splen, hyps, i, j;
char type;
nmbrString *essentialFlags = NULL_NMBRSTRING;
nmbrString *subProof = NULL_NMBRSTRING;
nmbrString *nmbrTmp = NULL_NMBRSTRING;
nmbrString *nmbrTmpPtr2;
plen = nmbrLen(proof);
stmt = proof[plen - 1];
nmbrLet(&essentialFlags, nmbrSpace(plen));
essentialFlags[plen - 1] = 1;
if (stmt < 0) { /* A local label reference or unknown */
if (plen != 1) bug(1334);
/* The only time it should get here is if the original proof has only one
step, which would be an unknown step */
if (stmt != -(long)'?' && stmt > -1000) bug(1335);
nmbrMakeTempAlloc(essentialFlags); /* Flag it for deallocation */
return (essentialFlags);
}
type = g_Statement[stmt].type;
if (type == f_ || type == e_) { /* A hypothesis */
/* The only time it should get here is if the original proof has only one
step */
if (plen != 1) bug(1336);
nmbrMakeTempAlloc(essentialFlags); /* Flag it for deallocation */
return (essentialFlags);
}
/* An assertion */
if (type != a_ && type != p_) bug(1337);
hyps = g_Statement[stmt].numReqHyp;
pos = plen - 2;
nmbrTmpPtr2 = g_Statement[stmt].reqHypList;
for (i = 0; i < hyps; i++) {
splen = nmbrGetSubproofLen(proof, pos);
if (g_Statement[nmbrTmpPtr2[hyps - i - 1]].type == e_) {
nmbrLet(&subProof, nmbrSeg(proof, pos - splen + 2, pos + 1));
nmbrLet(&nmbrTmp, nmbrGetEssential(subProof));
for (j = 0; j < splen; j++) {
essentialFlags[j + pos - splen + 1] = nmbrTmp[j];
}
}
pos = pos - splen;
}
if (pos != -1) bug (1338);
nmbrLet(&subProof,NULL_NMBRSTRING); /* Deallocate */
nmbrLet(&nmbrTmp, NULL_NMBRSTRING); /* Deallocate */
nmbrMakeTempAlloc(essentialFlags); /* Flag it for deallocation */
return (essentialFlags);
} /* nmbrGetEssential */
/* This function returns the target hypothesis vs. step number of a proof
string. This information is used for formatting proof displays. The
function calls itself recursively.
statemNum is the statement being proved. */
/* ???Optimization: remove nmbrString calls and use static variables
to communicate to recursive calls */
nmbrString *nmbrGetTargetHyp(nmbrString *proof, long statemNum)
{
long plen, stmt, pos, splen, hyps, i, j;
char type;
nmbrString *targetHyp = NULL_NMBRSTRING;
nmbrString *subProof = NULL_NMBRSTRING;
nmbrString *nmbrTmp = NULL_NMBRSTRING;
plen = nmbrLen(proof);
stmt = proof[plen - 1];
nmbrLet(&targetHyp, nmbrSpace(plen));
if (statemNum) { /* First (rather than recursive) call */
targetHyp[plen - 1] = statemNum; /* Statement being proved */
}
if (stmt < 0) { /* A local label reference or unknown */
if (plen != 1) bug(1339);
/* The only time it should get here is if the original proof has only one
step, which would be an unknown step */
if (stmt != -(long)'?') bug(1340);
nmbrMakeTempAlloc(targetHyp); /* Flag it for deallocation */
return (targetHyp);
}
type = g_Statement[stmt].type;
if (type == f_ || type == e_) { /* A hypothesis */
/* The only time it should get here is if the original proof has only one
step */
if (plen != 1) bug(1341);
nmbrMakeTempAlloc(targetHyp); /* Flag it for deallocation */
return (targetHyp);
}
/* An assertion */
if (type != a_ && type != p_) bug(1342);
hyps = g_Statement[stmt].numReqHyp;
pos = plen - 2;
for (i = 0; i < hyps; i++) {
splen = nmbrGetSubproofLen(proof, pos);
if (splen > 1) {
nmbrLet(&subProof, nmbrSeg(proof, pos - splen + 2, pos + 1));
nmbrLet(&nmbrTmp, nmbrGetTargetHyp(subProof,
g_Statement[stmt].reqHypList[hyps - i - 1]));
for (j = 0; j < splen; j++) {
targetHyp[j + pos - splen + 1] = nmbrTmp[j];
}
} else {
/* A one-step subproof; don't bother with recursive call */
targetHyp[pos] = g_Statement[stmt].reqHypList[hyps - i - 1];
}
pos = pos - splen;
}
if (pos != -1) bug (343);
nmbrLet(&subProof,NULL_NMBRSTRING); /* Deallocate */
nmbrLet(&nmbrTmp, NULL_NMBRSTRING); /* Deallocate */
nmbrMakeTempAlloc(targetHyp); /* Flag it for deallocation */
return (targetHyp);
} /* nmbrGetTargetHyp */
/* Converts a proof string to a compressed-proof-format ASCII string.
Normally, the proof string would be packed with nmbrSquishProof first,
although it's not a requirement (in which case the compressed proof will
be much longer of course). */
/* The statement number is needed because required hypotheses are
implicit in the compressed proof. */
/* The returned ASCII string isn't surrounded by spaces e.g. it
could be "( a1i a1d ) ACBCADEF". */
vstring compressProof(nmbrString *proof, long statemNum,
flag oldCompressionAlgorithm)
{
vstring output = "";
long outputLen;
long outputAllocated;
nmbrString *saveProof = NULL_NMBRSTRING;
nmbrString *labelList = NULL_NMBRSTRING;
nmbrString *hypList = NULL_NMBRSTRING;
nmbrString *assertionList = NULL_NMBRSTRING;
nmbrString *localList = NULL_NMBRSTRING;
nmbrString *localLabelFlags = NULL_NMBRSTRING;
long hypLabels, assertionLabels, localLabels;
long plen, step, stmt, labelLen, lab, numchrs;
/* long thresh, newnumchrs, newlab; */ /* 15-Oct-05 nm No longer used */
long i, j, k;
/* flag breakFlag; */ /* 15-Oct-05 nm No longer used */
/* char c; */ /* 15-Oct-05 nm No longer used */
long lettersLen, digitsLen;
static char *digits = "0123456789";
static char *letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
static char labelChar = ':';
/* 27-Dec-2013 nm Variables for new algorithm */
nmbrString *explList = NULL_NMBRSTRING;
long explLabels;
nmbrString *explRefCount = NULL_NMBRSTRING;
nmbrString *labelRefCount = NULL_NMBRSTRING;
long maxExplRefCount;
nmbrString *explComprLen = NULL_NMBRSTRING;
long explSortPosition;
long maxExplComprLen;
vstring explUsedFlag = "";
nmbrString *explLabelLen = NULL_NMBRSTRING;
nmbrString *newExplList = NULL_NMBRSTRING;
long newExplPosition;
long indentation;
long explOffset;
long explUnassignedCount;
nmbrString *explWorth = NULL_NMBRSTRING;
long explWidth;
vstring explIncluded = "";
/* Compression standard with all cap letters */
/* (For 500-700 step proofs, we only lose about 18% of file size --
but the compressed proof is more pleasing to the eye) */
letters = "ABCDEFGHIJKLMNOPQRST"; /* LSB is base 20 */
digits = "UVWXY"; /* MSB's are base 5 */
labelChar = 'Z'; /* Was colon */
lettersLen = (long)strlen(letters);
digitsLen = (long)strlen(digits);
nmbrLet(&saveProof, proof); /* In case of temp. alloc. of proof */
if (g_Statement[statemNum].type != (char)p_) bug(1344);
plen = nmbrLen(saveProof);
/* Create the initial label list of required hypotheses */
nmbrLet(&labelList, g_Statement[statemNum].reqHypList);
/* Add the other statement labels to the list */
/* Warning: The exact union algorithm is crucial here; the required
hypotheses MUST remain at the beginning of the list. */
nmbrLet(&labelList, nmbrUnion(labelList, saveProof));
/* Break the list into hypotheses, assertions, and local labels */
labelLen = nmbrLen(labelList);
nmbrLet(&hypList, nmbrSpace(labelLen));
nmbrLet(&assertionList, nmbrSpace(labelLen));
nmbrLet(&localLabelFlags, nmbrSpace(plen)); /* Warning: nmbrSpace() must
produce a string of 0's */
hypLabels = 0;
assertionLabels = 0;
localLabels = 0;
for (lab = 0; lab < labelLen; lab++) {
stmt = labelList[lab];
if (stmt < 0) {
if (stmt <= -1000) {
if (-1000 - stmt >= plen) bug(345);
localLabelFlags[-1000 - stmt] = 1;
localLabels++;
} else {
if (stmt != -(long)'?') bug(1346);
}
} else {
if (g_Statement[stmt].type != (char)a_ &&
g_Statement[stmt].type != (char)p_) {
hypList[hypLabels] = stmt;
hypLabels++;
} else {
assertionList[assertionLabels] = stmt;
assertionLabels++;
}
}
} /* Next lab */
nmbrLet(&hypList, nmbrLeft(hypList, hypLabels));
nmbrLet(&assertionList, nmbrLeft(assertionList, assertionLabels));
/* Get list of local labels, sorted in order of declaration */
nmbrLet(&localList, nmbrSpace(localLabels));
lab = 0;
for (step = 0; step < plen; step++) {
if (localLabelFlags[step]) {
localList[lab] = -1000 - step;
lab++;
}
}
if (lab != localLabels) bug(1347);
/* To obtain the old algorithm, we simply skip the new label re-ordering */
if (oldCompressionAlgorithm) goto OLD_ALGORITHM;
/* 27-Dec-2013 nm */
/************ New algorithm to sort labels according to usage ***********/
/* This algorithm, based on an idea proposed by Mario Carneiro, sorts
the explicit labels so that the most-used labels occur first, optimizing
the use of 1-character compressed label lengths, then 2-character
lengths, and so on. Also, an attempt is made to fit the label list into
the exact maximum current screen width, using the 0/1-knapsack
algorithm, so that fewer lines will result due to wasted space at the
end of each line with labels. */
/* Get the list of explicit labels */
nmbrLet(&explList, nmbrCat(
/* Trim off leading implicit required hypotheses */
nmbrRight(hypList, g_Statement[statemNum].numReqHyp + 1),
/* Add in the list of assertion ($a, $p) references */
assertionList, NULL));
explLabels = nmbrLen(explList);
/* Initialize reference counts for the explicit labels */
nmbrLet(&explRefCount, nmbrSpace(explLabels));
/* Count the number of references to labels in the original proof */
/* We allocate up to statemNum, since any earlier statement could appear */
nmbrLet(&labelRefCount, nmbrSpace(statemNum)); /* Warning: nmbrSpace() must
produce a string of 0's */
for (step = 0; step < plen; step++) { /* Scan the proof */
if (saveProof[step] > 0) { /* Ignore local labels and '?' */
if (saveProof[step] < statemNum) {
labelRefCount[saveProof[step]]++;
} else {
bug(1380); /* Corrupted proof should have been caught earlier */
}
}
}
maxExplRefCount = 0; /* Largest number of reference counts found */
/* Populate the explict label list with the counts */
for (i = 0; i < explLabels; i++) {
explRefCount[i] = labelRefCount[explList[i]]; /* Save the ref count */
if (explRefCount[i] <= 0) bug(1381);
if (explRefCount[i] > maxExplRefCount) {
maxExplRefCount = explRefCount[i]; /* Update largest count */
}
}
/* We're done with giant labelRefCount array; deallocate */
nmbrLet(&labelRefCount, NULL_NMBRSTRING);
/* Assign compressed label lengths starting from most used to least
used label */
/* Initialize compressed label lengths for the explicit labels */
nmbrLet(&explComprLen, nmbrSpace(explLabels));
explSortPosition = 0;
maxExplComprLen = 0;
/* The "sorting" below has n^2 behavior; improve if is it a problem */
/* explSortPosition is where the label would occur if reverse-sorted
by reference count, for the purpose of computing the compressed
label length. No actual sorting is done, since later we're
only interested in groups with the same compressed label length. */
for (i = maxExplRefCount; i >= 1; i--) {
for (j = 0; j < explLabels; j++) {
if (explRefCount[j] == i) {
/* Find length, numchrs, of compressed label */
/* If there are no req hyps, 0 = 1st label in explict list */
lab = g_Statement[statemNum].numReqHyp + explSortPosition;
/* The following 7 lines are from the compressed label length
determination algorithm below */
numchrs = 1;
k = lab / lettersLen;
while (1) {
if (!k) break;
numchrs++;
k = (k - 1) / digitsLen;
}
explComprLen[j] = numchrs; /* Assign the compressed label length */
if (numchrs > maxExplComprLen) {
maxExplComprLen = numchrs; /* Update maximum length */
}
explSortPosition++;
}
}
}
let(&explUsedFlag, string(explLabels, 'n')); /* Mark with 'y' when placed in
output label list (newExplList) */
nmbrLet(&explLabelLen, nmbrSpace(explLabels));
/* Populate list of label lengths for knapsack01() "size" */
for (i = 0; i < explLabels; i++) {
stmt = explList[i];
explLabelLen[i] = (long)(strlen(g_Statement[stmt].labelName)) + 1;
/* +1 accounts for space between labels */
}
/* Re-distribute labels in order of compressed label length, fitted to
line by knapsack01() algorithm */
nmbrLet(&newExplList, nmbrSpace(explLabels)); /* List in final order */
nmbrLet(&explWorth, nmbrSpace(explLabels)); /* "Value" for knapsack01() */
let(&explIncluded, string(explLabels, '?')); /* Returned by knapsack01() */
newExplPosition = 0; /* Counter for position in output label list */
indentation = 2 + getSourceIndentation(statemNum); /* Proof indentation */
explOffset = 2; /* add 2 for "( " opening parenthesis of compressed proof */
/* Fill up the output with labels in groups of increasing compressed label
size */
for (i = 1; i <= maxExplComprLen; i++) {
explUnassignedCount = 0; /* Unassigned at current compressed label size */
/* Initialize worths for knapsack01() */
for (j = 0; j < explLabels; j++) {
if (explComprLen[j] == i) {
if (explUsedFlag[j] == 'y') bug(1382);
explWorth[j] = explLabelLen[j]; /* Make worth=size so that label
length does not affect whether the label is chosen by knapsack01(),
so the only influence is whether it fits */
explUnassignedCount++;
} else { /* Not the current compressed label size */
explWorth[j] = -1; /* Negative worth will make knapsack avoid it */
}
}
while (explUnassignedCount > 0) {
/* Find the the amount of space available on the remainder of the line */
/* The +1 accounts for space after last label, which wrapping will trim */
/* Note that the actual line wrapping will happen with printLongLine
far in the future. Here we will just put the labels in the order
that will cause it to wrap at the desired place. */
explWidth = g_screenWidth - indentation - explOffset + 1;
/* Fill in the label list output line with labels that fit best */
/* The knapsack01() call below is always given the entire set of
explicit labels, with -1 worth assigned to the ones to be avoided.
It would be more efficient to give it a smaller list with -1s
removed, if run time becomes a problem. */
j = knapsack01(explLabels /*#items*/,
explLabelLen /*array of sizes*/,
explWorth /*array of worths*/,
explWidth /*maxSize*/,
explIncluded /*itemIncluded return values*/);
/*if (j == 0) bug(1383);*/ /* j=0 is legal when it can't fit any labels
on the rest of the line (such as if the line only has 1 space left
i.e. explWidth=1) */
if (j < 0) bug(1383);
/* Accumulate the labels selected by knapsack01() into the output list,
in the same order as they appeared in the original explicit label
list */
explUnassignedCount = 0;
/* Scan expIncluded y/n string returned by knapsack01() */
for (j = 0; j < explLabels; j++) {
if (explIncluded[j] == 'y') { /* was chosen by knapsack01() */
if (explComprLen[j] != i) bug(1384); /* Other compressed length
shouldn't occur because -1 worth should have been rejected by
knapsack01() */
newExplList[newExplPosition] = explList[j];
newExplPosition++;
explUsedFlag[j] = 'y';
if (explWorth[j] == -1) bug(1385); /* knapsack01() should
have rejected all previously assigned labels */
explWorth[j] = -1; /* Negative worth will avoid it next loop iter */
explOffset = explOffset + explLabelLen[j];
} else {
if (explComprLen[j] == i && explUsedFlag[j] == 'n') {
explUnassignedCount++; /* There are still more to be assigned
at this compressed label length */
if (explWorth[j] != explLabelLen[j]) bug(1386); /* Sanity check */
}
}
}
if (explUnassignedCount > 0) {
/* If there are labels still at this level (of compressed
label length), so start a new line for next knapsack01() call */
explOffset = 0;
}
}
}
if (newExplPosition != explLabels) bug(1387); /* Labels should be exhausted */
/* The hypList and assertionList below are artificially assigned
for use by the continuation of the old algorithm that follows */
/* "hypList" is truncated to have only the required hypotheses with no
optional ones */
nmbrLet(&hypList, nmbrLeft(hypList, g_Statement[statemNum].numReqHyp));
/* "assertionList" will have both the optional hypotheses and the assertions,
reordered */
nmbrLet(&assertionList, newExplList);
/********************** End of new algorithm ****************************/
OLD_ALGORITHM:
/* Combine all label lists */
nmbrLet(&labelList, nmbrCat(hypList, assertionList, localList, NULL));
/* Create the compressed proof */
outputLen = 0;
#define COMPR_INC 1000
let(&output, space(COMPR_INC));
outputAllocated = COMPR_INC;
plen = nmbrLen(saveProof);
for (step = 0; step < plen; step++) {
stmt = saveProof[step];
if (stmt == -(long)'?') {
/* Unknown step */
if (outputLen + 1 > outputAllocated) {
/* Increase allocation of the output string */
let(&output, cat(output, space(outputLen + 1 - outputAllocated +
COMPR_INC), NULL));
outputAllocated = outputLen + 1 + COMPR_INC; /* = (long)strlen(output) */
/* CPU-intensive bug check; enable only if required: */
/* if (outputAllocated != (long)strlen(output)) bug(1348); */
if (output[outputAllocated - 1] == 0 ||
output[outputAllocated] != 0) bug(1348); /* 13-Oct-05 nm */
}
output[outputLen] = '?';
outputLen++;
continue;
}
lab = nmbrElementIn(1, labelList, stmt);
if (!lab) bug(1349);
lab--; /* labelList array starts at 0, not 1 */
/* Determine the # of chars in the compressed label */
/* 15-Oct-05 nm - Obsolete (skips from YT to UVA, missing UUA) */
/*
numchrs = 1;
if (lab > lettersLen - 1) {
/ * It requires a numeric prefix * /
i = lab / lettersLen;
while(i) {
numchrs++;
if (i > digitsLen) {
i = i / digitsLen;
} else {
i = 0; / * MSB is sort of 'mod digitsLen+1' since
a blank is the MSB in the case of one
fewer characters in the label * /
}
}
}
*/
/* 15-Oct-05 nm - A corrected algorithm was provided by Marnix Klooster. */
/* For encoding we'd get (starting with n, counting from 1):
* start with the empty string
* prepend (n-1) mod 20 + 1 as character using 1->'A' .. 20->'T'
* n := (n-1) div 20
* while n > 0:
* prepend (n-1) mod 5 + 1 as character using 1->'U' .. 5->'Y'
* n := (n-1) div 5 */
if (lab < 0) bug(1373);
numchrs = 1;
i = lab / lettersLen;
while (1) {
if (!i) break;
numchrs++;
i = (i - 1) / digitsLen;
}
/* Add the compressed label to the proof */
if (outputLen + numchrs > outputAllocated) {
/* Increase allocation of the output string */
let(&output, cat(output, space(outputLen + numchrs - outputAllocated +
COMPR_INC), NULL));
outputAllocated = outputLen + numchrs + COMPR_INC; /* = (long)strlen(output) */
/* CPU-intensive bug check; enable only if required: */
/* if (outputAllocated != (long)strlen(output)) bug(1350); */
if (output[outputAllocated - 1] == 0 ||
output[outputAllocated] != 0) bug(1350); /* 13-Oct-05 nm */
}
outputLen = outputLen + numchrs;
/* 15-Oct-05 nm - Obsolete (skips from YT to UVA, missing UUA) */
/*
j = lab;
for (i = 0; i < numchrs; i++) { / * Create from LSB to MSB * /
if (!i) {
c = letters[j % lettersLen];
j = j / lettersLen;
} else {
if (j > digitsLen) {
c = digits[j % digitsLen];
j = j / digitsLen;
} else {
c = digits[j - 1]; / * MSB is sort of 'mod digitsLen+1' since
a blank is the MSB in the case of one
fewer characters in the label * /
}
}
output[outputLen - i - 1] = c;
} / * Next i * /
*/
/* 15-Oct-05 nm - A corrected algorithm was provided by Marnix Klooster. */
/* For encoding we'd get (starting with n, counting from 1):
* start with the empty string
* prepend (n-1) mod 20 + 1 as character using 1->'A' .. 20->'T'
* n := (n-1) div 20
* while n > 0:
* prepend (n-1) mod 5 + 1 as character using 1->'U' .. 5->'Y'
* n := (n-1) div 5 */
j = lab + 1; /* lab starts at 0, not 1 */
i = 1;
output[outputLen - i] = letters[(j - 1) % lettersLen];
j = (j - 1) / lettersLen;
while (1) {
if (!j) break;
i++;
output[outputLen - i] = digits[(j - 1) % digitsLen];
j = (j - 1) / digitsLen;
}
if (i != numchrs) bug(1374);
/***** Local labels ******/
/* See if a local label is declared in this step */
if (!localLabelFlags[step]) continue;
if (outputLen + 1 > outputAllocated) {
/* Increase allocation of the output string */
let(&output, cat(output, space(outputLen + 1 - outputAllocated +
COMPR_INC), NULL));
outputAllocated = outputLen + 1 + COMPR_INC; /* = (long)strlen(output) */
/* CPU-intensive bug check due to strlen; enable only if required: */
/* if (outputAllocated != (long)strlen(output)) bug(1352); */
if (output[outputAllocated - 1] == 0 ||
output[outputAllocated] != 0) bug(1352); /* 13-Oct-05 nm */
}
output[outputLen] = labelChar;
outputLen++;
} /* Next step */
/* Create the final compressed proof */
let(&output, cat("( ", nmbrCvtRToVString(nmbrCat(
/* Trim off leading implicit required hypotheses */
nmbrRight(hypList, g_Statement[statemNum].numReqHyp + 1),
assertionList, NULL),
/* 25-Jan-2016 nm */
0, /*explicitTargets*/
0 /*statemNum used only if explicitTargets*/),
" ) ", left(output, outputLen), NULL));
nmbrLet(&saveProof, NULL_NMBRSTRING);
nmbrLet(&labelList, NULL_NMBRSTRING);
nmbrLet(&hypList, NULL_NMBRSTRING);
nmbrLet(&assertionList, NULL_NMBRSTRING);
nmbrLet(&localList, NULL_NMBRSTRING);
nmbrLet(&localLabelFlags, NULL_NMBRSTRING);
/* Deallocate arrays for new algorithm */ /* 27-Dec-2013 nm */
nmbrLet(&explList, NULL_NMBRSTRING);
nmbrLet(&explRefCount, NULL_NMBRSTRING);
nmbrLet(&labelRefCount, NULL_NMBRSTRING);
nmbrLet(&explComprLen, NULL_NMBRSTRING);
let(&explUsedFlag, "");
nmbrLet(&explLabelLen, NULL_NMBRSTRING);
nmbrLet(&newExplList, NULL_NMBRSTRING);
nmbrLet(&explWorth, NULL_NMBRSTRING);
let(&explIncluded, "");
makeTempAlloc(output); /* Flag it for deallocation */
return(output);
} /* compressProof */
/* Added 11-Sep-2016 nm */
/* Compress the input proof, create the ASCII compressed proof,
and return its size in bytes. */
/* TODO: call this in MINIMIZE_WITH in metamath.c */
long compressedProofSize(nmbrString *proof, long statemNum) {
vstring tmpStr = "";
nmbrString *tmpNmbr = NULL_NMBRSTRING;
long bytes;
nmbrLet(&tmpNmbr, nmbrSquishProof(proof));
let(&tmpStr, compressProof(tmpNmbr,
statemNum, /* statement being proved */
0 /* don't use old algorithm (this will become obsolete) */
));
bytes = (long)strlen(tmpStr);
/* Deallocate memory */
let(&tmpStr, "");
nmbrLet(&tmpNmbr, NULL_NMBRSTRING);
return bytes;
} /* compressedProofSize */
/*******************************************************************/
/*********** Pointer string functions ******************************/
/*******************************************************************/
long g_pntrTempAllocStackTop = 0; /* Top of stack for pntrTempAlloc functon */
long g_pntrStartTempAllocStack = 0; /* Where to start freeing temporary allocation
when pntrLet() is called (normally 0, except in
special nested vstring functions) */
pntrString *pntrTempAllocStack[M_MAX_ALLOC_STACK];
pntrString *pntrTempAlloc(long size)
/* pntrString memory allocation/deallocation */
{
/* When "size" is >0, "size" instances of pntrString are allocated. */
/* When "size" is 0, all memory previously allocated with this */
/* function is deallocated, down to g_pntrStartTempAllocStack. */
/* int i; */ /* 11-Jul-2014 WL old code deleted */
if (size) {
if (g_pntrTempAllocStackTop>=(M_MAX_ALLOC_STACK-1))
/*??? Fix to allocate more */
outOfMemory("#109 (pntrString stack array)");
if (!(pntrTempAllocStack[g_pntrTempAllocStackTop++]=poolMalloc(size
*(long)(sizeof(pntrString)))))
/* outOfMemory("#110 (pntrString stack)"); */ /*???Unnec. w/ poolMalloc*/
/*E*/db2=db2+(size)*(long)(sizeof(pntrString));
return (pntrTempAllocStack[g_pntrTempAllocStackTop-1]);
} else {
/* 11-Jul-2014 WL old code deleted */
/*
for (i=g_pntrStartTempAllocStack; i < g_pntrTempAllocStackTop; i++) {
/@E@/db2=db2-(pntrLen(pntrTempAllocStack[i])+1)*(long)(sizeof(pntrString));
poolFree(pntrTempAllocStack[i]);
}
*/
/* 11-Jul-2014 WL new code */
while(g_pntrTempAllocStackTop != g_pntrStartTempAllocStack) {
/*E*/db2=db2-(pntrLen(pntrTempAllocStack[g_pntrTempAllocStackTop-1])+1)
/*E*/ *(long)(sizeof(pntrString));
poolFree(pntrTempAllocStack[--g_pntrTempAllocStackTop]);
}
/* end of 11-Jul-2014 WL new code */
g_pntrTempAllocStackTop=g_pntrStartTempAllocStack;
return (0);
}
}
/* Make string have temporary allocation to be released by next pntrLet() */
/* Warning: after pntrMakeTempAlloc() is called, the pntrString may NOT be
assigned again with pntrLet() */
void pntrMakeTempAlloc(pntrString *s)
{
if (g_pntrTempAllocStackTop>=(M_MAX_ALLOC_STACK-1)) {
printf(
"*** FATAL ERROR *** Temporary pntrString stack overflow in pntrMakeTempAlloc()\n");
#if __STDC__
fflush(stdout);
#endif
bug(1370);
}
if (s[0] != NULL) { /* Don't do it if pntrString is empty */
pntrTempAllocStack[g_pntrTempAllocStackTop++] = s;
}
/*E*/db2=db2+(pntrLen(s)+1)*(long)(sizeof(pntrString));
/*E*/db3=db3-(pntrLen(s)+1)*(long)(sizeof(pntrString));
}
void pntrLet(pntrString **target,pntrString *source)
/* pntrString assignment */
/* This function must ALWAYS be called to make assignment to */
/* a pntrString in order for the memory cleanup routines, etc. */
/* to work properly. If a pntrString has never been assigned before, */
/* it is the user's responsibility to initialize it to NULL_PNTRSTRING (the */
/* null string). */
{
long targetLength,sourceLength;
long targetAllocLen;
long poolDiff;
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
sourceLength=pntrLen(source); /* Save its actual length */
targetLength=pntrLen(*target); /* Save its actual length */
targetAllocLen=pntrAllocLen(*target); /* Save target's allocated length */
/*E*/if (targetLength) {
/*E*/ /* printf("Deleting %s\n",cvtMToVString(*target,0)); */
/*E*/ db3 = db3 - (targetLength+1)*(long)(sizeof(pntrString));
/*E*/}
/*E*/if (sourceLength) {
/*E*/ /* printf("Adding %s\n",cvtMToVString(source,0)); */
/*E*/ db3 = db3 + (sourceLength+1)*(long)(sizeof(pntrString));
/*E*/}
if (targetAllocLen) {
if (sourceLength) { /* source and target are both nonzero length */
if (targetAllocLen >= sourceLength) { /* Old string has room for new one */
pntrCpy(*target,source); /* Re-use the old space to save CPU time */
/* Memory pool handling */
/* Assign actual size of target string */
poolDiff = ((long *)(*target))[-1] - ((long *)source)[-1];
((long *)(*target))[-1] = ((long *)source)[-1];
/* If actual size of target string is less than allocated size, we
may have to add it to the used pool */
if (((long *)(*target))[-1] != ((long *)(*target))[-2]) {
if (((long *)(*target))[-1] > ((long *)(*target))[-2]) bug(1359);
if (((long *)(*target))[-3] == -1) {
/* It's not already in the used pool, so add it */
addToUsedPool(*target);
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0aa: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
} else {
/* Adjust free space independently */
poolTotalFree = poolTotalFree + poolDiff;
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0ab: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
}
} else {
if (((long *)(*target))[-3] != -1) {
/* It's in the pool (but all allocated space coincidentally used) */
/* Adjust free space independently */
poolTotalFree = poolTotalFree + poolDiff;
}
}
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0a: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
} else {
/* Free old string space and allocate new space */
poolFree(*target); /* Free old space */
/* *target=poolMalloc((sourceLength + 1) * sizeof(pntrString)); */
*target=poolMalloc((sourceLength + 1) * (long)(sizeof(pntrString)) * 2);
/* Allocate new space --
We are replacing a smaller string with a larger one;
assume it is growing, and allocate twice as much as
needed. */
/*if (!*target) outOfMemory("#111 (pntrString)");*/ /*???Unnec. w/ poolMalloc*/
pntrCpy(*target,source);
/* Memory pool handling */
/* Assign actual size of target string */
poolDiff = ((long *)(*target))[-1] - ((long *)source)[-1];
((long *)(*target))[-1] = ((long *)source)[-1];
/* If actual size of target string is less than allocated size, we
may have to add it to the used pool */
/* (The 1st 'if' is redundant with target doubling above) */
if (((long *)(*target))[-1] != ((long *)(*target))[-2]) {
if (((long *)(*target))[-1] > ((long *)(*target))[-2]) bug(1360);
if (((long *)(*target))[-3] == -1) {
/* It's not already in the used pool, so add it */
addToUsedPool(*target);
} else {
/* Adjust free space independently */
poolTotalFree = poolTotalFree + poolDiff;
}
} else {
if (((long *)(*target))[-3] != -1) {
/* It's in the pool (but all allocated space coincidentally used) */
/* Adjust free space independently */
poolTotalFree = poolTotalFree + poolDiff;
}
}
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0b: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
}
} else { /* source is 0 length, target is not */
poolFree(*target);
*target= NULL_PNTRSTRING;
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0c: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
}
} else {
if (sourceLength) { /* target is 0 length, source is not */
*target=poolMalloc((sourceLength + 1) * (long)(sizeof(pntrString)));
/* Allocate new space */
/* if (!*target) outOfMemory("#112 (pntrString)"); */ /*???Unnec. w/ poolMalloc*/
pntrCpy(*target,source);
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0d: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
} else { /* source and target are both 0 length */
/* *target= NULL_PNTRSTRING; */ /* Redundant */
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k0e: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
}
}
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("k1: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
pntrTempAlloc(0); /* Free up temporary strings used in expression computation*/
}
pntrString *pntrCat(pntrString *string1,...) /* String concatenation */
{
va_list ap; /* Declare list incrementer */
pntrString *arg[M_MAX_CAT_ARGS]; /* Array to store arguments */
long argLength[M_MAX_CAT_ARGS]; /* Array to store argument lengths */
int numArgs=1; /* Define "last argument" */
int i;
long j;
pntrString *ptr;
arg[0]=string1; /* First argument */
va_start(ap,string1); /* Begin the session */
while ((arg[numArgs++]=va_arg(ap,pntrString *)))
/* User-provided argument list must terminate with NULL */
if (numArgs>=M_MAX_CAT_ARGS-1) {
printf("*** FATAL ERROR *** Too many cat() arguments\n");
#if __STDC__
fflush(stdout);
#endif
bug(1371);
}
va_end(ap); /* End var args session */
numArgs--; /* The last argument (0) is not a string */
/* Find out the total string length needed */
j = 0;
for (i = 0; i < numArgs; i++) {
argLength[i]=pntrLen(arg[i]);
j=j+argLength[i];
}
/* Allocate the memory for it */
ptr=pntrTempAlloc(j+1);
/* Move the strings into the newly allocated area */
j = 0;
for (i = 0; i < numArgs; i++) {
pntrCpy(ptr+j,arg[i]);
j=j+argLength[i];
}
return (ptr);
}
/* Find out the length of a pntrString */
long pntrLen(pntrString *s)
{
/* Assume it's been allocated with poolMalloc. */
return ((((long *)s)[-1] - (long)(sizeof(pntrString)))
/ (long)(sizeof(pntrString)));
}
/* Find out the allocated length of a pntrString */
long pntrAllocLen(pntrString *s)
{
return ((((long *)s)[-2] - (long)(sizeof(pntrString)))
/ (long)(sizeof(pntrString)));
}
/* Set the actual size field in a pntrString allocated with poolFixedMalloc() */
/* Use this if "zapping" a pntrString element with -1 to reduce its length. */
/* Note that the pntrString will not be moved to the "used pool", even if
zapping its length results in free space; thus the free space will never
get recovered unless done by the caller or poolFree is called. (This is
done on purpose so the caller can know what free space is left.) */
/* ???Note that pntrZapLen's not moving string to used pool wastes potential
space when called by the routines in this module. Effect should be minor. */
void pntrZapLen(pntrString *s, long length) {
if (((long *)s)[-3] != -1) {
/* It's already in the used pool, so adjust free space tally */
poolTotalFree = poolTotalFree + ((long *)s)[-1]
- (length + 1) * (long)(sizeof(pntrString));
}
((long *)s)[-1] = (length + 1) * (long)(sizeof(pntrString));
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("l: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
}
/* Copy a string to another (pre-allocated) string */
/* Dangerous for general purpose use */
void pntrCpy(pntrString *s, pntrString *t)
{
long i;
i = 0;
while (t[i] != NULL) { /* End of string -- pntrRight depends on it!! */
s[i] = t[i];
i++;
}
s[i] = t[i]; /* End of string */
}
/* Copy a string to another (pre-allocated) string */
/* Like strncpy, only the 1st n characters are copied. */
/* Dangerous for general purpose use */
void pntrNCpy(pntrString *s,pntrString *t,long n)
{
long i;
i = 0;
while (t[i] != NULL) { /* End of string -- pntrSeg, pntrMid depend on it!! */
if (i >= n) break;
s[i] = t[i];
i++;
}
s[i] = t[i]; /* End of string */
}
/* Compare two strings */
/* Unlike strcmp, this returns a 1 if the strings are equal
and 0 otherwise. */
/* Only the pointers are compared. If pointers are different,
0 will be returned, even if the things pointed to have same contents. */
int pntrEq(pntrString *s,pntrString *t)
{
long i;
for (i = 0; s[i] == t[i]; i++)
if (s[i] == NULL) /* End of string */
return 1;
return 0;
}
/* Extract sin from character position start to stop into sout */
pntrString *pntrSeg(pntrString *sin, long start, long stop)
{
pntrString *sout;
long length;
if (start < 1 ) start = 1;
if (stop < 1 ) stop = 0;
length = stop - start + 1;
if (length < 0) length = 0;
sout = pntrTempAlloc(length + 1);
pntrNCpy(sout, sin + start - 1, length);
sout[length] = *NULL_PNTRSTRING;
return (sout);
}
/* Extract sin from character position start for length len */
pntrString *pntrMid(pntrString *sin, long start, long length)
{
pntrString *sout;
if (start < 1) start = 1;
if (length < 0) length = 0;
sout = pntrTempAlloc(length + 1);
pntrNCpy(sout, sin + start-1, length);
sout[length] = *NULL_PNTRSTRING;
return (sout);
}
/* Extract leftmost n characters */
pntrString *pntrLeft(pntrString *sin,long n)
{
pntrString *sout;
if (n < 0) n = 0;
sout=pntrTempAlloc(n+1);
pntrNCpy(sout,sin,n);
sout[n] = *NULL_PNTRSTRING;
return (sout);
}
/* Extract after character n */
pntrString *pntrRight(pntrString *sin,long n)
{
/*??? We could just return &sin[n-1], but this is safer for debugging. */
pntrString *sout;
long i;
if (n < 1) n = 1;
i = pntrLen(sin);
if (n > i) return (NULL_PNTRSTRING);
sout = pntrTempAlloc(i - n + 2);
pntrCpy(sout, &sin[n-1]);
return (sout);
}
/* Allocate and return an "empty" string n "characters" long */
/* Each entry in the allocated array points to an empty vString. */
pntrString *pntrSpace(long n)
{
pntrString *sout;
long j = 0;
if (n<0) bug(1360);
sout=pntrTempAlloc(n+1);
while (j<n) {
/* Initialize all fields */
sout[j] = "";
j++;
}
sout[j] = *NULL_PNTRSTRING; /* Flags end of string */
return (sout);
}
/* Allocate and return an "empty" string n "characters" long
initialized to nmbrStrings instead of vStrings */
pntrString *pntrNSpace(long n)
{
pntrString *sout;
long j = 0;
if (n<0) bug(1361);
sout=pntrTempAlloc(n+1);
while (j<n) {
/* Initialize all fields */
sout[j] = NULL_NMBRSTRING;
j++;
}
sout[j] = *NULL_PNTRSTRING; /* Flags end of string */
return (sout);
}
/* Allocate and return an "empty" string n "characters" long
initialized to pntrStrings instead of vStrings */
pntrString *pntrPSpace(long n)
{
pntrString *sout;
long j = 0;
if (n<0) bug(1372);
sout=pntrTempAlloc(n+1);
while (j<n) {
/* Initialize all fields */
sout[j] = NULL_PNTRSTRING;
j++;
}
sout[j] = *NULL_PNTRSTRING; /* Flags end of string */
return (sout);
}
/* Search for string2 in string1 starting at start_position */
long pntrInstr(long start_position,pntrString *string1,
pntrString *string2)
{
long ls1,ls2,i,j;
if (start_position<1) start_position=1;
ls1=pntrLen(string1);
ls2=pntrLen(string2);
for (i=start_position - 1; i <= ls1 - ls2; i++) {
for (j = 0; j<ls2; j++) {
if (string1[i+j] != string2[j])
break;
}
if (j == ls2) return (i+1);
}
return (0);
}
/* Search for string2 in string 1 in reverse starting at start_position */
/* (Reverse pntrInstr) */
long pntrRevInstr(long start_position,pntrString *string1,
pntrString *string2)
{
long ls1,ls2;
pntrString *tmp = NULL_PNTRSTRING;
ls1=pntrLen(string1);
ls2=pntrLen(string2);
if (start_position>ls1-ls2+1) start_position=ls1-ls2+2;
if (start_position<1) return 0;
while (!pntrEq(string2,pntrMid(string1,start_position,ls2))) {
start_position--;
pntrLet(&tmp,NULL_PNTRSTRING);
/* Clear pntrString buffer to prevent overflow caused by "mid" */
if (start_position < 1) return 0;
}
return (start_position);
}
/* Add a single null string element to a pntrString - faster than pntrCat */
pntrString *pntrAddElement(pntrString *g)
{
long length;
pntrString *v;
length = pntrLen(g);
v = pntrTempAlloc(length + 2);
pntrCpy(v, g);
v[length] = "";
v[length + 1] = *NULL_PNTRSTRING;
/*E*/if(db9)getPoolStats(&i1,&j1_,&k1); if(db9)printf("bbg3: pool %ld stat %ld\n",poolTotalFree,i1+j1_);
return(v);
}
/* Add a single null pntrString element to a pntrString -faster than pntrCat */
pntrString *pntrAddGElement(pntrString *g)
{
long length;
pntrString *v;
length = pntrLen(g);
v = pntrTempAlloc(length + 2);
pntrCpy(v, g);
v[length] = NULL_PNTRSTRING;
v[length + 1] = *NULL_PNTRSTRING;
return(v);
}
/*******************************************************************/
/*********** Miscellaneous utility functions ***********************/
/*******************************************************************/
/* 0/1 knapsack algorithm */
/* Returns the maximum worth (value) for items that can fit into maxSize */
/* itemIncluded[] will be populated with 'y'/'n' if item included/excluded */
long knapsack01(long items, /* # of items available to populate knapsack */
long *size, /* size of item 0,...,items-1 */
long *worth, /* worth (value) of item 0,...,items-1 */
long maxSize, /* size of knapsack (largest total size that will fit) */
char *itemIncluded /* output: 'y'/'n' if item 0..items-1 incl/excluded */)
{
long witem, wsize, a, b;
/* Maximum worth that can be attained for given #items and size */
long **maxWorth; /* 2d matrix */
maxWorth = alloc2DMatrix((size_t)items + 1, (size_t)maxSize + 1);
/* This may run faster for applications that have hard-coded limits
#define KS_MAX_ITEMS 100
#define KS_MAX_SIZE 200
static long maxWorth[KS_MAX_ITEMS + 1][KS_MAX_SIZE + 1];
if (items > KS_MAX_ITEMS) {
printf("matrix item overflow\n"); exit(1);
}
if (maxSize > KS_MAX_SIZE) {
printf("matrix size overflow\n"); exit(1);
}
*/
/* Populate the maximum worth matrix */
for (wsize = 0; wsize <= maxSize; wsize++) {
maxWorth[0][wsize] = 0;
}
for (witem = 1; witem <= items; witem++) {
for (wsize = 0; wsize <= maxSize; wsize++) {
if (wsize >= size[witem - 1]) {
/* Item witem can be part of the solution */
a = maxWorth[witem - 1][wsize];
b = maxWorth[witem - 1][wsize - size[witem - 1]] + worth[witem - 1];
/* Choose the case with greater value */
maxWorth[witem][wsize] = (a > b) ? a : b; /* max(a,b) */
} else {
/* Item witem can't be part of the solution, otherwise total size
would exceed the intermediate size wsize. */
maxWorth[witem][wsize] = maxWorth[witem - 1][wsize];
}
}
}
/* Find the included items */
wsize = maxSize;
for (witem = items; witem > 0; witem--) {
itemIncluded[witem - 1] = 'n'; /* Initialize as excluded */
if (wsize > 0) {
if (maxWorth[witem][wsize] != maxWorth[witem - 1][wsize]) {
itemIncluded[witem - 1] = 'y'; /* Include the item */
wsize = wsize - size[witem - 1];
}
}
}
a = maxWorth[items][maxSize]; /* Final maximum worth */
free2DMatrix(maxWorth, (size_t)items + 1 /*, maxSize + 1*/);
return a;
} /* knapsack01 */
/* Allocate a 2-dimensional long integer matrix */
/* Warning: only entries 0,...,xsize-1 and 0,...,ysize-1 are allocated;
don't use entry xsize or ysize! */
long **alloc2DMatrix(size_t xsize, size_t ysize)
{
long **matrix;
long i;
matrix = malloc(xsize * sizeof(long *));
if (matrix == NULL) {
fprintf(stderr,"?FATAL ERROR 1376 Out of memory\n");
exit(1);
}
for (i = 0; i < (long)xsize; i++) {
matrix[i] = malloc(ysize * sizeof(long));
if (matrix[i] == NULL) {
fprintf(stderr,"?FATAL ERROR 1377 Out of memory\n");
exit(1);
}
}
return matrix;
} /* alloc2DMatrix */
/* Free a 2-dimensional long integer matrix */
/* Note: the ysize argument isn't used, but is commented out as
a reminder so the caller doesn't confuse x and y */
void free2DMatrix(long **matrix, size_t xsize /*, size_t ysize*/)
{
long i;
for (i = (long)xsize - 1; i >= 0; i--) {
if (matrix[i] == NULL) bug(1378);
free(matrix[i]);
}
if (matrix == NULL) bug(1379);
free(matrix);
return;
} /* free2DMatrix */
/* Returns the amount of indentation of a statement label. Used to
determine how much to indent a saved proof. */
long getSourceIndentation(long statemNum) {
char *fbPtr; /* Source buffer pointer */
char *startLabel;
long indentation = 0;
fbPtr = g_Statement[statemNum].mathSectionPtr;
if (fbPtr[0] == 0) return 0;
startLabel = g_Statement[statemNum].labelSectionPtr;
if (startLabel[0] == 0) return 0;
while (1) { /* Go back to first line feed prior to the label */
if (fbPtr <= startLabel) break;
if (fbPtr[0] == '\n') break;
if (fbPtr[0] == ' ') {
indentation++; /* Space increments indentation */
} else {
indentation = 0; /* Non-space (i.e. a label character) resets back to 0 */
}
fbPtr--;
}
return indentation;
} /* getSourceIndentation */
/* Returns the last embedded comment (if any) in the label section of
a statement. This is used to provide the user with information in the SHOW
STATEMENT command. The caller must deallocate the result. */
vstring getDescription(long statemNum) {
vstring description = "";
long p1, p2;
let(&description, space(g_Statement[statemNum].labelSectionLen));
memcpy(description, g_Statement[statemNum].labelSectionPtr,
(size_t)(g_Statement[statemNum].labelSectionLen));
p1 = rinstr(description, "$(");
p2 = rinstr(description, "$)");
if (p1 == 0 || p2 == 0 || p2 < p1) {
let(&description, "");
return description;
}
let(&description, edit(seg(description, p1 + 2, p2 - 1),
8 + 128 /* discard leading and trailing blanks */));
return description;
/* 3-May-2017 nm Old code may have been somewhat faster, but it doesn't
work when g_Statement[statemNum].labelSectionChanged */
/************* deleted *******
char @fbPtr; /@ Source buffer pointer @/
vstring description = "";
char @startDescription;
char @endDescription;
char @startLabel;
fbPtr = g_Statement[statemNum].mathSectionPtr;
if (!fbPtr[0]) return (description);
startLabel = g_Statement[statemNum].labelSectionPtr;
if (!startLabel[0]) return (description);
endDescription = NULL;
while (1) { /@ Get end of embedded comment @/
if (fbPtr <= startLabel) break;
if (fbPtr[0] == '$' && fbPtr[1] == ')') {
endDescription = fbPtr;
break;
}
fbPtr--;
}
if (!endDescription) return (description); /@ No embedded comment @/
while (1) { /@ Get start of embedded comment @/
if (fbPtr < startLabel) bug(216);
if (fbPtr[0] == '$' && fbPtr[1] == '(') {
startDescription = fbPtr + 2;
break;
}
fbPtr--;
}
let(&description, space(endDescription - startDescription));
memcpy(description, startDescription,
(size_t)(endDescription - startDescription));
if (description[endDescription - startDescription - 1] == '\n') {
/@ Trim trailing new line @/
let(&description, left(description, endDescription - startDescription - 1));
}
/@ Discard leading and trailing blanks @/
let(&description, edit(description, 8 + 128));
return (description);
*********** end of 3-May-2017 deletion *****/
} /* getDescription */
/* 24-Aug-2020 nm */
/* Returns the label section of a statement with all comments except the
last removed. Unlike getDescription, this function returns the comment
surrounded by $( and $) as well as the leading indentation space
and everything after this comment (such as the actual label).
Since this is used for arbitrary (other than $a, $p) statements by the
EXPAND command, we also suppress section headers if they are the last
comment. The caller must deallocate the result. */
vstring getDescriptionAndLabel(long stmt) {
vstring descriptionAndLabel = "";
long p1, p2;
flag dontUseComment = 0; /* 12-Sep-2020 nm */
let(&descriptionAndLabel, space(g_Statement[stmt].labelSectionLen));
memcpy(descriptionAndLabel, g_Statement[stmt].labelSectionPtr,
(size_t)(g_Statement[stmt].labelSectionLen));
p1 = rinstr(descriptionAndLabel, "$(");
p2 = rinstr(descriptionAndLabel, "$)");
if (p1 == 0 || p2 == 0 || p2 < p1) {
/* The statement has no comment; just return the label and
surrounding spacing if any */
return descriptionAndLabel;
}
/* Search backwards for non-space or beginning of string */
p1--;
while (p1 != 0) {
if (descriptionAndLabel[p1 - 1] != ' '
&& descriptionAndLabel[p1 - 1] != '\n') break;
p1--;
}
let(&descriptionAndLabel, right(descriptionAndLabel, p1 + 1));
/* Ignore descriptionAndLabels that are section headers */
/* TODO: make this more precise here and in mmwtex.c - use 79-char decorations? */
if (instr(1, descriptionAndLabel, cat("\n", TINY_DECORATION, NULL)) != 0
|| instr(1, descriptionAndLabel, cat("\n", SMALL_DECORATION, NULL)) != 0
|| instr(1, descriptionAndLabel, cat("\n", BIG_DECORATION, NULL)) != 0
|| instr(1, descriptionAndLabel, cat("\n", HUGE_DECORATION, NULL)) != 0) {
/*let(&descriptionAndLabel, "");*/
dontUseComment = 1; /* 12-Sep-2020 nm */
}
/* Remove comments with file inclusion markup */
if (instr(1, descriptionAndLabel, "$[") != 0) {
/*let(&descriptionAndLabel, "");*/
dontUseComment = 1; /* 12-Sep-2020 nm */
}
/* Remove comments with $j markup */
if (instr(1, descriptionAndLabel, "$j") != 0) {
/*let(&descriptionAndLabel, "");*/
dontUseComment = 1; /* 12-Sep-2020 nm */
}
/****** deleted 12-Sep-2020
/@ If the cleaned description is empty, e.g. ${ after a header,
add in spaces corresponding to the scope @/
if (descriptionAndLabel[0] == 0) {
let(&descriptionAndLabel, space(2 * (g_Statement[stmt].scope + 1)));
}
*******/
if (dontUseComment == 1) {
/* Get everything that follows the comment */
p2 = rinstr(descriptionAndLabel, "$)");
if (p2 == 0) bug(1401); /* Should have exited earlier if no "$)" */
let(&descriptionAndLabel, right(descriptionAndLabel, p2 + 2));
}
return descriptionAndLabel;
} /* getDescriptionAndLabel */
/**** Deleted 12-Sep-2020 nm
/@ 24-Aug-2020 nm @/
/@ Reconstruct the full header from the strings returned by
getSectionHeadings(). The caller should deallocate the returned string. @/
/@ getSectionHeadings() currently return strings extracted from headers,
but not the full header needed for writeExtractedSource(). Maybe we
should have it return the full header in the future, but for now this
function reconstructs the full header. @/
vstring buildHeader(vstring header, vstring hdrComment, vstring decoration) {
long i;
vstring fullDecoration = "";
vstring fullHeader = "";
/@ The HUGE_DECORATION etc. have only the 1st 4 chars of the full line.
Build the full line. @/
let(&fullDecoration, "");
for (i = 1; i <= 5; i++) {
let(&fullDecoration, cat(fullDecoration, decoration, decoration,
decoration, decoration, NULL));
}
let(&fullDecoration, left(fullDecoration, 79));
i = (long)strlen(hdrComment);
let(&fullHeader, cat("\n\n$(\n", fullDecoration, "\n",
space((79 - (long)strlen(header))/2), header, "\n", fullDecoration,
"\n", hdrComment,
(i == 0) ? "$)\n" :
(hdrComment[i - 1] == ' ') ? "$)\n" : "\n$)\n",
NULL));
let(&fullDecoration, "");
return fullHeader;
} /@ buildHeader @/
*******/
/* Returns 0 or 1 to indicate absence or presence of an indicator in
the comment of the statement. */
/* mode = 1 = PROOF_DISCOURAGED means get any proof modification discouraged
indicator
mode = 2 = USAGE_DISCOURAGED means get any new usage discouraged indicator
mode = 0 = RESET means to reset everything (statemeNum is ignored) */
/* TODO: add a mode to reset a single statement if in the future we add
the ability to change the markup within the program. */
flag getMarkupFlag(long statemNum, flag mode) {
/* For speedup, the algorithm searches a statement's comment for markup
matches only the first time, then saves the result for subsequent calls
for that statement. */
static char init = 0;
static vstring commentSearchedFlags = ""; /* Y if comment was searched */
static vstring proofFlags = ""; /* Y if proof discouragement, else N */
static vstring usageFlags = ""; /* Y if usage discouragement, else N */
vstring str1 = "";
/* These are global in mmdata.h
#define PROOF_DISCOURAGED_MARKUP "(Proof modification is discouraged.)"
#define USAGE_DISCOURAGED_MARKUP "(New usage is discouraged.)"
extern vstring g_proofDiscouragedMarkup;
extern vstring g_usageDiscouragedMarkup;
*/
if (mode == RESET) { /* Deallocate */ /* Should be called by ERASE command */
let(&commentSearchedFlags, "");
let(&proofFlags, "");
let(&usageFlags, "");
init = 0;
return 0;
}
if (init == 0) {
init = 1;
/* The global variables g_proofDiscouragedMarkup and g_usageDiscouragedMarkup
are initialized to "" like all vstrings to allow them to be reassigned
by a possible future SET command. So the first time this is called
we need to assign them to the default markup strings. */
if (g_proofDiscouragedMarkup[0] == 0) {
let(&g_proofDiscouragedMarkup, PROOF_DISCOURAGED_MARKUP);
}
if (g_usageDiscouragedMarkup[0] == 0) {
let(&g_usageDiscouragedMarkup, USAGE_DISCOURAGED_MARKUP);
}
/* Initialize flag strings */
let(&commentSearchedFlags, string(g_statements + 1, 'N'));
let(&proofFlags, space(g_statements + 1));
let(&usageFlags, space(g_statements + 1));
}
if (statemNum < 1 || statemNum > g_statements) bug(1392);
if (commentSearchedFlags[statemNum] == 'N') {
if (g_Statement[statemNum].type == f_
|| g_Statement[statemNum].type == e_ /* 24-May-2016 nm */ ) {
/* Any comment before a $f, $e statement is assumed irrelevant */
proofFlags[statemNum] = 'N';
usageFlags[statemNum] = 'N';
} else {
if (g_Statement[statemNum].type != a_ && g_Statement[statemNum].type != p_) {
bug(1393);
}
str1 = getDescription(statemNum); /* str1 must be deallocated here */
/* Strip linefeeds and reduce spaces */
let(&str1, edit(str1, 4 + 8 + 16 + 128));
if (instr(1, str1, g_proofDiscouragedMarkup)) {
proofFlags[statemNum] = 'Y';
} else {
proofFlags[statemNum] = 'N';
}
if (instr(1, str1, g_usageDiscouragedMarkup)) {
usageFlags[statemNum] = 'Y';
} else {
usageFlags[statemNum] = 'N';
}
let(&str1, ""); /* Deallocate */
}
commentSearchedFlags[statemNum] = 'Y';
}
if (mode == PROOF_DISCOURAGED) return (proofFlags[statemNum] == 'Y') ? 1 : 0;
if (mode == USAGE_DISCOURAGED) return (usageFlags[statemNum] == 'Y') ? 1 : 0;
bug(1394);
return 0;
} /* getMarkupFlag */
/* 7-Nov-2015 nm */
/* Extract contributor or date from statement description per the
following mode argument:
CONTRIBUTOR 1
CONTRIB_DATE 2
REVISER 3
REVISE_DATE 4
SHORTENER 5
SHORTEN_DATE 6
MOST_RECENT_DATE 7
When an item above is missing, the empty string is returned for that item.
The following utility modes are available:
GC_ERROR_CHECK_SILENT 8
GC_ERROR_CHECK_PRINT 9
GC_RESET 0
GC_RESET_STMT 10
For GC_ERROR_CHECK_SILENT and GC_ERROR_CHECK_PRINT, a "F" is returned if
error-checking fails, otherwise "P" is returned. GC_ERROR_CHECK_PRINT also
prints the errors found.
GC_RESET clears the cache and returns the empty string. It is normally
used by the ERASE command. The stmtNum argument should be 0. The
empty string is returned.
GC_RESET_STMT re-initializes the cache for the specified statement only.
It should be called whenever the labelSection is changed e.g. by
SAVE PROOF. The empty string is returned.
*/
/* 3-May-2017 nm Changed to return a single result each call, to allow
expandability in the future */
/* The caller must deallocate the returned string. */
vstring getContrib(long stmtNum, char mode) {
/******** deleted 3-May-2017
flag getContrib(long stmtNum,
vstring @contributor, vstring @contribDate,
vstring @reviser, vstring @reviseDate,
vstring @shortener, vstring @shortenDate,
vstring @mostRecentDate, /@ The most recent of all 3 dates @/
flag printErrorsFlag,
flag mode /@ 0 == RESET = reset, 1 = normal @/ /@ 2-May-2017 nm @/) {
*******/
/* 2-May-2017 nm */
/* For speedup, the algorithm searches a statement's comment for markup
matches only the first time, then saves the result for subsequent calls
for that statement. */
static char init = 0;
vstring contributor = "";
vstring contribDate = "";
vstring reviser = "";
vstring reviseDate = "";
vstring shortener = "";
vstring shortenDate = "";
vstring mostRecentDate = ""; /* The most recent of all 3 dates */
static vstring commentSearchedFlags = ""; /* Y if comment was searched */
static pntrString *contributorList = NULL_PNTRSTRING;
static pntrString *contribDateList = NULL_PNTRSTRING;
static pntrString *reviserList = NULL_PNTRSTRING;
static pntrString *reviseDateList = NULL_PNTRSTRING;
static pntrString *shortenerList = NULL_PNTRSTRING;
static pntrString *shortenDateList = NULL_PNTRSTRING;
static pntrString *mostRecentDateList = NULL_PNTRSTRING;
long cStart = 0, cMid = 0, cEnd = 0;
long rStart = 0, rMid = 0, rEnd = 0;
long sStart = 0, sMid = 0, sEnd = 0;
long firstR = 0, firstS = 0;
vstring description = "";
vstring tmpDate0 = "";
vstring tmpDate1 = "";
vstring tmpDate2 = "";
long stmt, p, dd, mmm, yyyy;
flag errorCheckFlag = 0;
flag err = 0;
vstring returnStr = ""; /* Return value */
#define CONTRIB_MATCH " (Contributed by "
#define REVISE_MATCH " (Revised by "
#define SHORTEN_MATCH " (Proof shortened by "
#define END_MATCH ".) "
/* 3-May-2017 nm */
if (mode == GC_ERROR_CHECK_SILENT || mode == GC_ERROR_CHECK_PRINT) {
errorCheckFlag = 1;
}
/* 2-May-2017 nm */
if (mode == GC_RESET) {
/* This is normally called by the ERASE command only */
if (init != 0) {
if ((long)strlen(commentSearchedFlags) != g_statements + 1) {
bug(1395);
}
if (stmtNum != 0) {
bug(1400);
}
for (stmt = 1; stmt <= g_statements; stmt++) {
if (commentSearchedFlags[stmt] == 'Y') {
/* Deallocate cached strings */
let((vstring *)(&(contributorList[stmt])), "");
let((vstring *)(&(contribDateList[stmt])), "");
let((vstring *)(&(reviserList[stmt])), "");
let((vstring *)(&(reviseDateList[stmt])), "");
let((vstring *)(&(shortenerList[stmt])), "");
let((vstring *)(&(shortenDateList[stmt])), "");
let((vstring *)(&(mostRecentDateList[stmt])), "");
}
}
/* Deallocate the lists of pointers to cached strings */
pntrLet(&contributorList, NULL_PNTRSTRING);
pntrLet(&contribDateList, NULL_PNTRSTRING);
pntrLet(&reviserList, NULL_PNTRSTRING);
pntrLet(&reviseDateList, NULL_PNTRSTRING);
pntrLet(&shortenerList, NULL_PNTRSTRING);
pntrLet(&shortenDateList, NULL_PNTRSTRING);
pntrLet(&mostRecentDateList, NULL_PNTRSTRING);
let(&commentSearchedFlags, "");
init = 0;
} /* if (init != 0) */
return "";
}
/* 3-May-2017 nm */
if (mode == GC_RESET_STMT) {
/* This should be called whenever the labelSection is changed e.g. by
SAVE PROOF. */
if (init != 0) {
if ((long)strlen(commentSearchedFlags) != g_statements + 1) {
bug(1398);
}
if (stmtNum < 1 || stmtNum > g_statements + 1) {
bug(1399);
}
if (commentSearchedFlags[stmtNum] == 'Y') {
/* Deallocate cached strings */
let((vstring *)(&(contributorList[stmtNum])), "");
let((vstring *)(&(contribDateList[stmtNum])), "");
let((vstring *)(&(reviserList[stmtNum])), "");
let((vstring *)(&(reviseDateList[stmtNum])), "");
let((vstring *)(&(shortenerList[stmtNum])), "");
let((vstring *)(&(shortenDateList[stmtNum])), "");
let((vstring *)(&(mostRecentDateList[stmtNum])), "");
commentSearchedFlags[stmtNum] = 'N';
}
} /* if (init != 0) */
return "";
}
/* We now check only $a and $p statements - should we do others? */
if (g_Statement[stmtNum].type != a_ && g_Statement[stmtNum].type != p_) {
goto RETURN_POINT;
}
if (init == 0) {
init = 1;
/* Initialize flag string */
let(&commentSearchedFlags, string(g_statements + 1, 'N'));
/* Initialize pointers to "" (null vstring) */
pntrLet(&contributorList, pntrSpace(g_statements + 1));
pntrLet(&contribDateList, pntrSpace(g_statements + 1));
pntrLet(&reviserList, pntrSpace(g_statements + 1));
pntrLet(&reviseDateList, pntrSpace(g_statements + 1));
pntrLet(&shortenerList, pntrSpace(g_statements + 1));
pntrLet(&shortenDateList, pntrSpace(g_statements + 1));
pntrLet(&mostRecentDateList, pntrSpace(g_statements + 1));
}
if (stmtNum < 1 || stmtNum > g_statements) bug(1396);
if (commentSearchedFlags[stmtNum] == 'N' /* Not in cache */
|| errorCheckFlag == 1 /* Needed to get sStart, rStart, cStart */) {
/* It wasn't cached, so we extract from the statement's comment */
let(&description, "");
description = getDescription(stmtNum);
let(&description, edit(description,
4/*ctrl*/ + 8/*leading*/ + 16/*reduce*/ + 128/*trailing*/));
let(&description, cat(" ", description, " ", NULL)); /* Add for matching */
cStart = instr(1, description, CONTRIB_MATCH);
if (cStart != 0) {
cStart = cStart + (long)strlen(CONTRIB_MATCH); /* Start of contributor */
cEnd = instr(cStart, description, END_MATCH); /* End of date */
cMid = cEnd; /* After end of contributor and before start of date */
if (cMid != 0) {
while (description[cMid - 1] != ' ') {
cMid--;
if (cMid == 0) break;
}
}
/* We assign contributorList entry instead of contributor,
contribDateList entry instead of contribDate, etc. in case the
same string variable is used for several arguments for convenience
(e.g. to avoid having to declare 7 string variables if only one date
is needed) */
let((vstring *)(&(contributorList[stmtNum])),
seg(description, cStart, cMid - 2));
let((vstring *)(&(contribDateList[stmtNum])),
seg(description, cMid + 1, cEnd - 1));
} else {
/* The contributorList etc. are already initialized to the empty
string, so we don't have to assign them here. */
/*
let((vstring *)(&(contributorList[stmtNum])), "");
let((vstring *)(&(contribDateList[stmtNum])), "");
*/
}
rStart = 0;
do { /* Get the last revision entry */
p = instr(rStart + 1, description, REVISE_MATCH);
if (p != 0) {
rStart = p;
if (firstR == 0) firstR = p + (long)strlen(REVISE_MATCH);
/* Add the strlen so to later compare to rStart */
}
} while (p != 0);
if (rStart != 0) {
rStart = rStart + (long)strlen(REVISE_MATCH); /* Start of reviser */
rEnd = instr(rStart, description, END_MATCH); /* End of date */
rMid = rEnd; /* After end of reviser and before start of date */
if (rMid != 0) {
while (description[rMid - 1] != ' ') {
rMid--;
if (rMid == 0) break;
}
}
let((vstring *)(&(reviserList[stmtNum])),
seg(description, rStart, rMid - 2));
let((vstring *)(&(reviseDateList[stmtNum])),
seg(description, rMid + 1, rEnd - 1));
} else {
/* redundant; already done by init
let((vstring *)(&(reviserList[stmtNum])), "");
let((vstring *)(&(reviseDateList[stmtNum])), "");
*/
}
sStart = 0;
do { /* Get the last shorten entry */
p = instr(sStart + 1, description, SHORTEN_MATCH);
if (p != 0) {
sStart = p;
if (firstS == 0) firstS = p + (long)strlen(SHORTEN_MATCH);
/* Add the strlen so to later compare to rStart */
}
} while (p != 0);
if (sStart != 0) {
sStart = sStart + (long)strlen(SHORTEN_MATCH); /* Start of shortener */
sEnd = instr(sStart, description, END_MATCH); /* End of date */
sMid = sEnd; /* After end of shortener and before start of date */
if (sMid != 0) {
while (description[sMid - 1] != ' ') {
sMid--;
if (sMid == 0) break;
}
}
let((vstring *)(&(shortenerList[stmtNum])),
seg(description, sStart, sMid - 2));
let((vstring *)(&(shortenDateList[stmtNum])),
seg(description, sMid + 1, sEnd - 1));
} else {
/* redundant; already done by init
let((vstring *)(&(shortenerList[stmtNum])), "");
let((vstring *)(&(shortenDateList[stmtNum])), "");
*/
}
/* 13-Dec-2016 nm Get the most recent date */
let((vstring *)(&(mostRecentDateList[stmtNum])),
(vstring)(contribDateList[stmtNum]));
/* Note that compareDate() treats empty string as earliest date */
if (compareDates((vstring)(mostRecentDateList[stmtNum]),
(vstring)(reviseDateList[stmtNum])) == -1) {
let((vstring *)(&(mostRecentDateList[stmtNum])),
(vstring)(reviseDateList[stmtNum]));
}
if (compareDates((vstring)(mostRecentDateList[stmtNum]),
(vstring)(shortenDateList[stmtNum])) == -1) {
let((vstring *)(&(mostRecentDateList[stmtNum])),
(vstring)(shortenDateList[stmtNum]));
}
/* 2-May-2017 nm Tag the cache entry as updated */
commentSearchedFlags[stmtNum] = 'Y';
} /* commentSearchedFlags[stmtNum] == 'N' || errorCheckFlag == 1 */
/* Assign the output strings from the cache */
if (errorCheckFlag == 1) {
let(&contributor, (vstring)(contributorList[stmtNum]));
let(&contribDate, (vstring)(contribDateList[stmtNum]));
let(&reviser, (vstring)(reviserList[stmtNum]));
let(&reviseDate, (vstring)(reviseDateList[stmtNum]));
let(&shortener, (vstring)(shortenerList[stmtNum]));
let(&shortenDate, (vstring)(shortenDateList[stmtNum]));
let(&mostRecentDate, (vstring)(mostRecentDateList[stmtNum]));
} else {
/* Assign only the requested field for faster speed */
switch (mode) {
case CONTRIBUTOR:
let(&returnStr, (vstring)(contributorList[stmtNum])); break;
case CONTRIB_DATE:
let(&returnStr, (vstring)(contribDateList[stmtNum])); break;
case REVISER:
let(&returnStr, (vstring)(reviserList[stmtNum])); break;
case REVISE_DATE:
let(&returnStr, (vstring)(reviseDateList[stmtNum])); break;
case SHORTENER:
let(&returnStr, (vstring)(shortenerList[stmtNum])); break;
case SHORTEN_DATE:
let(&returnStr, (vstring)(shortenDateList[stmtNum])); break;
case MOST_RECENT_DATE:
let(&returnStr, (vstring)(mostRecentDateList[stmtNum])); break;
default: bug(1397); /* Any future modes should be added here */
} /* end switch (mode) */
}
/* Skip error checking for speedup if we're not printing errors */
if (errorCheckFlag == 0) goto RETURN_POINT;
/* For error checking, we don't require dates in syntax statements
(**** Note that this is set.mm-specific! ****) */
if (g_Statement[stmtNum].type == a_ /* Don't check syntax statements */
&& strcmp(left(g_Statement[stmtNum].labelName, 3), "df-")
&& strcmp(left(g_Statement[stmtNum].labelName, 3), "ax-")) {
goto RETURN_POINT;
}
if (cStart == 0) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: There is no \"", edit(CONTRIB_MATCH, 8+128),
"...)\" in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (instr(cStart + 1, description, CONTRIB_MATCH) != 0) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: There is more than one \"", edit(CONTRIB_MATCH, 8+128),
"...)\" ",
"in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
/* 3-May-2017 nm */
if (cStart != 0 && description[cMid - 2] != ',') {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
"?Warning: There is no comma between contributor and date",
", or period is missing,", /* 5-Aug-2017 nm */
" in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (rStart != 0 && description[rMid - 2] != ',') {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
"?Warning: There is no comma between reviser and date",
", or period is missing,", /* 5-Aug-2017 nm */
" in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (sStart != 0 && description[sMid - 2] != ',') {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
"?Warning: There is no comma between proof shortener and date",
", or period is missing,", /* 5-Aug-2017 nm */
" in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (instr(1, contributor, ",") != 0) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
"?Warning: There is a comma in the contributor name \"",
contributor,
"\" in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (instr(1, reviser, ",") != 0) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
"?Warning: There is a comma in the reviser name \"",
reviser,
"\" in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (instr(1, shortener, ",") != 0) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
"?Warning: There is a comma in the proof shortener name \"",
shortener,
"\" in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
/********* Turn off this warning unless we decide not to allow this
if ((firstR != rStart) || (firstS != sStart)) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/@ convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
@contributor, "/", @reviser, "/", @shortener, "] ",
@/
"?Warning: There are multiple \"",
edit(REVISE_MATCH, 8+128) , "...)\" or \"",
edit(SHORTEN_MATCH, 8+128) ,
"...)\" entries in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
" The last one of each type was used.",
NULL), " ", " ");
}
*********/
if ((firstR != 0 && firstR < cStart)
|| (firstS != 0 && firstS < cStart)) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: \"", edit(CONTRIB_MATCH, 8+128),
"...)\" is placed after \"",
edit(REVISE_MATCH, 8+128) , "...)\" or \"",
edit(SHORTEN_MATCH, 8+128) ,
"...)\" in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if ((cStart !=0 && (cMid == 0 || cEnd == 0 || cMid == cEnd
|| contributor[0] == 0 || contribDate[0] == 0))
|| (rStart !=0 && (rMid == 0 || rEnd == 0 || rMid == rEnd
|| reviser[0] == 0 || reviseDate[0] == 0))
|| (sStart !=0 && (sMid == 0 || sEnd == 0 || sMid == sEnd
|| shortener[0] == 0 || shortenDate[0] == 0))) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: There is a formatting error in a",
" \"", edit(CONTRIB_MATCH, 8+128), "...)\", \"",
edit(REVISE_MATCH, 8+128) , "...)\", or \"",
edit(SHORTEN_MATCH, 8+128),
"...)\" entry in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (contribDate[0] != 0) {
parseDate(contribDate, &dd, &mmm, &yyyy);
buildDate(dd, mmm, yyyy, &tmpDate0);
if (strcmp(contribDate, tmpDate0)) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: There is a formatting error in the \"",
edit(CONTRIB_MATCH, 8+128), "...)\" date \"", contribDate, "\""
" in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
}
if (reviseDate[0] != 0) {
parseDate(reviseDate, &dd, &mmm, &yyyy);
buildDate(dd, mmm, yyyy, &tmpDate0);
if (strcmp(reviseDate, tmpDate0)) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: There is a formatting error in the \"",
edit(REVISE_MATCH, 8+128) , "...)\" date \"", reviseDate, "\""
" in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
}
if (shortenDate[0] != 0) {
parseDate(shortenDate, &dd, &mmm, &yyyy);
buildDate(dd, mmm, yyyy, &tmpDate0);
if (strcmp(shortenDate, tmpDate0)) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: There is a formatting error in the \"",
edit(SHORTEN_MATCH, 8+128) , "...)\" date \"", shortenDate, "\""
" in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
}
if (contribDate[0] != 0 &&
((reviseDate[0] != 0
&& compareDates(contribDate, reviseDate) != -1)
|| (shortenDate[0] != 0
&& compareDates(contribDate, shortenDate) != -1))) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: The \"", edit(CONTRIB_MATCH, 8+128),
"...)\" date is not earlier than the \"",
edit(REVISE_MATCH, 8+128), "...)\" or \"",
edit(SHORTEN_MATCH, 8+128),
"...)\" date in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (reviseDate[0] != 0 && shortenDate[0] != 0) {
p = compareDates(reviseDate, shortenDate);
if ((rStart < sStart && p == 1)
|| (rStart > sStart && p == -1)) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: The \"", edit(REVISE_MATCH, 8+128), "...)\" and \"",
edit(SHORTEN_MATCH, 8+128),
"...)\" dates are in the wrong order in the comment above statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
}
#ifdef DATE_BELOW_PROOF /* 12-May-2017 nm */
/* TODO ******** The rest of the checks should be deleted if we decide
to drop the date after the proof */
if (g_Statement[stmtNum].type != p_) {
goto RETURN_POINT;
}
getProofDate(stmtNum, &tmpDate1, &tmpDate2);
if (tmpDate1[0] == 0) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: There is no date below the proof in statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (tmpDate2[0] == 0
&& (reviseDate[0] != 0 || shortenDate[0] != 0)) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: The comment has \"",
edit(REVISE_MATCH, 8+128), "...)\" or \"",
edit(SHORTEN_MATCH, 8+128),
"...)\" but there is only one date below the proof",
" in statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (tmpDate2[0] != 0 && reviseDate[0] == 0 && shortenDate[0] == 0) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: There are two dates below the proof but no \"",
edit(REVISE_MATCH, 8+128), "...)\" or \"",
edit(SHORTEN_MATCH, 8+128),
"...)\" entry in statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (tmpDate2[0] != 0
&& (reviseDate[0] != 0 || shortenDate[0] != 0)
&& strcmp(tmpDate1, reviseDate)
&& strcmp(tmpDate1, shortenDate)) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: Neither a \"",
edit(REVISE_MATCH, 8+128), "...)\" date ",
"nor a \"", edit(SHORTEN_MATCH, 8+128), "...)\" date ",
"matches the date ", tmpDate1,
" below the proof in statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (tmpDate2[0] != 0
&& reviseDate[0] != 0
&& compareDates(tmpDate1, reviseDate) == -1) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: The \"",
edit(REVISE_MATCH, 8+128), "...)\" date ", reviseDate,
" is later than the date ", tmpDate1,
" below the proof in statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (tmpDate2[0] != 0
&& shortenDate[0] != 0
&& compareDates(tmpDate1, shortenDate) == -1) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: The \"",
edit(SHORTEN_MATCH, 8+128), "...)\" date ", shortenDate,
" is later than the date ", tmpDate1,
" below the proof in statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (tmpDate2[0] != 0 && compareDates(tmpDate2, tmpDate1) != -1) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: The first date below the proof, ", tmpDate1,
", is not newer than the second, ", tmpDate2,
", in statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
if (tmpDate2[0] == 0) {
let(&tmpDate0, tmpDate1);
} else {
let(&tmpDate0, tmpDate2);
}
if (contribDate[0] != 0
&& tmpDate0[0] != 0 && strcmp(contribDate, tmpDate0)) {
err = 1;
if (mode == GC_ERROR_CHECK_PRINT) printLongLine(cat(
/* convenience prefix to assist massive revisions
g_Statement[stmtNum].labelName, " [",
contributor, "/", reviser, "/", shortener, "] ",
*/
"?Warning: The \"", edit(CONTRIB_MATCH, 8+128), "...)\" date ",
contribDate,
" doesn't match the date ", tmpDate0,
" below the proof in statement ",
str((double)stmtNum), ", label \"", g_Statement[stmtNum].labelName, "\".",
NULL), " ", " ");
}
/***** End of section to delete if date after proof is dropped */
#endif /* #ifdef DATE_BELOW_PROOF */
if (err == 1) {
let(&returnStr, "F"); /* fail */
} else {
let(&returnStr, "P"); /* pass */
}
RETURN_POINT:
let(&description, "");
if (errorCheckFlag == 1) { /* Slight speedup */
let(&contributor, "");
let(&contribDate, "");
let(&reviser, "");
let(&reviseDate, "");
let(&shortener, "");
let(&shortenDate, "");
let(&mostRecentDate, "");
let(&tmpDate0, "");
let(&tmpDate1, "");
let(&tmpDate2, "");
}
return returnStr;
} /* getContrib */
/*#ifdef DATE_BELOW_PROOF*/ /* 12-May-2017 nm */
/* 14-May-2017 nm - re-enabled (temporarily?) for converting old .mm's */
/* 4-Nov-2015 nm */
/* Extract up to 2 dates after a statement's proof. If no date is present,
date1 will be blank. If no 2nd date is present, date2 will be blank.
THIS WILL BECOME OBSOLETE WHEN WE START TO USE DATES IN THE
DESCRIPTION. */
void getProofDate(long stmtNum, vstring *date1, vstring *date2) {
vstring textAfterProof = "";
long p1, p2;
let(&textAfterProof, space(g_Statement[stmtNum + 1].labelSectionLen));
memcpy(textAfterProof, g_Statement[stmtNum + 1].labelSectionPtr,
(size_t)(g_Statement[stmtNum + 1].labelSectionLen));
let(&textAfterProof, edit(textAfterProof, 2)); /* Discard spaces and tabs */
p1 = instr(1, textAfterProof, "$([");
p2 = instr(p1, textAfterProof, "]$)");
if (p1 && p2) {
let(&(*date1), seg(textAfterProof, p1 + 3, p2 - 1)); /* 1st date stamp */
p1 = instr(p2, textAfterProof, "$([");
p2 = instr(p1, textAfterProof, "]$)");
if (p1 && p2) {
let(&(*date2), seg(textAfterProof, p1 + 3, p2 - 1)); /* 2nd date stamp */
} else {
let(&(*date2), ""); /* No 2nd date stamp */
}
} else {
let(&(*date1), ""); /* No 1st or 2nd date stamp */
let(&(*date2), "");
}
let(&textAfterProof, ""); /* Deallocate */
return;
} /* getProofDate */
/*#endif*/ /*#ifdef DATE_BELOW_PROOF*/ /* 12-May-2017 nm */
/* 4-Nov-2015 nm */
/* Get date, month, year fields from a dd-mmm-yyyy date string,
where dd may be 1 or 2 digits, mmm is 1st 3 letters of month,
and yyyy is 2 or 4 digits. A 1 is returned if an error was detected. */
flag parseDate(vstring dateStr, long *dd, long *mmm, long *yyyy) {
long j;
flag err = 0;
j = instr(1, dateStr, "-");
*dd = (long)val(left(dateStr, j - 1)); /* Day */
#define MONTHS "JanFebMarAprMayJunJulAugSepOctNovDec"
*mmm = ((instr(1, MONTHS, mid(dateStr, j + 1, 3)) - 1) / 3) + 1; /* 1 = Jan */
j = instr(j + 1, dateStr, "-");
*yyyy = (long)val(right(dateStr, j + 1));
if (*yyyy < 100) { /* 2-digit year (obsolete) */
#define START_YEAR 93 /* Earliest 19xx year in set.mm database */
if (*yyyy < START_YEAR) {
*yyyy = *yyyy + 2000;
} else {
*yyyy = *yyyy + 1900;
}
}
if (*dd < 1 || *dd > 31 || *mmm < 1 || *mmm > 12) err = 1; /* 13-Dec-2016 nm */
return err;
} /* parseDate */
/* 4-Nov-2015 nm */
/* Build date from numeric fields. mmm should be a number from 1 to 12.
There is no error-checking. */
void buildDate(long dd, long mmm, long yyyy, vstring *dateStr) {
let(&(*dateStr), cat(str((double)dd), "-", mid(MONTHS, mmm * 3 - 2, 3), "-",
str((double)yyyy), NULL));
return;
} /* buildDate */
/* 4-Nov-2015 nm */
/* Compare two dates in the form dd-mmm-yyyy. -1 = date1 < date2,
0 = date1 = date2, 1 = date1 > date2. There is no error checking. */
flag compareDates(vstring date1, vstring date2) {
long d1, m1, y1, d2, m2, y2, dd1, dd2;
/* 13-Dec-2016 nm */
/* If a date is the empty string, treat it as being _before_ any other
date */
if (date1[0] == 0 || date2[0] == 0) {
if (date1[0] == 0 && date2[0] == 0) {
return 0;
} else if (date1[0] == 0) {
return -1;
} else {
return 1;
}
}
parseDate(date1, &d1, &m1, &y1);
parseDate(date2, &d2, &m2, &y2);
/* dd1, dd2 increase monotonically but aren't true days since 1-Jan-0000 */
dd1 = d1 + m1 * 32 + y1 * 500;
dd2 = d2 + m2 * 32 + y2 * 500;
if (dd1 < dd2) {
return -1;
} else if (dd1 == dd2) {
return 0;
} else {
return 1;
}
} /* compareDates */
/* 17-Nov-2015 Moved out of metamath.c for better modularization */
/* Compare strings via pointers for qsort */
/* g_qsortKey is a global string key at which the sort starts; if empty,
start at the beginning of each line. */
int qsortStringCmp(const void *p1, const void *p2)
{
vstring tmp = "";
long n1, n2;
int r;
/* Returns -1 if p1 < p2, 0 if equal, 1 if p1 > p2 */
if (g_qsortKey[0] == 0) {
/* No key, use full line */
return strcmp(*(char * const *)p1, *(char * const *)p2);
} else {
n1 = instr(1, *(char * const *)p1, g_qsortKey);
n2 = instr(1, *(char * const *)p2, g_qsortKey);
r = strcmp(
right(*(char * const *)p1, n1),
right(*(char * const *)p2, n2));
let(&tmp, ""); /* Deallocate temp string stack */
return r;
}
}
/* 4-May-2017 Ari Ferrera */
void freeData() {
/* 15-Aug-2020 nm TODO: are some of these called twice? (in eraseSource) */
free(g_IncludeCall);
free(g_Statement);
free(g_MathToken);
free(memFreePool);
free(memUsedPool);
}
|