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 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252
|
/************************************************************************/
/* Extract v4.0.0 */
/* (C) Copyright 1994,2015 R. Clint Whaley (rwhaley@cs.utk.edu). */
/* This program is distributed under the terms of the Gnu */
/* General Public License (GPL), with the following two exceptions: */
/* (1) Clause (9), dealing with updating the GPL automatically, is */
/* specifically disallowed by the author. The author will */
/* determine if a newer GPL version is still appropriate. */
/* (2) The basefiles extract accepts as input, and the extracted */
/* files it produces as output, are specifically designated as */
/* as outside the scope if this license (i.e. they are *not* */
/* required by this license to be GPL). */
/* The full, unaltered, text of the GPL is included at the end of */
/* the program source listing. */
/* ------------------------------------------------------------------ */
/* Last modified by the author on 11/20/15. */
/************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#define NAMLEN 1024
#define LNLEN 8192
#define HANLEN 1024
#define SUBLEN 8192
#define F_nFlags 16
#define F_Case 0
#define F_RemBlank 1
#define F_Lang 2
#define F_Ugly 3
#define F_Verb 4
#define F_RepTab 5
#define F_LnLen 6
#define F_Append 7
#define F_TransNum 8
#define F_Query 9
#define F_AddKeys 10
#define F_Clint 11
#define F_FragMac 12
#define F_FragFlag 13
#define F_LLWarn 14
#define F_LocalProcs 15
#define EC_nExtCmnds 20
#define EC_Extract 0
#define EC_Skip 1
#define EC_Define 2
#define EC_Indent 3
#define EC_Key 4
#define EC_Abort 5
#define EC_MacSub 6
#define EC_EndExt 7
#define EC_Print 8
#define EC_Exp 9
#define EC_While 10
#define EC_Ifdef 11
#define EC_Dec 12
#define EC_AddKey 13
#define EC_Echo 14
#define EC_Iwhile 15
#define EC_Proc 16
#define EC_Output 17
#define EC_Iif 18
#define EC_Mif 19
#define MODE_IIF 1 /* integer if mode */
#define MODE_MIF 2 /* macro if mode */
/*
* Mode count numbers
*/
#define NMC 2
#define MCIIF 0
#define MCMIF 1
/*
* KILLMODE must be INMODE or it sets instead
*/
#define INMODE(modenv_, which_) ( (modenv_) | (which_) == (modenv_) )
#define SETMODE(modenv_, which_) (modenv_) = (modenv_) | (which_)
#define KILLMODE(modenv_, which_) (modenv_) = (modenv_) ^ (which_)
enum LANG {LangC, LangF77, LangF90, LangMake};
typedef struct fIlE2 FILE2;
struct fIlE2
{
char Fnam[NAMLEN];
unsigned int LineNo;
FILE *Fp;
FILE2 *prev;
};
typedef struct aRgStAcK ARGSTACK;
struct aRgStAcK
{
char KeyArgs[LNLEN];
int Not;
ARGSTACK *next;
};
typedef struct eXtMaC EXTMAC;
struct eXtMaC
{
int HanLen, SubLen;
char *Handle;
char *Sub;
EXTMAC *next;
};
typedef struct pUnYmAc PUNYMAC;
struct pUnYmAc
{
EXTMAC *mp;
PUNYMAC *next;
};
typedef struct kEyS KEYS;
struct kEyS
{
char *KeyHandle, *Match;
int HanLen;
EXTMAC *KeyMac;
ARGSTACK *MyArgs, *ArgStack;
KEYS *next;
};
typedef struct InDeNt INDENT;
struct InDeNt
{
int start;
int nspac;
INDENT *next;
INDENT *prev;
};
typedef struct wOrDs WORDS;
struct wOrDs
{
char *word;
WORDS *next;
};
typedef struct eXtPrOc EXTPROC;
struct eXtPrOc
{
char *ProcNam, *FileNam;
int nargs;
WORDS *argnams;
EXTPROC *next;
};
/*
* If localprocs is set, I kill not only all my procedures, but all those
* defined by subservient extracts, and all proc macros become puny
*/
typedef struct eXtEnV EXTENV;
struct eXtEnV
{
FILE2 *FpIn, *FpOut;
char Flags[F_nFlags];/* logical flag array */
char ExtCmndOn[EC_nExtCmnds];
KEYS *KeyBase; /* pointer to the list of keys */
EXTPROC *MyProcs; /* Pointer to first procedure I've defined */
EXTPROC *MyFuncs; /* Pointer to first function I've defined */
EXTMAC *MyMacBeg;
PUNYMAC *MyPunyMacs; /* Puny macros */
INDENT *clindent; /* command-line indent */
int Joining; /* are we in a mode where joining lines is OK */
};
FILE *Warn;
EXTPROC *AllProcs=NULL, *AllFuncs=NULL;
EXTMAC *MacroBase=NULL;
INDENT *indbase=NULL;
int LnCount=0, nLnNums, *LnNums;
int ExtDone=0;
char *JoinLn=NULL;
unsigned int mode=0;
unsigned int modedepth[NMC] = {0,0};
char *version = "4.0.0";
/*
* Prototypes of the functions used before their definition
*/
void HandleLine(EXTENV *EE, char *line);
void ApplyFlags(EXTENV *EE, char *line);
#define Mupcase(C) ( ((C) > 96 && (C) < 123) ? (C) & 0xDF : (C) )
#define Mlowcase(C) ( ((C) > 64 && (C) < 91) ? (C) | 32 : (C) )
#define Mciswspace(C) ( (((C) > 8) && ((C) < 14)) || ((C) == 32) )
#define Mcisnum(C) ( ((C) > 47) && ((C) < 58) )
#define Mabs(x) ( (x) < 0 ? (x) * -1 : (x) )
void *Wmalloc(size_t len)
{
void *p;
p = malloc(len);
assert(p != NULL);
return(p);
}
/*===========================================================================*/
/* The following routines are string handling routines of various flavors */
/* Before the C purists attack: I am well aware some of these routines */
/* duplicate functionality in the ANSI C standard libraries. However, I */
/* wrote the first version of extract a long time ago, and I ran into a */
/* platform where the ANSI C string libraries did not work properly . . . */
/*===========================================================================*/
int Wstrcmp(char *p1, char *p2)
/*****************************************************************************/
/* PURPOSE: compares two strings pointed at by p1 & p2. */
/* RETURNS: 0 if they are not the same, else non-zero. */
/*****************************************************************************/
{
if ( (*p1) && (*p1 == *p2) )
{
do
{
p1++;
p2++;
}
while ( (*p1 == *p2) && (*p1) );
}
return( !(*p1 - *p2) );
} /* end Wstrcmp */
int WstrcmpN(char *p1, char *p2, int N)
/*****************************************************************************/
/* PURPOSE: Compares 1st N characters of strings pointed to by p1 & p2. */
/* RETURNS: nonzero if they are equal, else 0 */
/*****************************************************************************/
{
while( N && (*p1++ == *p2++) ) N--;
return(!N);
}
int Wfnd_substr(char *str, char *sub)
/*****************************************************************************/
/* PURPOSE: finds if a given string (sub) is contained in another (str). */
/* RETURNS: Position of start of string (starting from 1), 0 if string */
/* is not found. */
/*****************************************************************************/
{
char ch=(*sub);
char *str2, *sub2;
int i=0;
sub++;
if (*str)
{
do
{
i++;
if (*str++ == ch) /* 1st char matches */
{
str2 = str;
sub2 = sub;
while ( (*str2) && (*str2 == *sub2) ) {str2++; sub2++;}
if ( !(*sub2) ) return(i);
}
}
while (*str);
return(0);
}
else if (ch) return(0);
else return(1);
}
int Wstrlen(char *p1)
/*****************************************************************************/
/* PURPOSE: finds the number of characters in a string, not including \0. */
/* RETURNS: Length of string, not including null terminator. */
/*****************************************************************************/
{
int length=0;
if (*p1) do length++; while (*(++p1));
return (length);
}
int Wstrcpy(char *p1, char *p2)
/*****************************************************************************/
/* PURPOSE: Copies string pointed at by p2 into p1. */
/* RETURNS: Length of strings, not including null terminator. */
/*****************************************************************************/
{
int i=0;
if (*p1++ = *p2++) do i++; while(*p1++ = *p2++);
return(i);
}
void WstrcpyN(char *p1, char *p2, int N)
/*****************************************************************************/
/* PURPOSE: Copies N characters from p2 into p1. */
/*****************************************************************************/
{
if (N) do *p1++ = *p2++; while(--N);
}
void WstrcpyN_LOW(char *p1, char *p2, int N)
/*****************************************************************************/
/* PURPOSE: Copies N characters from p2 into p1 while lowcasing. */
/*****************************************************************************/
{
if (N)
{
do
{
*p1++ = Mlowcase(*p2);
p2++;
}
while(--N);
}
}
void WstrcpyN_UP(char *p1, char *p2, int N)
/*****************************************************************************/
/* PURPOSE: Copies N characters from p2 into p1 while upcasing. */
/*****************************************************************************/
{
if (N)
{
do
{
*p1++ = Mupcase(*p2);
p2++;
}
while(--N);
}
}
int WstrcpyUP(char *p1, char *p2)
/*****************************************************************************/
/* PURPOSE: Copies string pointed at by p2 into p1, and upcases. */
/* RETURNS: Length of strings, not including null terminator. */
/*****************************************************************************/
{
int i=0;
while( *p1++ = Mupcase(*p2) )
{
i++;
p2++;
}
return(i);
}
int WstrcpyLOW(char *p1, char *p2)
/*****************************************************************************/
/* PURPOSE: Copies string pointed at by p2 into p1, and lowcases. */
/* RETURNS: Length of strings, not including null terminator. */
/*****************************************************************************/
{
int i=0;
while( *p1++ = Mlowcase(*p2) )
{
i++;
p2++;
}
return(i);
}
int Wsafe_slowcase(char line[], int maxlen)
/*****************************************************************************/
/* PURPOSE: Changes string to lowercase, not touching quoted strings. */
/* RETURNS: -1 if there is an unmatched quote, else 0. */
/* NOTE : algorithm fails if have two strings: 'ST1' STUFF 'ST2' */
/* if you wanna fix, need to know the language */
/*****************************************************************************/
{
int i, ERROR=0;
char quote;
for (i=0; (line[i] && (i < maxlen)); i++)
{
line[i] = Mlowcase(line[i]);
if (line[i] == 34 || line[i] == 39 || line[i] == '`') /* is a " or ' */
{
quote = line[i++];
while(line[i]) i++;
while(line[i] != quote) i--;
if (i > maxlen) ERROR = -1;
}
}
return(ERROR);
}
int Wsafe_supcase(char line[], int maxlen)
/*****************************************************************************/
/* PURPOSE: Changes line to uppercase, not touching quoted strings. */
/* RETURNS: -1 if there is an unmatched quote, else 0. */
/* NOTE : algorithm fails if have two strings: 'ST1' STUFF 'ST2' */
/* if you wanna fix, need to know the language */
/*****************************************************************************/
{
int i, ERROR=0;
char quote;
for (i=0; (line[i] && (i < maxlen)); i++)
{
line[i] = Mupcase(line[i]);
if (line[i] == 34 || line[i] == 39 || line[i] == '`') /* is a " or ' */
{
quote = line[i++];
while ( (line[i] - quote) && (line[i]) && (i < maxlen) ) i++;
if ( line[i] != quote ) ERROR = -1; /* unmatched quote */
}
}
return(ERROR);
}
void Wslowcase(char *p1)
/*****************************************************************************/
/* PURPOSE: Changes to lowercase entire string pointed to by p1. */
/*****************************************************************************/
{
while(*p1 = Mlowcase(*p1)) p1++;
}
void Wsupcase(char *p1)
/*****************************************************************************/
/* PURPOSE: Changes to uppercase entire string pointed to by p1. */
/*****************************************************************************/
{
while(*p1 = Mupcase(*p1)) p1++;
}
void Wsstrip(char *p1)
/*****************************************************************************/
/* PURPOSE: strip all white spaces from string */
/*****************************************************************************/
{
char *p2=p1;
do
{
if (Mciswspace(*p2)) p2++;
else *p1++ = *p2++;
}
while (*p1);
}
void Wtab2spcs(char *p, int N)
/*****************************************************************************/
/* PURPOSE: replace all tabs in string p with N spaces */
/*****************************************************************************/
{
char tline[256];
int i;
int Wstrcpy(char *p1, char *p2);
while (*p)
{
if (*p == '\t')
{
Wstrcpy(tline, p+1);
i = N;
while (N--) *p++ = ' ';
Wstrcpy(p, tline);
}
else p++;
}
}
int Wstr2int(char *str, int *iptr)
/*****************************************************************************/
/* PURPOSE: read an integer from string str into *iptr */
/* RETURNS: number of characters read in */
/*****************************************************************************/
{
int i, j;
sscanf(str, "%d", iptr);
for (i=0, j=Mabs(*iptr); j > 0; i++) j /= 10;
if (*iptr == 0) i=1;
if (str[0] == '+' || str[0] == '-') i++;
return(i);
}
void Wsafe_mws2spc(char *p1)
/*****************************************************************************/
/* PURPOSE: replaces contiguous white spaces with 1 space, skip quotes. */
/*****************************************************************************/
{
char *p2;
p2 = p1;
while (*p2)
{
if (*p1 == '"')
{
do *p2++ = *p1++; while(*p1 != '"');
*p2++ = *p1++;
continue;
}
if (Mciswspace(*p1))
{
*p2++ = ' ';
while (Mciswspace(*p1)) p1++;
continue;
}
while ( (*p2) && !(Mciswspace(*p1)) ) *p2++ = *p1++;
}
}
void Wremove_trailing_blanks(char *p)
/*****************************************************************************/
/* PURPOSE: Remove trailing whitespace from string p. */
/*****************************************************************************/
{
char *st=p;
if ( !(*p) ) return;
do p++; while (*p);
p--;
while ( (Mciswspace(*p)) && (p != st) ) p--;
if ( Mciswspace(*p) )
{
*p = '\n';
p[1] = '\0';
}
else
{
p[1] = '\n';
p[2] = '\0';
}
}
void CharSet(int N, char *x, char val)
/*****************************************************************************/
/* Set N elements starting at x to val */
/*****************************************************************************/
{
int i;
for (i=0; i < N; i++) x[i] = val;
}
int commandcpy(char *out, char *in)
/*****************************************************************************/
/* PURPOSE: copy in to out, remove all leading spaces, put 1 space after */
/* first word, lowcase first word, then copy rest string unchanged. */
/* RETURNS: Length of first word (the possible command). */
/*****************************************************************************/
{
int i=0;
while( Mciswspace(*in) ) in++; /* skip leading white spaces */
while( (*in) && !Mciswspace(*in) ) /* copy first word */
{
*out++ = Mlowcase(*in);
in++;
i++;
}
/*
* Only 1 space follows word, and copy rest of line unaltered
*/
*out++ = ' ';
while( Mciswspace(*in) ) in++;
while (*out++ = *in++);
return(i);
}
void keycpy(char *out, char *in)
/*****************************************************************************/
/* PURPOSE: copy in to out, remove all leading spaces, lowcase and put 1 */
/* space after each word, and leave stuff in ` ` alone. */
/*****************************************************************************/
{
char *p, *ch;
p = out;
while (*in)
{
while( Mciswspace(*in) ) in++; /* skip leading spaces */
if (*in == '`') /* don't mess with ` ` quotes */
{
ch = in;
while (*ch) ch++;
while (*ch != '`') ch--;
while (in != ch) *out++ = *in++;
*out++ = *in++;
}
while( (*in) && !Mciswspace(*in) ) /* copy word */
{
*out++ = Mlowcase(*in);
in++;
}
*out++ = ' '; /* white spaces collapse to 1 space */
if (*in) in++;
}
*out = '\0';
}
/*===========================================================================*/
/* These functions print out error and warning messages from extract */
/*===========================================================================*/
void ExtWarn(EXTENV *EE, char *form, ...)
{
FILE2 *fp;
va_list argptr;
char cline[256];
int i;
va_start(argptr, form);
vsprintf(cline, form, argptr);
va_end(argptr);
/*
* Remove newlines from cline
*/
for (i=0; cline[i]; i++);
i--;
if (cline[i] == '\n') cline[i--] = '\0';
if (EE)
{
fflush(EE->FpOut->Fp);
fprintf(stderr, "\nExtract warning \'%s\'.\n", cline);
fprintf(stderr, "Input file chain:\n");
fprintf(stderr, " Warning occured on line %d of file %s.\n",
EE->FpIn->LineNo, EE->FpIn->Fnam);
for (fp=EE->FpIn->prev; fp; fp = fp->prev)
fprintf(stderr, " Which was extracted from line %d of file %s.\n",
fp->LineNo, fp->Fnam);
fprintf(stderr, "\nWarning occured after line %d of output file %s.\n",
EE->FpOut->LineNo, EE->FpOut->Fnam);
}
else fprintf(stderr, "\nExtract warning \'%s\'\n", cline);
}
void ExtErr(EXTENV *EE, char *form, ...)
{
FILE2 *fp;
va_list argptr;
char cline[256];
int i;
va_start(argptr, form);
vsprintf(cline, form, argptr);
va_end(argptr);
/*
* Remove newlines from cline
*/
for (i=0; cline[i]; i++);
i--;
if (cline[i] == '\n') cline[i--] = '\0';
if (EE)
{
if (EE->FpOut && EE->FpOut->Fp) fflush(EE->FpOut->Fp);
fprintf(stderr, "\nExtract ERROR \'%s\'.\n", cline);
if (EE->FpIn)
{
fprintf(stderr, "Input file chain:\n");
fprintf(stderr, " Error occured on line %d of file %s.\n",
EE->FpIn->LineNo, EE->FpIn->Fnam);
for (fp=EE->FpIn->prev; fp; fp = fp->prev)
fprintf(stderr,
" Which was extracted from line %d of file %s.\n",
fp->LineNo, fp->Fnam);
fprintf(stderr, "\nError occured after line %d of output file %s.\n\n",
EE->FpOut->LineNo, EE->FpOut->Fnam);
}
}
else fprintf(stderr, "\nExtract ERROR \'%s\'\n\n", cline);
exit(1);
}
void HandleAtEscape(char *ln)
{
int i;
while (i = Wfnd_substr(ln, "@(@)"))
{
Wstrcpy(ln+i, ln+i+3);
ln += i;
}
}
/*===========================================================================*/
/* These routines do basic file stuff */
/*===========================================================================*/
void PutLn(EXTENV *EE, char *line)
{
int ExtLn, h, i=0, k;
static int j=0;
char *tln;
/*
* Check if line needs to be joined with next line
*/
if (EE->Joining)
{
while(line[i]) i++;
if (i)
{
i--;
while(Mciswspace(line[i]) && i) i--;
}
ExtLn = (line[i] == '\\') && (i);
if (ExtLn) ExtLn = line[i-1] == '@';
/*
* If we are extending a line, or have an extended line in memory,
* save the present line, and merge it with any previously stored lines
*/
if ( ExtLn || JoinLn )
{
tln = malloc(i + j);
for (k=0; k != j; k++) tln[k] = JoinLn[k];
if (ExtLn) h = i + j - 1;
else h = i + j + 1;
for (; k < h; k++) tln[k] = line[k-j];
tln[k] = '\n';
tln[k+1] = '\0';
j = h;
if (JoinLn) free(JoinLn);
JoinLn = tln;
}
else tln = line;
}
else
{
tln = line;
ExtLn = 0;
}
/*
* If this line wasn't extended, can finally dump to file
*/
if (!ExtLn)
{
EE->FpOut->LineNo++;
if (!LnCount)
{
ApplyFlags(EE, tln);
HandleAtEscape(tln);
fputs(tln, EE->FpOut->Fp);
}
else if (EE->FpOut->LineNo == LnNums[nLnNums])
{
fprintf(Warn,
"\nLine %d of extracted file corresponds to line %d of \'%s\'\n",
EE->FpOut->LineNo, EE->FpIn->LineNo, EE->FpIn->Fnam);
nLnNums--;
if (nLnNums == 0)
{
fprintf(Warn, "\n\nEXTRACT finished translating line numbers.\n\n");
exit(0);
}
}
if (JoinLn) free(JoinLn);
JoinLn = NULL;
j = 0;
}
}
int GetLn(EXTENV *EE, char *line)
{
if (ExtDone) return(0);
EE->FpIn->LineNo++;
/* return( (int) fgets(line, LNLEN, EE->FpIn->Fp) ); */
return( (fgets(line, LNLEN, EE->FpIn->Fp) != NULL) );
}
FILE2 *OpenFile(EXTENV *EE, char *Fnam, char *mode)
{
char yorn;
FILE2 *fp;
fp = (FILE2 *) malloc(sizeof(*fp));
Wstrcpy(fp->Fnam, Fnam);
fp->LineNo = 0;
if (mode[0] == 'w')
{
fp->prev = EE->FpOut;
if ( Wstrcmp(Fnam, "stdout") )
{
fp->Fp = stdout;
return(fp);
}
else if ( Wstrcmp(Fnam, "stderr") )
{
fp->Fp = stderr;
return(fp);
}
if (EE->Flags[F_Verb] > 2) fprintf(Warn, "Creating file %s.\n", Fnam);
if (EE->Flags[F_Query])
{
if ( !access(Fnam, 0) )
{
fprintf(Warn, "File %s already exists. Overwrite?", Fnam);
fflush(stdin);
yorn = getchar();
if ( (yorn != 'y') && (yorn != 'Y') ) exit(0);
}
}
}
else if ( (mode[0] == 'r') && (mode[1] == '\0') )
{
fp->prev = EE->FpIn;
if ( Wstrcmp(Fnam, "stdin") )
{
fp->Fp = stdin;
return(fp);
}
if (EE->Flags[F_Verb] > 2)
fprintf(Warn, "Opening file \'%s\' for input . . .\n", Fnam);
if ( access(Fnam, 0) )
{
fprintf(Warn, "Cannot find input file \'%s\'\n", Fnam);
exit(1);
}
}
fp->Fp = fopen(Fnam, mode);
if (fp->Fp == NULL) ExtErr(EE, "Error opening file %s",Fnam);
return(fp);
}
void CloseFile(EXTENV *EE, FILE2 *fp)
{
if (JoinLn) if (EE->FpOut == fp) PutLn(EE, "\0");
if (EE->Flags[F_Verb] > 2) fprintf(Warn, "Closing file \'%s\'.\n", fp->Fnam);
if ( (fp->Fp != stderr) && (fp->Fp != stdin) && (fp->Fp != stdout) )
{
if ( fclose(fp->Fp) == EOF )
ExtErr(EE, "Error closing file %s", fp->Fnam);
}
free(fp);
} /* end close_file */
/*===========================================================================*/
/* These functions all deal with breaking lines into their respective words */
/*===========================================================================*/
static WORDS *GetWord(char *wrd)
/*****************************************************************************/
/* PURPOSE: Safely allocates word of correct length */
/* RETURNS: Allocated WORD. */
/*****************************************************************************/
{
WORDS *wp;
wp = malloc(sizeof(WORDS));
wp->word = malloc( (Wstrlen(wrd)+1)*sizeof(char) );
Wstrcpy(wp->word, wrd);
wp->next = NULL;
return(wp);
}
static WORDS *KillWord(WORDS *wbase, WORDS *wkill)
/*****************************************************************************/
/* PURPOSE: Kills single word wkill in list of words starting with wbase. */
/* RETURNS: Possibly new wbase. */
/*****************************************************************************/
{
WORDS *wptr;
if (wkill == wbase) wbase = wbase->next;
else
{
for (wptr=wbase; wptr->next != wkill; wptr = wptr->next);
wptr->next = wkill->next;
}
free(wkill->word);
free(wkill);
return(wbase);
}
static void KillWords(WORDS *wbase)
/*****************************************************************************/
/* PURPOSE: Frees space used by words, starting at wbase. */
/*****************************************************************************/
{
while(wbase) wbase = KillWord(wbase, wbase);
}
void PrintWords(WORDS *wbase)
/*****************************************************************************/
/* PURPOSE: print words pointed to by wbase (debugging routine) */
/*****************************************************************************/
{
printf("Words: ");
while(wbase)
{
printf("'%s' ",wbase->word);
wbase = wbase->next;
}
printf("\n");
}
WORDS *GetVars(char *line, char Ab, char Ae)
/*****************************************************************************/
/* PURPOSE: splits a line into its seperate variables for @declare. Ab and */
/* Ae are the array beginning and ending characters for your */
/* language ([] for C, and () for Fortran). Variables are */
/* seperated by whitespace(s) and/or a comma. */
/*****************************************************************************/
{
int i=0, j, k;
WORDS *wbase=NULL, *wp;
static WORDS *wptr=NULL;
char wrd[LNLEN];
if (line == NULL)
{
wptr = NULL;
return(NULL);
}
while(Mciswspace(line[i])) i++;
if (line[i] == '\0') return(NULL);
wbase = GetWord(" ");
if (wptr)
{
wp = wptr;
wptr->next = wbase;
}
else wp = wbase;
wptr = wbase;
while(line[i])
{
j = 0;
while(line[i] != ',') /* get one word */
{
if (line[i] == '@') /* check for sticky space */
{
if (line[i+1] == '^')
{
wrd[j++] = ' ';
i += 2;
continue;
}
}
if (Mciswspace(line[i]))
{
while(Mciswspace(line[i])) i++;
if (line[i] != Ab) break;
else continue;
}
if (line[i] == Ab) /* have an array declaration */
{
k = 0;
do
{
wrd[j++] = line[i];
if (line[i] == Ab) k++;
else if (line[i] == Ae) k--;
i++;
}
while(k);
if (!line[i]) break;
}
else wrd[j++] = line[i++];
}
wrd[j] = '\0';
wptr->next = GetWord(wrd);
wptr = wptr->next;
if (line[i] == ',')
{
i++;
while(Mciswspace(line[i])) i++;
}
}
if (wptr == wbase) wptr = wbase = KillWord(wp, wbase);
else wbase = KillWord(wp, wbase);
return(wbase);
}
WORDS *GetArgs(EXTENV *EE, char *line)
/*****************************************************************************/
/* PURPOSE: splits a line into its seperate words by splitting on whitespcs, */
/* accepts args in double quotes as one arg. */
/*****************************************************************************/
{
int i=0, j;
WORDS *wptr, *wbase=NULL;
char word[LNLEN];
while(Mciswspace(line[i])) i++;
wptr = wbase = GetWord(" ");
while(line[i])
{
if (line[i] == '\"')
{
i++;
for (j=0; (line[i] != '\"') && line[i]; word[j++] = line[i++]);
if (!line[i]) ExtErr(EE, "Unmatched double quote.");
i++;
}
else
{
j = 0;
while(!Mciswspace(line[i]) && line[i])
{
if (line[i] != '@' || line[i+1] != '^')
word[j++] = line[i++];
else /* sticky space */
{
word[j++] = ' ';
i += 2;
}
}
}
word[j] = '\0';
wptr->next = GetWord(word);
wptr = wptr->next;
while(Mciswspace(line[i])) i++;
}
return( KillWord(wbase, wbase) );
}
WORDS *GetWords(char *line)
/*****************************************************************************/
/* PURPOSE: splits a line into its seperate words by splitting on whitespcs */
/*****************************************************************************/
{
int i=0, j;
WORDS *wptr, *wbase;
char word[LNLEN];
while(Mciswspace(line[i])) i++;
wptr = wbase = GetWord(" ");
while(line[i])
{
j = 0;
while(!Mciswspace(line[i]) && line[i])
{
if (line[i] != '@' || line[i+1] != '^')
word[j++] = line[i++];
else /* sticky space */
{
word[j++] = ' ';
i += 2;
}
}
word[j] = '\0';
wptr->next = GetWord(word);
wptr = wptr->next;
while(Mciswspace(line[i])) i++;
}
return( KillWord(wbase, wbase) );
}
FILE *CreateListFile(EXTENV *EE, char *EndStr, int EndLen, char *line,
char *tline)
{
FILE2 fp, *FpHold;
char fsave[6];
int i, joining;
joining = EE->Joining;
EE->Joining = 0;
sprintf(fp.Fnam, "Line%d_of_%s\n",EE->FpIn->LineNo, EE->FpIn->Fnam);
fsave[0] = EE->Flags[F_Case];
fsave[1] = EE->Flags[F_RemBlank];
fsave[2] = EE->Flags[F_Ugly];
fsave[3] = EE->Flags[F_RepTab];
fsave[4] = EE->Flags[F_Clint];
fsave[5] = EE->Flags[F_LLWarn];
EE->Flags[F_Case] = EE->Flags[F_RemBlank] = EE->Flags[F_Ugly] =
EE->Flags[F_RepTab] = EE->Flags[F_Clint] =
EE->Flags[F_LLWarn] = 0;
fp.LineNo = 0;
fp.Fp = tmpfile();
FpHold = EE->FpOut;
EE->FpOut = &fp;
while(GetLn(EE, line))
{
i = commandcpy(tline, line) + 1;
if (i == EndLen) if ( WstrcmpN(tline, EndStr, EndLen) ) break;
HandleLine(EE, line);
}
EE->FpOut = FpHold;
EE->Flags[F_Case] = fsave[0];
EE->Flags[F_RemBlank] = fsave[1];
EE->Flags[F_Ugly] = fsave[2];
EE->Flags[F_RepTab] = fsave[3];
EE->Flags[F_Clint] = fsave[4];
EE->Flags[F_LLWarn] = fsave[5];
EE->Joining = joining;
return(fp.Fp);
}
WORDS *fGetWords(EXTENV *EE, char *EndStr, int EndLen, char *line, char *tline)
/*****************************************************************************/
/* PURPOSE: Gets words from the input file until EndStr is found. */
/*****************************************************************************/
{
FILE *fp;
WORDS *wptr, *wp;
fp = CreateListFile(EE, EndStr, EndLen, line, tline);
rewind(fp);
if ( fgets(line, LNLEN, fp) )
{
do /* skip leading blank lines */
{
wp = wptr = GetWords(line);
if (wp == NULL) if ( !fgets(line, LNLEN, fp) ) break;
}
while (wp == NULL);
while( fgets(line, LNLEN, fp) )
{
while (wp->next) wp = wp->next;
wp->next = GetWords(line);
}
}
fclose(fp);
return(wptr);
}
WORDS *fGetVars(EXTENV *EE, char *EndStr, int EndLen, char *line, char *tline,
char Ab, char Ae)
/*****************************************************************************/
/* PURPOSE: Gets variables from the input file until EndStr is found. */
/*****************************************************************************/
{
FILE *fp;
WORDS *wptr=NULL, *wp;
fp = CreateListFile(EE, EndStr, EndLen, line, tline);
rewind(fp);
if ( fgets(line, LNLEN, fp) )
{
do
{
wptr = GetVars(line, Ab, Ae);
if (!wptr) if ( !fgets(line, LNLEN, fp) ) break;
}
while (wptr == NULL); /* skip leading blank lines */
while( fgets(line, LNLEN, fp) ) GetVars(line, Ab, Ae);
GetVars(NULL, Ab, Ae);
}
fclose(fp);
return(wptr);
}
WORDS *GetKeys(char *line)
/*****************************************************************************/
/* PURPOSE: Gets keyargs from keyline, puts a space at end. */
/*****************************************************************************/
{
int i=0, j;
WORDS *wptr, *wbase;
char word[LNLEN];
while(Mciswspace(line[i])) i++;
wptr = wbase = GetWord(" ");
while(line[i])
{
word[0] = ' ';
for (j=1; !Mciswspace(line[i]); word[j++] = line[i++]);
word[j++] = ' ';
word[j] = '\0';
wptr->next = GetWord(word);
wptr = wptr->next;
while(Mciswspace(line[i])) i++;
}
return( KillWord(wbase,wbase) );
}
WORDS *CreateKeyLine0(EXTENV *EE, WORDS *wbase, char *ln)
{
WORDS *wp, *wp0;
wp = GetKeys(ln);
if (wp)
{
if (!wbase) wbase = wp;
else
{
for (wp0=wbase; wp0->next; wp0 = wp0->next);
wp0->next = wp;
}
while (wp->next) wp = wp->next;
if (Wfnd_substr(wp->word, "@\\")) /* ln extension */
{
wbase = KillWord(wbase, wp);
assert(GetLn(EE, ln));
CreateKeyLine0(EE, wbase, ln);
}
}
return(wbase);
}
char *CreateKeyLine(EXTENV *EE, char *ln)
{
char *tln;
WORDS *wp, *wbase;
int i;
wbase = CreateKeyLine0(EE, NULL, ln);
for (wp=wbase, i=0; wp; wp = wp->next) i += Wstrlen(wp->word)+1;
if (i >= LNLEN) ExtErr(EE, "Lame-ass static ARGSTACK allocation exceeded");
tln = malloc((i+1)*sizeof(char));
assert(tln);
for (wp=wbase, i=0; wp; wp = wp->next)
{
i += WstrcpyLOW(tln+i, wp->word);
tln[i++] = ' ';
}
tln[i] = '\0';
KillWords(wbase);
return(tln);
}
void UpLowWords(char cas, WORDS *wbase)
/*****************************************************************************/
/* PURPOSE: if cas == 'u', upcases all words, otherwise, lowcases all words. */
/*****************************************************************************/
{
void (*fcase)(char *);
if (cas == 'u') fcase = Wsupcase;
else fcase = Wslowcase;
while(wbase)
{
fcase(wbase->word);
wbase = wbase->next;
}
}
void RemQuoteWords(WORDS *wp)
/*****************************************************************************/
/* PURPOSE: Removes quotes around words, if they exist, and stops when a */
/* word starts with - */
/*****************************************************************************/
{
int i;
while (wp)
{
if (wp->word[0] == '-') break;
if (wp->word[0] == '\"')
{
i = Wstrcpy(wp->word, wp->word+1) - 1;
assert(wp->word[i] == '\"');
wp->word[i] = '\0';
}
wp = wp->next;
}
}
WORDS *SortWords(char *cas, WORDS *wbase)
/*****************************************************************************/
/* PURPOSE: sorts words in alphabetic order, and up or low cases them based */
/* on *cas. */
/*****************************************************************************/
{
int i;
WORDS *wbase2=NULL, *wptr, *wptr2, *prev;
if (cas) UpLowWords(*cas, wbase);
while(wbase)
{
/*
* Find largest word left in original list
*/
wptr2 = wbase;
for (wptr=wbase->next; wptr; wptr=wptr->next)
{
for (i=0; !(wptr->word[i] - wptr2->word[i]) && (i < SUBLEN); i++);
if (wptr->word[i] > wptr2->word[i]) wptr2 = wptr;
}
/*
* Remove largest word from original list
*/
for (wptr=wbase; (wptr != wptr2); wptr = wptr->next) prev = wptr;
if (wptr == wbase) wbase = wbase->next;
else prev->next = wptr->next;
/*
* Put largest word on second list
*/
wptr2->next = wbase2;
wbase2 = wptr2;
}
return(wbase2);
}
int CountWords(WORDS *wp)
/*****************************************************************************/
/* RETURNS: Number of words in list starting at wp */
/*****************************************************************************/
{
int i;
for (i=0; wp; wp = wp->next, i++);
return(i);
}
/*===========================================================================*/
/* These routines all work on extract's macros. */
/*===========================================================================*/
EXTMAC *FindMac(char *handle)
{
int i;
EXTMAC *ptr;
i = Wstrlen(handle);
for(ptr=MacroBase; ptr; ptr=ptr->next)
{
if (ptr->HanLen == i + 3)
if( WstrcmpN(&(ptr->Handle[2]), handle, i) )
if( ptr->Handle[i+2] == ')' ) break;
}
return(ptr);
}
int CountMacros(void)
{
EXTMAC *p;
int i;
for (i=0,p=MacroBase; p; i++,p = p->next);
return(i);
}
/************************************************************************/
/* The routines push_macro & pop_macro are the heart of extract's */
/* macro substitution utility. A macro is defined by: */
/* @define <handle> @<substitution>@ */
/* The @define must followed by one and only one space. */
/* The handle is everything until */
/* the next whitespace. The begin and end of the substitute string */
/* are demarked by the @ character. Therefore, if we wanted to */
/* substitute "joe" for "bob" in the text, we could have following */
/* definition: */
/* @define bob @joe@ */
/* the delimiter is @(handle) */
/* So, using the macro we've defined above, */
/* we would have the following: */
/* "Well, @(bob) is a fairy." */
/* Once extracted, this would read, "Well, joe is a fairy.". */
/* Macros are based on a stack, so to use old @define's you must */
/* do an @undef. */
/************************************************************************/
EXTMAC *PushMacro2(EXTENV *EE, int PUNY, char *handle, char *sub)
{
EXTMAC *mp;
PUNYMAC *pp;
mp = Wmalloc(sizeof(EXTMAC));
mp->next = MacroBase;
MacroBase = mp;
mp->HanLen = Wstrlen(handle) + 3;
mp->Handle = Wmalloc(mp->HanLen+1);
mp->Handle[0] = '@';
mp->Handle[1] = '(';
Wstrcpy(mp->Handle+2, handle);
mp->Handle[mp->HanLen-1] = ')';
mp->Handle[mp->HanLen] = '\0';
mp->SubLen = Wstrlen(sub);
mp->Sub = Wmalloc(mp->SubLen+1);
Wstrcpy(mp->Sub, sub);
if ( Wfnd_substr(mp->Sub, mp->Handle) )
ExtErr(EE, "No recursive macro definitions allowed.");
if (PUNY)
{
pp = Wmalloc(sizeof(PUNYMAC));
pp->next = EE->MyPunyMacs;
EE->MyPunyMacs = pp;
pp->mp = mp;
}
return(mp);
}
void PushMacro(EXTENV *EE, char *line)
{
int i, j;
char *h, *s, *t;
h = line + 8;
for (i=8; (!Mciswspace(line[i])); i++)
if (line[i] == '@' || line[i] == '(' || line[i] == ')')
ExtErr(EE, "Prohibited character in macro handle, line=\'%s\'",line);
j = i; /* where handle ends */
i++;
while( (Mciswspace(line[i])) && (line[i]) ) i++;
if (line[i++] != '@') ExtErr(EE, "Bad macro definition: \'%s\'.", line);
s = line + i;
while (line[i]) i++;
while (line[i] != '@') i--;
if (s-1 == line + i) ExtErr(EE, "Bad macro definition: \'%s\'.", line);
line[i] = '\0';
line[j] = '\0';
PushMacro2(EE, EE->Flags[F_FragMac], h, s);
}
int PopThisMacro(EXTENV *EE, EXTMAC *killme)
{
int i;
EXTMAC *ptr, *tptr;
for(ptr=MacroBase; ptr && ptr != killme; ptr=ptr->next) tptr = ptr;
if (ptr)
{
if (ptr == MacroBase) MacroBase = ptr->next;
else tptr->next = ptr->next;
free(ptr->Handle);
free(ptr->Sub);
free(ptr);
return(1);
}
return(0);
}
int PopMacro2(EXTENV *EE, char *handle)
{
int i;
EXTMAC *ptr, *tptr;
i = Wstrlen(handle);
for(ptr=MacroBase; ptr; ptr=ptr->next)
{
if (ptr->HanLen == i + 3)
if( WstrcmpN(&(ptr->Handle[2]), handle, i) )
if( ptr->Handle[i+2] == ')' ) break;
tptr = ptr;
}
if (ptr)
{
if (ptr == MacroBase) MacroBase = ptr->next;
else tptr->next = ptr->next;
free(ptr->Handle);
free(ptr->Sub);
free(ptr);
return(1);
}
return(0);
}
int PopMacro(EXTENV *EE, char *line)
{
char ch, *han;
int i, j;
han = line + 7;
for (i=0; han[i] && !Mciswspace(han[i]); i++)
if (han[i] == '(' || han[i] == ')' || han[i] == '@')
ExtErr(EE, "Malformed @undef: \'%s\'", line);
ch = han[i];
han[i] = '\0';
j = PopMacro2(EE, han);
han[i] = ch;
return(j);
}
void MakeMacSub(char *line, int I, EXTMAC *mp)
/*****************************************************************************/
/* PURPOSE: substitute mp->Sub at &line[I]. */
/*****************************************************************************/
{
char ch1, ch='l';
char *p, *st, *sub=mp->Sub;
char lstr[32];
int i=I, k, Spcs=0, len=mp->SubLen, sublen=mp->SubLen, hanlen=mp->HanLen;
void (*cpyfuncN)(char *, char *, int);
cpyfuncN = WstrcpyN;
if (i > 2) /* could have some formatting going on */
{
/*
* If we have @up or @low, set cpyfuncN appropriatly
*/
if (line[i-1] == 'p') /* maybe @up */
{
if ( (line[i-2] == 'u') && (line[i-3] == '@') )
{
i -= 3;
cpyfuncN = WstrcpyN_UP;
}
}
else if (i > 3)
{
if (line[i-4] == '@')
{
if (line[i-3] == 'l' && line[i-2] == 'o' && line[i-1] == 'w')
{
i -= 4;
cpyfuncN = WstrcpyN_LOW;
}
else if (line[i-3] == 'l' && line[i-2] == 'e' && line[i-1] == 'n')
{
sprintf(lstr, "%d", mp->SubLen);
len = sublen = Wstrlen(lstr);
sub = lstr;
i -= 4;
}
}
}
/*
* See if we have formating of type @#[l,r,c] going on
*/
if (i > 2)
{
ch1 = Mlowcase(line[i-1]);
if ( (ch1 == 'l') || (ch1 == 'r') || (ch1 == 'c') )
{
if (Mcisnum(line[i-2]))
{
for (k=i-3; k && Mcisnum(line[k]); k--);
if (line[k] == '@') /* it is one of those formats */
{
line[i-1] = '\0'; /* gonna overwrite this later anyway */
sscanf(line+k+1, "%d", &len);
if (len < sublen) sublen = len;
i = k;
ch = ch1;
}
}
}
}
}
hanlen += I - i;
/*
* Figure out how many spaces to put before mp->Sub due to formatting
*/
if (len != sublen)
{
if (ch == 'r') Spcs = len - sublen;
else if (ch == 'c') Spcs = (len - sublen) / 2;
}
/*
* If formatted Sub longer than handle, move last part of line to right
*/
if (len > hanlen)
{
p = line + I + mp->HanLen;
k = len - hanlen; /* number of elements to move string */
st = p - 1;
while (*p) p++;
do p[k] = *p; while (--p != st);
}
/*
* If Macro handle longer than formatted Sub, move last part of line to left
*/
else if (len != hanlen) Wstrcpy(line+i+len, line+I+mp->HanLen);
for (k=Spcs; k; k--) line[i++] = ' '; /* handle preceding spaces */
cpyfuncN(line+i, sub, sublen); /* Copy Macro sub string */
i += sublen; /* handle trailing spaces */
for (k=len - Spcs - sublen; k; k--) line[i++] = ' ';
}
int ExpandMacro(EXTMAC *macp, char *line)
{
int i, j, k, len, Expanded=0;
char ch, tline[LNLEN];
int (*cpyfunc)(char *, char *);
while ( (i = Wfnd_substr(line, macp->Handle)) )
{
Expanded = 1;
MakeMacSub(line, i-1, macp);
}
return(Expanded);
}
int ExpandThisMacro(EXTENV *EE, char *line, int i, int hlen)
{
EXTMAC *mp;
for (mp=MacroBase; mp; mp = mp->next)
if (mp->HanLen == hlen) if ( WstrcmpN(line+i, mp->Handle, hlen) ) break;
if (mp)
{
MakeMacSub(line, i, mp);
return(1);
}
else return(0);
}
void MacroSub(EXTENV *EE, char *line)
{
char *p;
int i, hlen, il=0, DONE=1;
EXTMAC *macp;
/*
* Try substituting macros from left to right in line; if this fails, use
* slower but more general method
*/
while ( i = Wfnd_substr(line+il, "@(") )
{
il += i - 1;
p = line + il + 2;
for (hlen=2; (*p) && (*p != ')'); hlen++, p++);
if (*p) hlen++;
else return; /* no macros without end paren */
if (!ExpandThisMacro(EE, line, il, hlen))
{
DONE=0;
break;
}
}
if (!DONE)
{
macp=MacroBase;
while(macp)
{
i = Wfnd_substr(line, "@(");
if (!i) break;
if ( ExpandMacro(macp, line) ) macp = MacroBase;
else macp = macp->next;
}
}
} /* end of MacroSub */
void KillMyMacros(EXTENV *EE)
{
EXTMAC *mp;
while(MacroBase != EE->MyMacBeg)
{
mp = MacroBase;
MacroBase = mp->next;
free(mp->Handle);
free(mp->Sub);
free(mp);
}
}
/*===========================================================================*/
/* These functions deal with key manipulations. */
/*===========================================================================*/
void KillThisKey(EXTENV *EE, KEYS *DeadKey)
{
char line[LNLEN];
KEYS *key, *prev=NULL;
ARGSTACK *args;
assert(EE->KeyBase != NULL);
for (key=EE->KeyBase; key != DeadKey; key = key->next) prev = key;
assert( PopThisMacro(EE, key->KeyMac) ); /* pop automatic macro */
/*
* Remove key from list
*/
if (key == EE->KeyBase) EE->KeyBase = key->next;
else prev->next = key->next;
while (key->ArgStack)
{
args = key->ArgStack->next;
free(key->ArgStack);
key->ArgStack = args;
}
if (key->MyArgs) free(key->MyArgs);
free(key->KeyHandle);
free(key->Match);
free(key);
}
void KillKey(EXTENV *EE, char *s)
{
int slen;
KEYS *key, *prev=NULL;
slen = Wstrlen(s);
key = EE->KeyBase;
while(key)
{
if (slen+2 == key->HanLen) /* possible match if they're same length */
{
if ( WstrcmpN(s, &key->KeyHandle[1], slen) )
{
KillThisKey(EE, key);
if (prev) key = prev->next;
else key = EE->KeyBase;
}
else { prev = key; key = key->next; }
}
else { prev = key; key = key->next; }
}
}
void AddKey(EXTENV *EE, char *s)
{
char *cp, *cp2;
int i, j, BegAt, EndAt, len, err=0;
KEYS *key;
key = Wmalloc(sizeof(KEYS));
key->next = EE->KeyBase;
EE->KeyBase = key;
for(i=0; s[i] != '=' && s[i]; i++);
if (!(s[i])) ExtErr(NULL, "Bad key input \'%s\'.", s);
if (!(s[i+1])) ExtErr(NULL, "Bad key input \'%s\'.", s);
key->HanLen = i + 2;
key->KeyHandle = Wmalloc(key->HanLen + 1);
key->KeyHandle[0] = '@';
WstrcpyN_LOW(key->KeyHandle+1, s, i);
key->KeyHandle[key->HanLen-1] = ' ';
key->KeyHandle[key->HanLen] = '\0';
cp2 = s + i + 1;
for (i=0; !Mciswspace(cp2[i]) && cp2[i]; i++);
if (!i) ExtErr(NULL, "Bad key input \'%s\'.", s);
len = i + 2;
BegAt = (*cp2 == '@');
EndAt = (cp2[i-1] == '@') && (i > 1);
if (BegAt)
{
cp2++;
j = 0;
len -= 2;
i--;
}
else j = 1;
if (EndAt)
{
len -= 2;
i--;
}
if ( i < 0 || len < 0) ExtErr(NULL, "Bad key input \'%s\'.", s);
key->Match = Wmalloc(len+1);
if (!BegAt) key->Match[0] = ' ';
WstrcpyN_LOW(key->Match+j, cp2, i);
if (!EndAt) key->Match[len-1] = ' ';
key->Match[len] = '\0';
key->MyArgs = (ARGSTACK *) Wmalloc(sizeof(ARGSTACK));
key->MyArgs->Not = 1;
key->MyArgs->KeyArgs[0] = '\0';
key->ArgStack = NULL;
/*
* Add automatic key macro
*/
key->KeyHandle[key->HanLen-1] = '\0';
cp = cp2 = Wmalloc(len+3);
if (*key->Match != ' ') *cp++ = '@';
cp += Wstrcpy(cp, key->Match) - 1;
if (*cp != ' ') { cp++; *cp++ = '@'; }
*cp = '\0';
if (*cp2 == ' ') cp = cp2 + 1;
else cp = cp2;
key->KeyMac = PushMacro2(EE, 0, key->KeyHandle, cp);
key->KeyHandle[key->HanLen-1] = ' ';
free(cp2);
}
void PushKey(EXTENV *EE, KEYS *Key)
{
int i;
ARGSTACK *NewArgs;
i = sizeof(ARGSTACK);
NewArgs = (ARGSTACK *) Wmalloc(i);
WstrcpyN((void *) NewArgs, (void *) Key->MyArgs, i);
NewArgs->next = Key->ArgStack;
Key->ArgStack = NewArgs;
}
void PopKey(EXTENV *EE, KEYS *Key)
{
if (Key->ArgStack == NULL)
{
ExtWarn(EE, "Trying to pop from key ( %s) with nonexistant stack.\n",
Key->KeyHandle);
return;
}
if (Key->MyArgs) free(Key->MyArgs);
Key->MyArgs = Key->ArgStack;
Key->ArgStack = Key->ArgStack->next;
}
void PeekKey(EXTENV *EE, KEYS *Key)
{
if (Key->ArgStack == NULL)
{
ExtWarn(EE, "Trying to peek from key(%s) with nonexistant stack.\n",
Key->KeyHandle);
return;
}
if (Key->MyArgs == NULL) Key->MyArgs = Wmalloc(sizeof(ARGSTACK));
WstrcpyN((void *) Key->MyArgs, (void *) Key->ArgStack, sizeof(ARGSTACK));
}
void CopyKeys(EXTENV *EE1, EXTENV *EE2)
{
KEYS *Key, *nKey, *tKey1, *tKey2;
ARGSTACK *ap, *ap2;
if (EE2)
{
if (EE2->KeyBase)
{
nKey = (KEYS *) malloc(sizeof(KEYS));
if (EE1->KeyBase == NULL) EE1->KeyBase = nKey;
else
{
for (Key=EE1->KeyBase; Key->next; Key = Key->next);
Key->next = nKey;
}
nKey->next = NULL;
tKey1 = nKey;
for (Key=EE2->KeyBase; Key; Key = Key->next) /* for all Key in OldEnv */
{
/*
* Do not define keys that have already been defined on command line
*/
for (tKey2=EE1->KeyBase; tKey2 != tKey1; tKey2 = tKey2->next)
if (Wstrcmp(tKey2->KeyHandle, Key->KeyHandle)) break;
if (tKey2 == tKey1)
{
nKey->HanLen = Key->HanLen;
nKey->KeyHandle = Wmalloc(Key->HanLen+1);
Wstrcpy(nKey->KeyHandle, Key->KeyHandle);
nKey->Match = malloc( Wstrlen(Key->Match)+1 );
Wstrcpy(nKey->Match, Key->Match);
nKey->KeyMac = PushMacro2(EE1, 0, nKey->KeyHandle, nKey->Match);
nKey->next = (KEYS *) malloc(sizeof(KEYS));
assert(nKey->next);
nKey->next->next = NULL;
/*
* copy MyArgs and ArgStack
*/
nKey->MyArgs = (ARGSTACK *) malloc(sizeof(ARGSTACK));
WstrcpyN((void *) nKey->MyArgs, (void *) Key->MyArgs,
sizeof(ARGSTACK));
if (Key->ArgStack)
{
ap2 = nKey->ArgStack = (ARGSTACK *) malloc(sizeof(ARGSTACK));
WstrcpyN((void *) nKey->ArgStack, (void *) Key->ArgStack,
sizeof(ARGSTACK));
for (ap=Key->ArgStack->next; ap; ap=ap->next)
{
ap2->next = (ARGSTACK *) malloc(sizeof(ARGSTACK));
ap2 = ap2->next;
WstrcpyN((void *) ap2, (void *) ap, sizeof(ARGSTACK));
}
ap2->next = NULL;
}
else nKey->ArgStack = NULL;
nKey = nKey->next;
}
}
if (nKey == EE1->KeyBase) EE1->KeyBase = nKey->next;
else
{
for (Key=EE1->KeyBase; Key->next != nKey; Key = Key->next);
Key->next = nKey->next;
}
if (nKey) free(nKey);
}
}
}
KEYS *IsKeyLn(EXTENV *EE, char *line)
{
int i;
KEYS *Key;
for (i=0; !Mciswspace(line[i]) && line[i]; i++);
i++;
for(Key=EE->KeyBase; Key; Key = Key->next)
if (Key->HanLen == i)
if ( WstrcmpN(Key->KeyHandle, line, Key->HanLen) ) break;
return(Key);
}
void HandleKeyLn(EXTENV *EE, char *line, KEYS *Key)
{
char *cptr, *cp, tln[LNLEN];
int i, j, k, argmatch, OneLn=0, CHSTAK=0;
WORDS *wp, *wbase;
void FindKeyMatch(EXTENV *EE, KEYS *Key);
Wsafe_slowcase(line, LNLEN);
/*
* If it's a 1-line keyline
*/
for (i=0; (line[i]) && (line[i] != '`'); i++);
if (line[i])
{
OneLn = i + 1;
line[i] = '\0';
PushKey(EE, Key);
}
i = Key->HanLen;
cptr = Key->MyArgs->KeyArgs;
/*
* Check if we are changing the argstack
*/
if (line[i] == '@')
{
if ( WstrcmpN(&line[i], "@printargs", 10) )
{
i = 0;
if (Key->MyArgs->Not)
{
line[i++] = '!';
line[i++] = ' ';
}
Wstrcpy(&line[i], cptr);
fprintf(Warn, "Keyargs: \'%s\'\n", line);
return;
}
CHSTAK = 1;
if ( WstrcmpN(&line[i], "@push", 5) )
{
PushKey(EE, Key);
PeekKey(EE, Key);
}
else if ( WstrcmpN(&line[i], "@pop", 4) ) PopKey(EE, Key);
else if ( WstrcmpN(&line[i], "@peek", 5) ) PeekKey(EE, Key);
else CHSTAK = 0;
}
/*
* See if we are adding/removing to/from MyArgs
*/
if (!CHSTAK)
{
if ( (line[i] == '-') || (line[i] == '+') )
{
i += 2;
Wstrcpy(tln, line+i);
wbase = CreateKeyLine0(EE, NULL, tln);
for(wp=wbase; wp; wp = wp->next)
{
k = Wfnd_substr(cptr, wp->word);
j = Wstrlen(wp->word);
if (line[i-2] == '+')
{
if (Key->MyArgs->Not)
{
if (k) Wstrcpy(&cptr[k-1], &cptr[k-1+j]);
}
else if (!k)
{
k = Wstrlen(cptr);
if (k == 0) cptr[k++] = ' ';
keycpy(&cptr[k], wp->word);
}
}
else
{
if (Key->MyArgs->Not)
{
if (!k)
{
k = Wstrlen(cptr);
if (k == 0) cptr[k++] = ' ';
keycpy(&cptr[k], wp->word);
}
}
else if (k) Wstrcpy(&cptr[k-1], &cptr[k-1+j]);
}
}
KillWords(wbase);
}
/*
* Otherwise, making a new MyArgs
*/
else
{
if (line[i] == '!')
{
Key->MyArgs->Not = 1;
i += 2;
}
else Key->MyArgs->Not = 0;
cptr[0] = ' ';
Wstrcpy(tln, line+i-1);
cp = CreateKeyLine(EE, tln);
if (cp)
{
Wstrcpy(cptr+1, cp);
free(cp);
}
else cptr[1] = '\0';
}
}
argmatch = Wfnd_substr(Key->MyArgs->KeyArgs, Key->Match);
if (Key->MyArgs->Not) argmatch = !argmatch;
if (OneLn)
{
/*
* Get one line, and handle it
*/
if (argmatch)
{
Wstrcpy(line, &line[OneLn]);
for(i=0; line[i]; i++);
while (line[i] != '`') i--;
line[i++] = '\n';
line[i] = '\0';
HandleLine(EE, line);
}
/*
* Restore key to previous state
*/
PopKey(EE, Key);
i = Wstrcpy(line, Key->KeyHandle);
if (Key->MyArgs->Not)
{
line[i++] = '!';
line[i++] = ' ';
}
Wstrcpy(&line[i], Key->MyArgs->KeyArgs);
HandleLine(EE, line);
}
else if (!argmatch) FindKeyMatch(EE, Key);
}
void FindKeyMatch(EXTENV *EE, KEYS *Key)
{
int j, k, argmatch=0;
static char line[LNLEN], tline[LNLEN];
j = Key->HanLen + 1;
k = EE->Joining;
EE->Joining = 0;
while(!argmatch)
{
argmatch = !GetLn(EE, line);
if (EE->ExtCmndOn[EC_MacSub]) MacroSub(EE, line);
keycpy(tline, line);
if (!argmatch)
{
argmatch = WstrcmpN(tline, Key->KeyHandle, Key->HanLen);
if (argmatch) HandleKeyLn(EE, tline, Key);
}
}
EE->Joining = k;
}
/*===========================================================================*/
/* Rest of file is misc category :-) */
/*===========================================================================*/
int icalc(EXTENV *EE, char line[])
{
int i, k=0;
int istack[128];
if ( Mcisnum(line[0]) || (line[0] == '-' && Mcisnum(line[1])) )
i = Wstr2int(line, &istack[k]);
else ExtErr(EE, "Non-numeric string in @iexp: \'%s\'", line);
while ( Mciswspace(line[i]) ) i++;
while(line[i])
{
if ( Mcisnum(line[i]) || (line[i] == '-' && Mcisnum(line[i+1])) )
i += Wstr2int(&line[i], &istack[++k]);
else
{
switch(line[i++])
{
case '+':
istack[k-1] += istack[k];
k--;
break;
case '-':
istack[k-1] = istack[k] - istack[k-1];
k--;
break;
case '/':
istack[k-1] = istack[k] / istack[k-1];
k--;
break;
case '*':
istack[k-1] = istack[k] * istack[k-1];
k--;
break;
case '%':
istack[k-1] = istack[k] % istack[k-1];
k--;
break;
case '=': /* boolean comparison */
istack[k-1] = (istack[k] == istack[k-1]);
k--;
break;
case '!': /* boolean comparison */
istack[k-1] = (istack[k] != istack[k-1]);
k--;
break;
case '<': /* less than boolean comparison */
istack[k-1] = (istack[k] < istack[k-1]);
k--;
break;
case '>': /* greater than boolean comparison */
istack[k-1] = (istack[k] > istack[k-1]);
k--;
break;
case '{': /* less than or equal boolean comparison */
istack[k-1] = (istack[k] >= istack[k-1]);
k--;
break;
case '}': /* greater than or equal boolean comparison */
istack[k-1] = (istack[k] >= istack[k-1]);
k--;
break;
case '&': /* bitwise and */
istack[k-1] = (istack[k] & istack[k-1]);
k--;
break;
case '|': /* bitwise or */
istack[k-1] = (istack[k] | istack[k-1]);
k--;
break;
case '^': /* bitwise exclusive or */
istack[k-1] = (istack[k] ^ istack[k-1]);
k--;
break;
case '~': /* bitwise complement */
istack[k] = ~(istack[k]);
break;
case 'l': /* bitwise left shift */
istack[k-1] = (istack[k] << istack[k-1]);
k--;
break;
case 'r': /* bitwise right shift */
istack[k-1] = (istack[k] >> istack[k-1]);
k--;
break;
case 'a': /* absolute value */
if (istack[k] < 0) istack[k] = -istack[k];
break;
#if 0
case 'p': /* print stack */
for (j=0; j <= k; j++) printf(" %d ",istack[j]);
printf("!\n");
break;
#endif
default:
ExtErr(EE,
"Unrecognized character/non-numeric string in @iexp: \'%s\'",
line);
}
}
while ( Mciswspace(line[i]) ) i++;
}
return(istack[k]);
}
void MakeLnButtUgly(EXTENV *EE, char *line)
{
char comm='*', ext='$';
int i, k;
static int LNSKIP=1;
if (EE->Flags[F_Ugly] == 3)
{
comm = '!';
ext = '&';
}
/*
* All comment lines begin with '*'; all other lines must be upcase
*/
if ( !Mciswspace(line[0]) && !Mcisnum(line[0]) ) line[0] = comm;
else
{
/* fprintf(stderr, "line0=%c, %d, %d\n",line[0], !Mciswspace(line[0]), !Mcisnum(line[0])); */
for (i=0; Mciswspace(line[i]); i++);
/*
* if line is blank, make it a comment
*/
if (line[i] == '\0')
{
if (EE->Flags[F_Ugly] != 3)
{
line[0] = comm;
line[1] = '\n';
line[2] = '\0';
return;
}
}
if (EE->Flags[F_Case] == 1) Wsafe_supcase(line, 72);
else if (EE->Flags[F_Case] == 2) Wsafe_slowcase(line, 72);
/*
* Use only $ for continuation
*/
if ( !Mciswspace(line[5]) )
{
if ( (line[5]) && (line[0] != comm) ) line[5] = ext;
}
else /* Right justify labels to col 5 */
{
for (i=0; ( (i < 5) && (!Mcisnum(line[i])) ); i++);
if (i < 5)
{
sscanf(&line[i], "%d", &k);
for (i=0; i < 5; i++) line[i] = ' ';
sprintf(line, "%5d", k);
line[5] = ' ';
}
}
}
/*
* Change lines of ---- to blank lines
*/
if (EE->Flags[F_Ugly] == 2)
{
if (line[0] == comm)
{
for (i=1; ( (Mciswspace(line[i]) || line[i] == '-') ); i++);
if (line[i] == '\0')
{
if (LNSKIP) line[0] = '\0';
else
{
line[0] = comm;
line[1] = '\n';
line[2] = '\0';
}
LNSKIP = !LNSKIP;
}
}
}
}
void HandleIfdef(EXTENV *EE, char line[])
{
char tline[LNLEN];
int i, j, k=7, defined, KeepOn;
if (line[7] == '!')
{
k++;
while(Mciswspace(line[k])) k++;
}
for(i=k; !Mciswspace(line[i]); i++) tline[i-k] = line[i];
tline[i-k] = '\0';
/* defined = (int) FindMac(tline); */
defined = (FindMac(tline) != NULL);
if (line[7] == '!') defined = !defined;
if (GetLn(EE, line)) KeepOn = 1;
else KeepOn = 0;
if (KeepOn) j = commandcpy(tline, line);
if (defined) /* loop over calls to HandleLine */
{
while( KeepOn )
{
if (j == 9) KeepOn = !WstrcmpN(tline, "@endifdef ", 10);
if (KeepOn)
{
HandleLine(EE, line);
KeepOn = GetLn(EE, line);
if (!KeepOn)
fprintf(stderr, "File %s ended inside of an @ifdef\n",
EE->FpIn->Fnam);
j = commandcpy(tline, line);
}
}
}
else /* do not call HandleLine while skipping */
{
i = EE->Joining;
EE->Joining = 0;
while( KeepOn )
{
if (j == 9)
{
if (WstrcmpN(tline, "@endifdef ", 10)) KeepOn--;
}
else if (j == 6) if (WstrcmpN(tline, "@ifdef ", 7)) KeepOn++;
if (KeepOn)
{
if (!GetLn(EE, line))
{
fprintf(stderr, "File %s ended inside of %d @ifdefs\n",
EE->FpIn->Fnam, KeepOn);
KeepOn = 0;
}
else if (EE->ExtCmndOn[EC_MacSub]) MacroSub(EE, line);
j = commandcpy(tline, line);
}
}
EE->Joining = i;
}
}
void HandleDec(EXTENV *EE, char *line)
{
char tline[LNLEN];
char *outln, *endstr=NULL;
char *cas, ch, Ab, Ae;
enum LANG WhatLang;
int i, k, EndLen=0, istart, istart2=(-1), LnLen;
int EXTENDLINE=1, SORT=1;
WORDS *wptr, *wp, *wp0;
LnLen = EE->Flags[F_LnLen];
if (EE->Flags[F_Lang] == 'c')
{
WhatLang = LangC;
if (LnLen == 0) LnLen = 79;
Ab = '[';
Ae = ']';
}
else if (EE->Flags[F_Lang] == 'm')
{
WhatLang = LangMake;
if (LnLen == 0) LnLen = 79;
Ae = Ab = '\"';
}
else
{
if (EE->Flags[F_Lang] == '9')
{
WhatLang = LangF90;
if (LnLen == 0) LnLen = 77;
}
else
{
WhatLang = LangF77;
if (LnLen == 0) LnLen = 71;
}
Ab = '(';
Ae = ')';
}
switch(EE->Flags[F_Case])
{
case 0:
cas = NULL;
break;
case 1:
ch = 'u';
cas = &ch;
break;
case 2:
ch = 'l';
cas = &ch;
break;
}
outln = malloc(LnLen+10);
for (i=10; line[i] != '\"'; i++) outln[i-10] = line[i];
Wsafe_mws2spc(&line[i+1]);
if (line[i+1])
{
EXTENDLINE = (Mupcase(line[i+2]) != 'N');
if ( (line[i+2]) && (line[i+3]) )
{
SORT = (Mupcase(line[i+4]) != 'N');
if ( (line[i+4]) && (line[i+5]) )
{
if (line[i+6] == '\"')
{
for (k=i+7; line[k] != '\"'; k++) if (!line[k]) ExtErr(EE, "Unmatched quote on line %d of basefile %s", EE->FpIn->LineNo, EE->FpIn->Fnam);
EndLen = k - (i+7);
endstr = (char *) malloc(EndLen+1);
for (k=0; k != EndLen; k++) endstr[k] = line[k+i+7];
endstr[EndLen] = '\0';
k += i + 8;
if (line[k]) k++;
}
else k = i + 6;
if ( Mcisnum(line[k]) )
{
Wstr2int(&line[k], &istart2);
istart2--;
}
}
}
}
istart = i - 10;
if (istart2 == -1) istart2 = istart;
wp0 = wptr = fGetVars(EE, "@enddeclare ", 12, line, tline, Ab, Ae);
if (EXTENDLINE)
{
if (WhatLang == LangMake) LnLen -= 2;
}
else if (WhatLang == LangC) LnLen--;
if (SORT) wp0 = wptr = SortWords(cas, wptr);
/*
* After sorting, find last parameter and add end string to it
*/
if (endstr)
{
for (wp=wptr; wp->next; wp = wp->next);
sprintf(tline, "%s%s", wp->word, endstr);
wp0 = wptr = KillWord(wp0, wp);
if (wp0)
{
for (wp=wptr; wp->next; wp = wp->next);
wp->next = GetWord(tline);
}
else
wp0 = wptr = GetWord(tline);
}
while(wptr)
{
for(i=istart; i <= LnLen; i++)
{
outln[i] = wptr->word[i-istart];
if (!outln[i]) break;
}
if (i > LnLen)
{
if (istart == istart2) /* parameter too long to fit on any line */
ExtErr(EE, "Parameter \'%s\' too long to fit on @DECLARE line",
wptr->word);
i = istart;
if (!EXTENDLINE)
{
i -= 2;
if (WhatLang == LangC) outln[i++] = ';';
}
else
{
if (WhatLang == LangMake) outln[i++] = '\\';
else i--;
if (EE->Flags[F_Lang] == '9')
{
while(i <= LnLen) outln[i++] = ' ';
i = LnLen + 1;
outln[i++] = ' ';
outln[i++] = '&';
}
else if (EE->Flags[F_Ugly] == 3)
{
while(i < 72) outln[i++] = ' ';
i = 72;
outln[i++] = '&';
}
}
outln[i++] = '\n';
outln[i] = '\0';
HandleLine(EE, outln);
istart = istart2;
if (EXTENDLINE)
{
for(i=0; i < istart2; i++) outln[i] = ' ';
if (EE->Flags[F_Lang] == 'f') outln[5] = '$';
}
}
else
{
wptr = wptr->next;
if (WhatLang != LangMake) outln[i++] = ',';
outln[i++] = ' ';
istart = i;
}
}
if (WhatLang == LangMake) i--;
else i -= 2;
if (endstr) free(endstr);
outln[i++] = '\n';
outln[i] = '\0';
KillWords(wp0);
HandleLine(EE, outln);
free(outln);
}
void HandleMultidef(EXTENV *EE, char *line)
{
WORDS *wp, *wp0;
char tline[LNLEN], cline[LNLEN];
char *han=line+10;
int i, j, k;
for (i=10; ((!Mciswspace(line[i])) && line[i]); i++);
if (line[i]) line[i++] = '\0';
for (j=i; ( (line[j]) && Mciswspace(line[j]) ); j++);
if (line[j]) /* one-line multidef */
wp0 = GetWords(&line[j]);
else
{
han = Wmalloc(i-10);
Wstrcpy(han, line+10);
k = EE->Joining;
EE->Joining = 0;
wp0 = fGetWords(EE, "@endmultidef ", 13, line, tline);
EE->Joining = k;
}
for (wp=wp0; wp; wp = wp->next) PushMacro2(EE, EE->Flags[F_FragMac], han, wp->word);
KillWords(wp0);
if (han != line+10) free(han);
}
void DumpSkip(EXTENV *EE, FILE *fpout, char *begstr, char *endstr)
/*
* Dumps to file/skips lines between matching begstr and endstr
*/
{
char ln[LNLEN], tln[LNLEN];
int ibeg, iend, KeepOn=1, i, k;
k = EE->Joining;
EE->Joining = 0;
ibeg = Wstrlen(begstr);
iend = Wstrlen(endstr);
do
{
if ( GetLn(EE, ln) )
{
i = commandcpy(tln, ln) + 1;
if (EE->ExtCmndOn[EC_MacSub]) MacroSub(EE, tln);
if (i == ibeg) if ( WstrcmpN(tln, begstr, ibeg) ) KeepOn++;
if (i == iend) if ( WstrcmpN(tln, endstr, iend) ) KeepOn--;
if (fpout && KeepOn) fputs(ln, fpout);
}
else break;
}
while (KeepOn);
if (fpout) fflush(fpout);
EE->Joining = k;
}
void HandleWhile(EXTENV *EE, char line[])
{
FILE2 tfp, *OldFp;
WORDS *wp, *wp2;
char tline[LNLEN];
char *han;
int KeepOn, i;
static int inest=0;
inest++;
/*
* Define while line definitions
*/
wp2 = GetWords(&line[10]);
if (wp2 == NULL) ExtErr(EE, "Malformed @whiledef=\'%s\'",line);
i = Wstrlen(wp2->word) + 1;
han = Wmalloc(i);
Wstrcpy(han, wp2->word);
for (wp=wp2->next; wp; wp = wp->next)
PushMacro2(EE, EE->Flags[F_FragMac], han, wp->word);
KillWords(wp2);
/*
* Open temporary file, and dump while loop to it
*/
sprintf(tfp.Fnam, "Whiledeftmpfile%d", inest);
tfp.LineNo = 0;
tfp.Fp = tmpfile(); /* fopen(tfp.Fnam, "w+"); */
DumpSkip(EE, tfp.Fp, "@whiledef ", "@endwhile ");
OldFp = EE->FpIn;
tfp.prev = OldFp;
EE->FpIn = &tfp;
while ( FindMac(han) )
{
rewind(tfp.Fp);
while( GetLn(EE, line) ) HandleLine(EE, line);
PopMacro2(EE, han);
}
EE->FpIn = OldFp;
fclose(tfp.Fp);
free(han);
inest--;
}
char GetIntComp(EXTENV *EE, char *ln, int *A1CONST, int *A2CONST,
int *ia1, int *ia2, char *mac1, char *mac2)
/*
* Expects comparison line of form arg1 [<,>,!,=] arg2;
* This routine finds arg1 and arg2. Note that if
* arg1 or arg2 is a macro, it cannot begin with a number or -
* Returns type of comparison (<, =, !)
*/
{
EXTMAC *mp;
WORDS *wp, *wp0, *wp1;
char comp = '<';
*ia1 = *ia2 = *A1CONST = *A2CONST = 0;
if (mac1) *mac1 = '\0';
if (mac2) *mac2 = '\0';
wp = GetWords(ln);
if (wp == NULL || wp->next == NULL || wp->next->next == NULL)
ExtErr(EE, "Invalid integer condition: '%s'\n", ln);
{ wp0 = wp; wp1 = wp->next->next; }
if (wp->next->word[0] == '>') { wp1 = wp; wp0 = wp->next->next; }
else if (wp->next->word[0] == '=') comp = '=';
else if (wp->next->word[0] == '!') comp = '!';
else if (wp->next->word[0] == '{') comp = '{';
else if (wp->next->word[0] == '}') comp = '}';
else if (wp->next->word[0] != '<')
ExtErr(EE, "Invalid integer condition: '%s'\n", ln);
if (Mcisnum(wp0->word[0]) || wp0->word[0] == '-')
{
*A1CONST = 1;
if (sscanf(wp0->word, "%d", ia1) != 1)
ExtErr(EE, "Invalid integer condition: '%s'\n", ln);
}
else if (mac1) sprintf(mac1, "@(%s)", wp0->word);
else
{
*A1CONST = 1;
mp = FindMac(wp0->word);
if (!mp) ExtErr(EE, "Undefined macro in integer condition: '%s'\n", ln);
if (sscanf(mp->Sub, "%d", ia1) != 1)
ExtErr(EE, "Invalid macro in integer condition: '%s'\n", ln);
}
if (Mcisnum(wp1->word[0]) || wp1->word[0] == '-')
{
*A2CONST = 1;
if (sscanf(wp1->word, "%d", ia2) != 1)
ExtErr(EE, "Invalid integer condition: '%s'\n", ln);
}
else if (mac2) sprintf(mac2, "@(%s)", wp1->word);
else
{
*A1CONST = 1;
mp = FindMac(wp1->word);
if (!mp) ExtErr(EE, "Undefined macro in integer condition: '%s'\n", ln);
if (sscanf(mp->Sub, "%d", ia2) != 1)
ExtErr(EE, "Invalid macro in integer condition: '%s'\n", ln);
}
KillWords(wp);
return(comp);
}
int Getiarg(EXTENV *EE, int ARGCONST, int ival, char *mac)
{
int i;
char ln[LNLEN];
if (ARGCONST) return(ival);
Wstrcpy(ln, mac);
MacroSub(EE, ln);
if (sscanf(ln, "%d", &i) != 1)
ExtErr(EE, "Invalid @iwhile macro: '%s'\n", mac);
return(i);
}
int MIfCond(EXTENV *EE, char *ln)
/*
* Expects ln of machandle1 [=,!,~] machandle2
* where machandle is either or macro handle, or if it begins with ",
* a string literal (white spaces have to come from @^, and there is no
* matching closing ")
* comparators: = : equal; ! : not equal; ~ : is substring of
*/
{
WORDS *wp;
EXTMAC *mp1, *mp2;
char *comp1, *comp2;
int iret=0;
wp = GetWords(ln);
if (!wp || !wp->next || !wp->next->next)
ExtErr(EE, "Invalid macro condition: '%s'\n", ln);
if (wp->word[0] == '"') /* word is string literal, not macro */
comp1 = wp->word + 1;
else
{
mp1 = FindMac(wp->word);
if (!mp1) ExtErr(EE, "Undefined macro in condition: '%s'\n", ln);
comp1 = mp1->Sub;
}
if (wp->next->next->word[0] == '"') /* word is string literal, not macro */
comp2 = wp->next->next->word + 1;
else
{
mp2 = FindMac(wp->next->next->word);
if (!mp2) ExtErr(EE, "Undefined macros in condition: '%s'\n", ln);
comp2 = mp2->Sub;
}
if (wp->next->word[0] == '=') iret = Wstrcmp(comp2, comp1);
else if (wp->next->word[0] == '!') iret = !Wstrcmp(comp2, comp1);
else if (wp->next->word[0] == '~') iret = Wfnd_substr(comp2, comp1);
else ExtErr(EE, "Invalid macro condition operator: '%s'\n", ln);
KillWords(wp);
return(iret);
}
void HandleMIf(EXTENV *EE, char *ln)
/*
* Expects ln of @mif handle1 [<,>,=,!] handle2
*/
{
if ( MIfCond(EE, ln+5) )
{
SETMODE(mode, MODE_MIF);
modedepth[MCMIF]++;
}
else DumpSkip(EE, NULL, "@mif ", "@endmif ");
}
void HandleIIf(EXTENV *EE, char *ln)
/*
* Expects ln of @iif int1 [<,>,},{,=,] int2
*/
{
char ch;
int i, j, ia1, ia2;
if (WstrcmpN(ln+5, "@iexp ",6)) /* rest of line is @iexp, */
i = icalc(EE, ln+5+6);
else
{
ch = GetIntComp(EE, ln+5, &i, &j, &ia1, &ia2, NULL, NULL);
if (ch == '=') i = (ia1 == ia2);
else if (ch == '!') i = (ia1 != ia2);
else if (ch == '<') i = (ia1 < ia2);
else if (ch == '{') i = (ia1 <= ia2);
else if (ch == '}') i = (ia1 >= ia2);
}
if (!i) /* skip */
DumpSkip(EE, NULL, "@iif ", "@endiif ");
else
{
SETMODE(mode, MODE_IIF);
modedepth[MCIIF]++;
}
}
void HandleIwhile(EXTENV *EE, char *ln)
{
int A1CONST, A2CONST, ia1, ia2, KeepOn;
static int inest=0;
FILE2 tfp, *OldFp;
char mac1[HANLEN], mac2[HANLEN];
char comp;
inest++;
comp = GetIntComp(EE, ln+8, &A1CONST, &A2CONST, &ia1, &ia2, mac1, mac2);
/*
* Open temporary file, and dump while loop to it
*/
sprintf(tfp.Fnam, "Iwhiletmpfile%d", inest);
tfp.LineNo = 0;
tfp.Fp = tmpfile(); /* fopen(tfp.Fnam, "w+"); */
DumpSkip(EE, tfp.Fp, "@iwhile ", "@endiwhile ");
OldFp = EE->FpIn;
tfp.prev = OldFp;
EE->FpIn = &tfp;
ia1 = Getiarg(EE, A1CONST, ia1, mac1);
ia2 = Getiarg(EE, A2CONST, ia2, mac2);
if (comp == '<') KeepOn = (ia1 < ia2);
else if (comp == '=') KeepOn = (ia1 == ia2);
else if (comp == '!') KeepOn = (ia1 != ia2);
else if (comp == '{') KeepOn = (ia1 <= ia2);
else if (comp == '}') KeepOn = (ia1 >= ia2);
while (KeepOn)
{
rewind(tfp.Fp);
while( GetLn(EE, ln) ) HandleLine(EE, ln);
ia1 = Getiarg(EE, A1CONST, ia1, mac1);
ia2 = Getiarg(EE, A2CONST, ia2, mac2);
if (comp == '<') KeepOn = (ia1 < ia2);
else if (comp == '{') KeepOn = (ia1 <= ia2);
else if (comp == '}') KeepOn = (ia1 >= ia2);
else if (comp == '=') KeepOn = (ia1 == ia2);
else if (comp == '!') KeepOn = (ia1 != ia2);
}
EE->FpIn = OldFp;
fclose(tfp.Fp);
inest--;
}
void ApplyFlags(EXTENV *EE, char *line)
{
char tline[LNLEN], *flag;
int i, j;
INDENT *indptr;
extern INDENT *indbase;
flag = EE->Flags;
if (flag[F_RemBlank]) Wremove_trailing_blanks(line);
if (indbase)
{
Wtab2spcs(line, 8);
indptr = indbase;
do
{
j = indptr->start;
if (indptr->nspac > 0)
{
Wstrcpy(tline, &line[j]);
for (i=j; i < j + indptr->nspac; i++) line[i] = ' ';
Wstrcpy(&line[i], tline);
}
else if (indptr->nspac < 0) Wstrcpy(&line[j+indptr->nspac], &line[j]);
indptr = indptr->next;
}
while (indptr != indbase);
}
else if (flag[F_RepTab]) Wtab2spcs(line, flag[F_RepTab]);
if (flag[F_Ugly]) MakeLnButtUgly(EE, line);
else
{
if (flag[F_Case] == 1) Wsafe_supcase(line, LNLEN);
else if (flag[F_Case] == 2) Wsafe_slowcase(line, LNLEN);
}
if (flag[F_Clint])
{
if (flag[F_Lang] == 'f')
if (!( !Mciswspace(line[0]) && !Mcisnum(line[0]) ))
Wsafe_slowcase(line, 72);
}
if (flag[F_LLWarn])
{
if ( (flag[F_LLWarn] > 1) || (flag[F_Lang] != 'f') ||
Mciswspace(line[0]) || Mcisnum(line[0]) )
{
i = Wstrlen(line) - 1;
while (Mciswspace(line[i])) i--;
if (i > flag[F_LnLen])
fprintf(Warn,
"\nWARNING: Line %d of file \'%s\' is %d characters long!\n",
EE->FpIn->LineNo, EE->FpIn->Fnam, i+1);
}
}
}
INDENT *AddIndent(EXTENV *EE, int start, int nspaces)
{
INDENT *indptr;
indptr = (INDENT *) malloc(sizeof(INDENT));
if (indbase == NULL)
{
indbase = indptr;
indbase->next = indbase;
indbase->prev = indbase;
}
else
{
indptr->prev = indbase->prev;
indptr->next = indbase;
indbase->prev->next = indptr;
indbase->prev = indptr;
}
indptr->start = start - 1;
indptr->nspac = nspaces;
return(indptr);
}
void KillIndent(EXTENV *EE, INDENT *indptr)
{
INDENT *ip;
if (indbase)
{
if (indptr == indbase) ip = indbase;
else for(ip=indbase->next; ip != indptr && ip != indbase; ip = ip->next);
if (ip != indptr) ExtErr(EE, "Error in indention freeing");
ip->prev->next = ip->next;
ip->next->prev = ip->prev;
if (ip == indbase)
{
if (indbase->next == indbase) indbase = NULL;
else indbase = indbase->next;
free(ip);
}
}
else ExtWarn(EE, "unmatched @ENDINDENT");
}
void PrintUsage()
{
fprintf(Warn, "\nExtract v%s: Written by R. Clint Whaley.\n", version);
fprintf(Warn, "Report bugs to: rwhaley@cs.utk.edu,\n");
fprintf(Warn, "_after_ scoping the homepage: www.cs.utk.edu/~rwhaley/EXTRACT/Extract.html\n");
fprintf(Warn,
"\nExtract3.0: Victor\'s Revenge -- bells, whistles, a gong, and\n");
fprintf(Warn, " a bag hanging off the side.\n");
fprintf(Warn,
"\nExtract3.1: Andy Strikes Back -- bells, whistles, a gong, \n");
fprintf(Warn,
" a bag hanging off the side, and a toupee on top.\n");
fprintf(Warn,
"\nExtract4.0: This Time, Its Personal -- bells, whistles, a gong, \n");
fprintf(Warn,
" a bag hanging off the side, a toupee on top, and\n");
fprintf(Warn,
" chrome all around.\n");
fprintf(Warn, "\nUser's guide and docs at:\n");
fprintf(Warn, "\n www.cs.utk.edu/~rwhaley/EXTRACT/Extract.html\n\n");
fprintf(Warn, "\nUSAGE:");
fprintf(Warn,
"\n\nextract [-<flags>] [key1=match1, ..., keyN=matchN]\n\n");
fprintf(Warn,
"Those items in [] are optional, while those in <> are to be\n");
fprintf(Warn,"replaced by the appropriate string.\n");
fprintf(Warn,
"All flags have the option 0. This means do not perform the\n");
fprintf(Warn,
"operation indicated by the flag. For instance -case0 means do not change case.\n");
fprintf(Warn,
"This can be used to override the defaults setup by your ~/.extractrc file.\n");
fprintf(Warn, "\nThe available flags are:\n");
fprintf(Warn,
" -o <outfile> : use file <outfile> for output rather than stdout.\n");
fprintf(Warn,
" -b <basefile> : use file <basefile> for input rather than stdin.\n");
fprintf(Warn,
" -case[0,U,L]: change case of output file. U=upcase, L=lowcase.\n");
fprintf(Warn, " -RepTab[0][#]: replace tabs with # spaces. # defaults to 8.\n");
fprintf(Warn,
" -Remtblank[0]: remove trailing blanks at the end of each line\n");
fprintf(Warn, " -LAPACK[0,1,2,3]: Make code look like LAPACK coding style.\n");
fprintf(Warn, " -verb[0,1,2,3]: Vary the verbosity of extract.\n");
fprintf(Warn, " -LnLen[0]#: set maximal number of columns for line length warning.\n");
fprintf(Warn, " -LLWarn[0,1,2]: Line length warnings: 0: none, 1: non-comment, 2: always\n");
fprintf(Warn,
" -fmode[0,Q,A]: File mode. 0: default - overwrite if file exists;\n");
fprintf(Warn, " Q: Query before overwriting, A: Append.\n");
fprintf(Warn, " -lang[0,f77,C,M] Language extracted file is.\n");
fprintf(Warn, " -addkeys[0]: Makes it so keys are always inherited.\n");
fprintf(Warn, " -punymac[0]: Makes it so macros in in-line extract files are automatically\n");
fprintf(Warn,
" popped before returning to the @extract line.\n");
fprintf(Warn, " NOTE: this flag is never inherited.\n");
fprintf(Warn,
" -punyflags[0]: Says that @extract being done should not inherit\n");
fprintf(Warn, " present flag settings.\n");
fprintf(Warn,
" -localprocs[0]: If true, makes it so extract procedures and functions are\n");
fprintf(Warn,
" undefined before returning to the calling @extract line\n");
fprintf(Warn, " -def <handle> \"<replacement>\".\n");
fprintf(Warn, " -indent <col> <nspaces>\n");
fprintf(Warn,
" -trans \"Ln#1, ..., Ln#N\": translate extracted line numbers to basefile\n");
fprintf(Warn, " line numbers.\n");
fprintf(Warn,
" -no@<extract,skip,def,ifdef,whiledef,indent,key,abort,macsub,endext,print,exp,declare,addkey,echo,iwhile,proc,output,iif,mif,all>[0]:\n");
fprintf(Warn, " turn off basefile commands.\n");
exit(1);
}
void ExtInit(EXTENV *EE, WORDS *wp)
{
char *fin=NULL, *fout=NULL, *flags, *ec, *access;
char line[LNLEN];
int i, j, err;
WORDS *p, *p2;
EE->clindent = NULL;
EE->MyPunyMacs = NULL;
EE->MyProcs = NULL;
EE->Joining = 1;
p = wp;
flags = EE->Flags;
ec = EE->ExtCmndOn;
access = "w";
EE->KeyBase = NULL;
while (p)
{
err = 0;
Wsafe_slowcase(p->word, LNLEN);
/*
* parameter is flag
*/
if (p->word[0] == '-')
{
if ( WstrcmpN(p->word+1, "multidef", 8) )
{
p2 = p = p->next;
if (!p) ExtErr(NULL, "No handle for commandline macro definition");
if (!p->next)
ExtErr(NULL, "No substring for commandline macro definition");
while (p->next)
{
if (p->next->word[0] == '-') break;
sprintf(line, "@define %s @%s@\n",p2->word, p->next->word);
PushMacro(EE, line);
p = p->next;
}
}
else if ( WstrcmpN(&p->word[1], "def", 3) )
{
/*
* Push commandline macro onto stack
*/
p = p->next;
if (!p) ExtErr(NULL, "No handle for commandline macro definition");
if (!p->next)
ExtErr(NULL, "No substring for commandline macro definition");
PushMacro2(EE, 1, p->word, p->next->word);
p = p->next;
}
else if ( (p->word[1] == 'o') && (p->word[2] == '\0') )
{
p = p->next;
if (p) fout = p->word;
}
else if ( (p->word[1] == 'b') && (p->word[2] == '\0') )
{
p = p->next;
if (p) fin = p->word;
}
else if ( WstrcmpN(&p->word[1], "case", 4) )
{
if (p->word[5] == 'u') flags[F_Case] = 1;
else if (p->word[5] == 'l') flags[F_Case] = 2;
else if (p->word[5] == '0' || p->word[5] == '\0') flags[F_Case] = 0;
else err = 1;
}
else if ( WstrcmpN(&p->word[1], "reptab", 6) )
{
if (p->word[7] == '\0') flags[F_RepTab] = 8;
else
{
sscanf(&p->word[7], "%d", &i);
flags[F_RepTab] = i;
}
}
else if ( WstrcmpN(&p->word[1], "remtblank", 9) )
{
flags[F_RemBlank] = (p->word[10] != '0');
}
else if ( WstrcmpN(&p->word[1], "lapack", 6) )
{
if (p->word[7] == '0') flags[F_Ugly] = 0;
else if (p->word[7] == '2') flags[F_Ugly] = 2;
else if (p->word[7] == '3') flags[F_Ugly] = 3;
else
{
flags[F_Ugly] = 1;
flags[F_Case] = 1;
}
}
else if ( WstrcmpN(&p->word[1], "verb", 4) )
{
if (p->word[5] == '1') flags[F_Verb] = 1;
else if (p->word[5] == '2') flags[F_Verb] = 2;
else if (p->word[5] == '3') flags[F_Verb] = 3;
else flags[F_Verb] = 0;
}
else if ( WstrcmpN(&p->word[1], "lnlen", 5) )
{
sscanf(&p->word[6], "%d", &i);
flags[F_LnLen] = i;
}
else if ( WstrcmpN(&p->word[1], "llwarn", 6) )
{
sscanf(&p->word[7], "%d", &i);
flags[F_LLWarn] = i;
}
else if ( WstrcmpN(&p->word[1], "fmode", 5) )
{
if (p->word[6] == 'a') access = "a";
else access = "w";
flags[F_Query] = (p->word[6] == 'q');
}
else if ( WstrcmpN(&p->word[1], "lang", 4) )
{
flags[F_Lang] = 'f';
if (p->word[5] == 'c') flags[F_Lang] = 'c';
else if (p->word[5] == 'm') flags[F_Lang] = 'm';
else if (p->word[5] == 'f')
if (p->word[6] == '9') flags[F_Lang] = '9';
}
else if ( WstrcmpN(&p->word[1], "trans", 5) )
{
fprintf(Warn, "\nFlag \'-trans\' not yet implemented.\n");
}
else if ( WstrcmpN(&p->word[1], "addkeys", 7) )
{
if (p->word[8] == '0') flags[F_AddKeys] = 0;
else flags[F_AddKeys] = 1;
}
else if ( WstrcmpN(&p->word[1], "localprocs", 10) )
{
if (p->word[11] == '0') flags[F_LocalProcs] = 0;
else flags[F_LocalProcs] = 1;
}
else if ( WstrcmpN(&p->word[1], "punymac", 7) )
{
if (p->word[8] == '0') flags[F_FragMac] = 0;
else flags[F_FragMac] = 1;
}
else if ( WstrcmpN(&p->word[1], "punyflags", 9) )
{
if (p->word[10] == '0') flags[F_FragFlag] = 0;
else flags[F_FragFlag] = 1;
}
else if ( WstrcmpN(&p->word[1], "clint", 5) )
{
if (p->word[6] == '0') flags[F_Clint] = 0;
else flags[F_Clint] = 1;
}
else if ( WstrcmpN(&p->word[1], "indent", 6) )
{
p = p->next;
sscanf(p->word, "%d", &i);
p = p->next;
sscanf(p->word, "%d", &j);
EE->clindent = AddIndent(EE, i, j);
}
else if ( WstrcmpN(&p->word[1], "help", 4) ) PrintUsage();
else if ( WstrcmpN(&p->word[1], "no@", 3) )
{
if ( WstrcmpN(&p->word[4], "extract", 7) )
ec[EC_Extract] = p->word[11] == '0';
else if ( WstrcmpN(&p->word[4], "skip", 4) )
ec[EC_Skip] = p->word[8] == '0';
else if ( WstrcmpN(&p->word[4], "def", 3) )
ec[EC_Define] = p->word[7] == '0';
else if ( WstrcmpN(&p->word[4], "ifdef", 5) )
ec[EC_Ifdef] = p->word[9] == '0';
else if ( WstrcmpN(&p->word[4], "whiledef", 8) )
ec[EC_While] = p->word[12] == '0';
else if ( WstrcmpN(&p->word[4], "indent", 6) )
ec[EC_Indent] = p->word[10] == '0';
else if ( WstrcmpN(&p->word[4], "key", 3) )
ec[EC_Key] = p->word[7] == '0';
else if ( WstrcmpN(&p->word[4], "abort", 5) )
ec[EC_Abort] = p->word[9] == '0';
else if ( WstrcmpN(&p->word[4], "macsub", 6) )
ec[EC_MacSub] = p->word[10] == '0';
else if ( WstrcmpN(&p->word[4], "endext", 6) )
ec[EC_EndExt] = p->word[10] == '0';
else if ( WstrcmpN(&p->word[4], "print", 5) )
ec[EC_EndExt] = p->word[9] == '0';
else if ( WstrcmpN(&p->word[4], "exp", 3) )
ec[EC_Exp] = p->word[7] == '0';
else if ( WstrcmpN(&p->word[4], "declare", 7) )
ec[EC_Dec] = p->word[7] == '0';
else if ( WstrcmpN(&p->word[4], "addkey", 6) )
ec[EC_AddKey] = p->word[6] == '0';
else if ( WstrcmpN(&p->word[4], "echo", 4) )
ec[EC_Echo] = p->word[4] == '0';
else if ( WstrcmpN(&p->word[4], "iwhile", 6) )
ec[EC_Iwhile] = p->word[6] == '0';
else if ( WstrcmpN(&p->word[4], "output", 6) )
ec[EC_Output] = p->word[6] == '0';
else if ( WstrcmpN(&p->word[4], "iif", 3) )
ec[EC_Iif] = p->word[6] == '0';
else if ( WstrcmpN(&p->word[4], "mif", 3) )
ec[EC_Mif] = p->word[6] == '0';
else if ( WstrcmpN(&p->word[4], "all", 3) )
{
if (p->word[7] == '0') CharSet(EC_nExtCmnds, ec, 1);
else
{
CharSet(EC_nExtCmnds, ec, 0);
ec[EC_Output] = 1;
}
}
else err = 1;
}
else err = 1;
if (err) fprintf(Warn, "\n\nUnknown flag \'%s\' ignored.\n\n", p->word);
}
/*
* Parameter is setting up keys
*/
else AddKey(EE, p->word);
p = p->next;
}
if (fin) EE->FpIn = OpenFile(EE, fin, "r");
if (fout) EE->FpOut = OpenFile(EE, fout, access);
KillWords(wp);
/*
* Must set LnLen if we want warnings
*/
if (flags[F_LLWarn])
{
if (!flags[F_LnLen])
{
if (flags[F_Lang] == 'f') flags[F_LnLen] = 71;
else flags[F_LnLen] = 80;
}
}
/*
* Never replace tabs in Makefiles
*/
if (flags[F_Lang] == 'm') flags[F_RepTab] = 0;
}
void Extract(EXTENV *OldEnv, WORDS *wp)
{
char line[LNLEN];
EXTENV EE;
PUNYMAC *pp;
KEYS *kp;
EXTPROC *fup;
void KillProc(EXTENV *EE, EXTPROC *fpkill);
EE.FpIn = NULL;
EE.FpOut = NULL;
if (OldEnv)
{
if (OldEnv->Flags[F_FragFlag]) CharSet(F_nFlags, EE.Flags, 0);
else WstrcpyN((void *) EE.Flags, (void *) OldEnv->Flags, F_nFlags);
EE.Flags[F_FragMac] = 0;
WstrcpyN((void *) EE.ExtCmndOn, (void *) OldEnv->ExtCmndOn, EC_nExtCmnds);
fflush(OldEnv->FpOut->Fp); /* flush oldenv's outfile */
}
else
{
CharSet(F_nFlags, EE.Flags, 0);
EE.Flags[F_Lang] = 'f';
CharSet(EC_nExtCmnds, EE.ExtCmndOn, 1);
}
/*
* If no input/output or keys are specified, they are inherited
*/
if (OldEnv) EE.FpIn = OldEnv->FpIn;
if (OldEnv) EE.FpOut = OldEnv->FpOut;
ExtInit(&EE, wp);
if (EE.FpIn == NULL) EE.FpIn = OpenFile(&EE, "stdin", "r");
if (EE.FpOut == NULL) EE.FpOut = OpenFile(&EE, "stdout", "w");
/*
* Print 'extract starting' message, if desired
*/
if (EE.Flags[F_Verb])
{
if (!OldEnv)
{
fprintf(Warn, "\nBegin commandline extract on files: in=\'%s\', out=\'%s\'.\n",
EE.FpIn->Fnam, EE.FpOut->Fnam);
}
else if (EE.Flags[F_Verb] > 1)
{
fprintf(Warn, " Begin basefile extract on files: in=\'%s\', out=\'%s\'.\n",
EE.FpIn->Fnam, EE.FpOut->Fnam);
}
}
/*
* If keys are inherited, they do not affect OldEnv's keys, so we must copy
*/
if ( (!EE.KeyBase) || (EE.Flags[F_AddKeys]) ) CopyKeys(&EE, OldEnv);
/*
* Store where my macros begin
*/
sprintf(line, "@__MyMacBeg__%p", &EE);
PushMacro2(&EE, 0, line, "");
EE.MyMacBeg = MacroBase;
/*
* Read in and handle the file
*/
while( GetLn(&EE, line) )
{
HandleLine(&EE, line);
}
ExtDone = 0;
/*
* Print 'extract done' message, if desired
*/
if (EE.Flags[F_Verb])
{
if (!OldEnv)
{
fprintf(Warn,
"Done commandline extract. Files: in=\'%s\', out=\'%s\'; Lines: in=%d, out=%d.\n",
EE.FpIn->Fnam, EE.FpOut->Fnam, EE.FpIn->LineNo, EE.FpOut->LineNo);
}
else if (EE.Flags[F_Verb] > 1)
{
fprintf(Warn,
" Done basefile extract. Files: in=\'%s\', out=\'%s\'; Lines: in=%d, out=%d.\n",
EE.FpIn->Fnam, EE.FpOut->Fnam, EE.FpIn->LineNo, EE.FpOut->LineNo);
}
}
/*
* Close files
*/
if (OldEnv == NULL) CloseFile(&EE, EE.FpIn);
else if (EE.FpIn != OldEnv->FpIn) CloseFile(&EE, EE.FpIn);
if (OldEnv == NULL) CloseFile(&EE, EE.FpOut);
else if (EE.FpOut != OldEnv->FpOut) CloseFile(&EE, EE.FpOut);
/*
* Undefine puny macros
*/
while(pp = EE.MyPunyMacs)
{
EE.MyPunyMacs = pp->next;
PopThisMacro(&EE, pp->mp);
free(pp);
}
/*
* If Fragile macros are set, free all macros defined by this extract session
*/
if (EE.Flags[F_FragMac]) KillMyMacros(&EE);
/*
* Pop MyMacBeg
*/
sprintf(line, "@__MyMacBeg__%p", &EE);
PopMacro2(&EE, line);
/*
* Free my keys
*/
while(kp = EE.KeyBase) KillThisKey(&EE, EE.KeyBase);
/*
* Free my procedures
*/
if (EE.MyProcs)
{
while (AllProcs != EE.MyProcs)
{
fup = AllProcs->next;
KillProc(&EE, AllProcs);
AllProcs = fup;
}
fup = AllProcs->next;
KillProc(&EE, AllProcs);
AllProcs = fup;
}
if (EE.clindent) KillIndent(&EE, EE.clindent);
}
/*
* ==================================================================
* Extract procedures and functions
* ==================================================================
*/
void KillProc0(EXTENV *EE, EXTPROC **basep, EXTPROC **myprocs, EXTPROC *ppkill)
{
EXTPROC *fp;
if (ppkill)
{
free(ppkill->ProcNam);
free(ppkill->FileNam);
KillWords(ppkill->argnams);
if (ppkill == *basep)
{
*basep = (*basep)->next;
if (ppkill == *myprocs) *myprocs = NULL;
}
else
{
for (fp=(*basep); fp->next != ppkill ; fp = fp->next);
if (ppkill == *myprocs) *myprocs = fp;
fp->next = ppkill->next;
}
free(ppkill);
}
}
void KillProc(EXTENV *EE, EXTPROC *fpkill)
{
KillProc0(EE, &AllProcs, &(EE->MyProcs), fpkill);
}
void KillFunc(EXTENV *EE, EXTPROC *fpkill)
{
KillProc0(EE, &AllFuncs, &(EE->MyFuncs), fpkill);
}
EXTPROC *FindProc(EXTENV *EE, EXTPROC *basep, char *proc)
{
EXTPROC *pp;
for (pp=basep; pp; pp = pp->next)
if (Wstrcmp(proc, pp->ProcNam)) break;
return(pp);
}
void PopProc(EXTENV *EE, char *proc)
{
EXTPROC *pp;
if (AllProcs)
{
pp = FindProc(EE, AllProcs, proc);
if (pp) KillProc(EE, pp);
else ExtWarn(EE, "Nonsensical @UNDEFPROC: %s", proc);
}
else ExtWarn(EE, "Nonsensical @UNDEFPROC: %s", proc);
}
void PopFunc(EXTENV *EE, char *func)
{
EXTPROC *pp;
if (AllFuncs)
{
pp = FindProc(EE, AllFuncs, func);
if (pp) KillFunc(EE, pp);
else ExtWarn(EE, "Nonsensical @UNDEFFUNC: %s", func);
}
else ExtWarn(EE, "Nonsensical @UNDEFFUNC: %s", func);
}
void AddProc0(EXTENV *EE, EXTPROC **basep, EXTPROC **myprocs, EXTPROC *fpadd)
{
if (*myprocs == NULL) *myprocs = fpadd;
fpadd->next = *basep;
*basep = fpadd;
}
void PushProc0(EXTENV *EE, EXTPROC **basep, EXTPROC **myfuncs, char *ln)
/*
* expects ln of form "<procnam> [arg1 ... argN]"
*/
{
EXTPROC *pp;
WORDS *wp, *wpbase;
FILE *fp;
char *cp;
int i;
pp = malloc(sizeof(EXTPROC));
wpbase = wp = GetWords(ln);
if (!wpbase)
{
if (*basep == AllProcs)
ExtErr(EE, "@BEGINPROC must have a PROCNAME");
else ExtErr(EE, "@BEGINFUNC must have a FUNCNAME");
}
i = Wstrlen(wp->word) + 1;
pp->ProcNam = malloc(i);
Wstrcpy(pp->ProcNam, wp->word);
pp->argnams = KillWord(wp, wp);
pp->nargs = CountWords(pp->argnams);
cp = tmpnam(NULL);
if (cp == NULL) ExtErr(EE, "Out of tmpnams!!!");
i = Wstrlen(cp) + 1;
pp->FileNam = malloc(i*sizeof(char));
Wstrcpy(pp->FileNam, cp);
/*
* Dump procedure to tmpfile
*/
fp = fopen(pp->FileNam, "w");
if (*basep == AllProcs) DumpSkip(EE, fp, "@beginproc ", "@endproc ");
else DumpSkip(EE, fp, "@beginfunc ", "@endfunc ");
fclose(fp);
AddProc0(EE, basep, myfuncs, pp);
}
void PushProc(EXTENV *EE, char *ln)
/*
* expects ln of form "@beginproc <procnam> [arg1 ... argN]"
*/
{
PushProc0(EE, &AllProcs, &(EE->MyProcs), ln+11);
}
void PushFunc(EXTENV *EE, char *ln)
/*
* expects ln of form "@beginfunc <procnam> [arg1 ... argN]"
*/
{
PushProc0(EE, &AllFuncs, &(EE->MyFuncs), ln+11);
}
void HandleProcCall0(EXTENV *EE, EXTPROC *basep, char *ln)
/*
* expects ln of form "<proc/func> [arg1 ... argN]"
*/
{
WORDS *wbase, *wp, *ew, *ap, *dp;
EXTPROC *pp;
wbase = wp = GetWords(ln);
pp = FindProc(EE, basep, wp->word);
if (!pp) ExtErr(EE, "Call to undefined proc/func %s", wp->word);
ew = GetWord("-b");
ew->next = GetWord(pp->FileNam);
wp = ew->next;
if (EE->Flags[F_LocalProcs] || basep == AllFuncs)
{
wp->next = GetWord("-punymac");
wp = wp->next;
if (basep == AllFuncs)
{
wp->next = GetWord("-no@output");
wp = wp->next;
}
}
for (dp=wbase->next, ap = pp->argnams; ap; ap = ap->next, dp = dp->next)
{
wp->next = GetWord("-def");
wp = wp->next;
wp->next = GetWord(ap->word);
wp = wp->next;
wp->next = GetWord(dp->word);
wp = wp->next;
}
KillWords(wbase);
Extract(EE, ew);
}
void HandleProcCall(EXTENV *EE, char *ln)
/*
* expects ln of form "@callproc <procnam> [arg1 ... argN]"
*/
{
HandleProcCall0(EE, AllProcs, ln+10);
}
int LnIsExtCmnd(EXTENV *EE, char *line)
{
int icalc(EXTENV *EE, char line[]);
int i, j, k, KeepOn, DONE=1;
char tline[LNLEN], *Use;
INDENT *indptr;
WORDS *wp, *wp0;
KEYS *Key;
Use = EE->ExtCmndOn;
/*
* If first non-whitespace is not an @, line is not extract command
*/
for (i=0; Mciswspace(line[i]); i++);
if (line[i] != '@') return(0);
i = commandcpy(tline, &line[i]) + 1;
switch(i)
{
case 4: /* alias for @skip does not work in middle of line! */
if (WstrcmpN(tline, "@// ", 3))
{
if ( !Use[EC_Skip] ) return(0);
}
else DONE = 0;
break;
case 5:
if (WstrcmpN(tline, "@iif ", 4))
{
if ( !Use[EC_Iif] ) return(0);
HandleIIf(EE, tline);
}
else if (WstrcmpN(tline, "@mif ", 4))
{
if ( !Use[EC_Mif] ) return(0);
HandleMIf(EE, tline);
}
else DONE = 0;
break;
case 6:
if (WstrcmpN(tline, "@skip ", 6))
{
if ( !Use[EC_Skip] ) return(0);
}
else if (WstrcmpN(tline, "@echo ", 6))
{
if (Use[EC_Echo])
{
for (k=0; line[k] != '@'; k++);
k += 6;
PutLn(EE, line+k);
}
else return(0);
}
else if (WstrcmpN(tline, "@iexp ", 6))
{
if ( !Use[EC_Exp] ) return(0);
for (i=6; Mciswspace(tline[i]); i++);
j=0;
while(!Mciswspace(tline[i])) line[j++] = tline[i++]; /* get mac handle */
line[j] = '\0';
keycpy(tline, &tline[i]);
i = icalc(EE, tline);
PopMacro2(EE, line); /* replace any previous definition */
sprintf(tline, "%d", i);
PushMacro2(EE, EE->Flags[F_FragMac], line, tline);
}
else DONE = 0;
break;
case 7:
if (WstrcmpN(tline, "@undef ", 7))
{
if ( Use[EC_Define] )
{
if ( !PopMacro(EE, tline) )
ExtWarn(EE, "Nonsensical @UNDEF: %s", line);
}
else return(0);
}
else if (WstrcmpN(tline, "@ifdef ", 7))
{
if (!Use[EC_Ifdef]) return(0);
HandleIfdef(EE, tline);
}
else if (WstrcmpN(tline, "@abort ", 7))
{
if (Use[EC_Abort])
{
CloseFile(EE, EE->FpIn);
CloseFile(EE, EE->FpOut);
ExtErr(EE, "Extract aborted with message `%s`", &tline[7]);
exit(1);
}
else return(0);
}
else if (WstrcmpN(tline, "@print ", 7))
{
if (Use[EC_Print]) fprintf(Warn, "%s", &tline[7]);
else return(0);
}
else DONE = 0;
break;
case 8:
if (WstrcmpN(tline, "@define ", 8))
{
if ( Use[EC_Define] ) PushMacro(EE, tline);
else return(0);
}
else if (WstrcmpN(tline, "@system ", 8))
{
i = Wstrcpy(line, &tline[9]);
while (line[i] != '\"') i--;
line[i++] = '\n';
line[i++] = '\0';
system(line);
}
else if (WstrcmpN(tline, "@iwhile ", 8))
{
if (!Use[EC_Iwhile]) return(0);
HandleIwhile(EE, tline);
}
else if (WstrcmpN(tline, "@endmif ", 8))
{
if ( !Use[EC_Mif] ) return(0);
if (INMODE(mode, MODE_MIF))
{
if (modedepth[MCMIF] > 0)
{
modedepth[MCMIF]--;
if (!(modedepth[MCMIF])) KILLMODE(mode, MODE_MIF);
}
else ExtErr(EE, "Internal mode error: `%s`", tline);
}
else ExtWarn(EE, "Nonsensical @ENDMIF");
}
else if (WstrcmpN(tline, "@endiif ", 8))
{
if ( !Use[EC_Iif] ) return(0);
if (INMODE(mode, MODE_IIF))
{
if (modedepth[MCIIF] > 0)
{
modedepth[MCIIF]--;
if (!(modedepth[MCIIF])) KILLMODE(mode, MODE_IIF);
}
else ExtErr(EE, "Internal mode error: `%s`", tline);
}
else ExtWarn(EE, "Nonsensical @ENDIIF");
}
else DONE = 0;
break;
case 9:
if (WstrcmpN(tline, "@extract ", 9))
{
if ( Use[EC_Extract] )
{
Wstrcpy(tline, &tline[9]);
wp = GetArgs(EE, tline);
Extract(EE, wp);
}
else return(0);
}
else if (WstrcmpN(tline, "@declare ", 9))
{
if (!Use[EC_Dec]) return(0);
HandleDec(EE, tline);
}
else if (WstrcmpN(tline, "@addkeys ",9))
{
if ( Use[EC_AddKey] )
{
wp0 = wp = GetWords(&tline[9]);
while (wp)
{
AddKey(EE, wp->word);
wp = wp->next;
}
KillWords(wp0);
}
else return(0);
}
else if (WstrcmpN(tline, "@endskip ", 9))
{
if ( Use[EC_Skip] ) ExtWarn(EE, "unmatched @ENDSKIP");
else return(0);
}
else if (WstrcmpN(tline, "@endproc ", 9))
{
if ( Use[EC_Proc] ) ExtWarn(EE, "unmatched @ENDPROC");
else return(0);
}
else DONE = 0;
break;
case 10:
if (WstrcmpN(tline, "@whiledef ", 10))
{
if (!Use[EC_While]) return(0);
HandleWhile(EE, tline);
}
else if (WstrcmpN(tline, "@multidef ", 10))
{
if (!Use[EC_Define]) return(0);
HandleMultidef(EE, tline);
}
else if (WstrcmpN(tline, "@callproc ", 10))
{
if (!Use[EC_Proc]) return(0);
HandleProcCall(EE, tline);
}
else if (WstrcmpN(tline, "@killkeys ", 10))
{
if ( Use[EC_AddKey] )
{
wp0 = wp = GetWords(&tline[10]);
while (wp)
{
KillKey(EE, wp->word);
wp = wp->next;
}
KillWords(wp0);
}
else return(0);
}
else if (WstrcmpN(tline, "@endwhile ", 10))
{
if ( Use[EC_While] ) ExtWarn(EE, "unmatched @ENDWHILE");
else return(0);
}
else if (WstrcmpN(tline, "@endifdef ", 10))
{
if ( Use[EC_Ifdef] ) ExtWarn(EE, "unmatched @ENDIFDEF");
else return(0);
}
else if (WstrcmpN(tline, "@undefall ", 10))
{
if ( Use[EC_Define] )
{
keycpy(tline, &tline[10]);
/*
* If undefining all of one handle
*/
if (tline[0])
{
Wstrcpy(line, "@undef ");
i = 10;
for (i=10; ( !Mciswspace(tline[i]) && tline[i] ); i++)
line[i-3] = tline[i];
line[i-3] = '\0';
while( PopMacro(EE, line) );
}
/*
* If undefining all handles
*/
else KillMyMacros(EE);
}
else return(0);
}
else DONE = 0;
break;
case 11:
if (WstrcmpN(tline, "@beginskip ", 11))
{
if ( Use[EC_Skip] ) DumpSkip(EE, NULL, "@beginskip ", "@endskip ");
else return(0);
}
else if (WstrcmpN(tline, "@endindent ", 11))
{
if ( Use[EC_Indent] ) KillIndent(EE, indbase->prev);
else return(0);
}
else if (WstrcmpN(tline, "@beginproc ", 11))
{
if (Use[EC_Proc]) PushProc(EE, tline);
else return(0);
}
else if (WstrcmpN(tline, "@endiwhile ", 11))
{
if ( Use[EC_Iwhile] ) ExtWarn(EE, "unmatched @ENDIWHILE");
else return(0);
}
else DONE = 0;
break;
case 12:
if (WstrcmpN(tline, "@enddeclare ", 12))
{
if ( Use[EC_Dec] ) ExtWarn(EE, "unmatched @enddeclare");
else return(0);
}
if (WstrcmpN(tline, "@print@nmac ", 12))
{
if (Use[EC_Print])
fprintf(Warn, "%d:%s", CountMacros(), tline+12);
else
return(0);
}
else if (WstrcmpN(tline, "@endextract ", 12))
{
if ( Use[EC_EndExt] ) ExtDone = 1;
else return(0);
}
else DONE = 0;
break;
case 13:
if (WstrcmpN(tline, "@beginindent ", 13))
{
if ( Use[EC_Indent] )
{
keycpy(tline, line);
sscanf(&tline[13], "%d", &j);
for (i=13; Mcisnum(tline[i]); i++);
sscanf(&tline[++i], "%d", &k);
AddIndent(EE, j, k);
}
else return(0);
}
else DONE = 0;
break;
case 15:
if (WstrcmpN(tline, "@print@allmacs ", 15))
{
if (Use[EC_Print])
{
int i;
EXTMAC *p;
for (i=0, p=MacroBase; p; i++,p=p->next)
{
fprintf(Warn, "'%10s' -> '%s'\n", p->Handle, p->Sub);
}
fprintf(Warn, "Done %d macros.\n\n", i);
}
else
return(0);
}
else DONE = 0;
break;
default:
DONE = 0;
break;
}
/*
* Must be @<keyhandle>, a line continuation (@\) or an unassociated @ sign
*/
if (!DONE)
{
if (Use[EC_Key])
{
keycpy(tline, line);
Key = IsKeyLn(EE, tline);
if (Key) HandleKeyLn(EE, tline, Key);
else
{
if (tline[1] != '\\') /* don't warn about line continuation */
{
if ( !(tline[1] == '(' && tline[2] == '@' && tline[3] == ')') )
ExtWarn(EE, "Non-associated @");
}
return(0);
}
}
else return(0);
}
return(1);
}
void HandleLine(EXTENV *EE, char *line)
{
/*
* If line is not an extract command, apply flags and write to file
*/
if (EE->ExtCmndOn[EC_MacSub]) MacroSub(EE, line);
if ( !LnIsExtCmnd(EE, line) )
{
if (EE->ExtCmndOn[EC_Output]) PutLn(EE, line);
else ExtErr(EE, "Output in prohibited mode: %s", line);
}
}
main(int nargs, char *args[])
{
char line[LNLEN], *path;
int i;
FILE *fp;
WORDS *wp, *wp2=NULL, *wp3;
Warn = stderr;
path = getenv("HOME");
i = Wstrcpy(line, path);
Wstrcpy(&line[i], "/.extractrc");
fp = fopen(line, "r");
if (fp)
{
if ( fgets(line, LNLEN, fp) ) wp2 = GetWords(line);
fclose(fp);
}
if (nargs > 1)
{
wp3 = wp = GetWord(args[1]);
for (i=2; i < nargs; i++)
{
wp->next = GetWord(args[i]);
wp = wp->next;
}
wp = wp3;
if (wp2) /* add commandline words to end of .extractrc words */
{
for (wp3=wp2; wp3->next; wp3 = wp3->next);
wp3->next = wp;
wp = wp2;
}
}
else if (fp == NULL) wp = GetWord("-help");
else wp = wp2;
Extract(NULL, wp);
exit(0);
}
/************************************************************************/
/* This program is distributed under the terms of the Gnu */
/* General Public License (GPL), with the following two exceptions: */
/* (1) Clause (9), dealing with updating the GPL automatically, is */
/* specifically disallowed by the author. The author will */
/* determine if a newer GPL version is still appropriate. */
/* (2) The basefiles extract accepts as input, and the extracted */
/* files it produces as output, are specifically designated as */
/* as outside the scope if this license (i.e. they are *not* */
/* required by this license to be GPL). */
/* The full, unaltered, text of the GPL is included below. */
/************************************************************************/
/*
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc. 675 Mass
Ave, Cambridge, MA 02139, USA. Everyone is permitted to copy and
distribute verbatim copies of this license document, but changing it
is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change
free software--to make sure the software is free for all its users.
This General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit
to using it. (Some other Free Software Foundation software is
covered by the GNU Library General Public License instead.) You can
apply it to your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that
you have the freedom to distribute copies of free software (and
charge for this service if you wish), that you receive source code
or can get it if you want it, that you can change the software or
use pieces of it in new free programs; and that you know you can do
these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the
rights. These restrictions translate to certain responsibilities for
you if you distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights
that you have. You must make sure that they, too, receive or can get
the source code. And you must show them these terms so they know
their rights.
We protect your rights with two steps: (1) copyright the software,
and (2) offer you this license which gives you legal permission to
copy, distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on,
we want its recipients to know that what they have is not the
original, so that any problems introduced by others will not reflect
on the original authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making
the program proprietary. To prevent this, we have made it clear that
any patent must be licensed for everyone's free use or not licensed
at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program",
below, refers to any such program or work, and a "work based on the
Program" means either the Program or any derivative work under
copyright law: that is to say, a work containing the Program or a
portion of it, either verbatim or with modifications and/or
translated into another language. (Hereinafter, translation is
included without limitation in the term "modification".) Each
licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the
Program is covered only if its contents constitute a work based on
the Program (independent of having been made by running the
Program). Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any
warranty; and give any other recipients of the Program a copy of
this License along with the Program.
You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange for
a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any part
thereof, to be licensed as a whole at no charge to all third parties
under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a notice
that there is no warranty (or else, saying that you provide a
warranty) and that users may redistribute the program under these
conditions, and telling the user how to view a copy of this License.
(Exception: if the Program itself is interactive but does not
normally print such an announcement, your work based on the Program
is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work
based on the Program, the distribution of the whole must be on the
terms of this License, whose permissions for other licensees extend
to the entire whole, and thus to each and every part regardless of
who wrote it.
Thus, it is not the intent of this section to claim rights or
contest your rights to work written entirely by you; rather, the
intent is to exercise the right to control the distribution of
derivative or collective works based on the Program.
In addition, mere aggregation of another work not based on the
Program with the Program (or with a work based on the Program) on a
volume of a storage or distribution medium does not bring the other
work under the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms
of Sections 1 and 2 above provided that you also do one of the
following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections 1
and 2 above on a medium customarily used for software interchange;
or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your cost
of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer to
distribute corresponding source code. (This alternative is allowed
only for noncommercial distribution and only if you received the
program in object code or executable form with such an offer, in
accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as
a special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this
License. However, parties who have received copies, or rights, from
you under this License will not have their licenses terminated so
long as such parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject
to these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted
herein. You are not responsible for enforcing compliance by third
parties to this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do
not excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under
this License and any other pertinent obligations, then as a
consequence you may not distribute the Program at all. For example,
if a patent license would not permit royalty-free redistribution of
the Program by all those who receive copies directly or indirectly
through you, then the only way you could satisfy both it and this
License would be to refrain entirely from distribution of the
Program.
If any portion of this section is held invalid or unenforceable
under any particular circumstance, the balance of the section is
intended to apply and the section as a whole is intended to apply in
other circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is
willing to distribute software through any other system and a
licensee cannot impose that choice.
This section is intended to make thoroughly clear what is believed
to be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces,
the original copyright holder who places the Program under this
License may add an explicit geographical distribution limitation
excluding those countries, so that distribution is permitted only in
or among countries not thus excluded. In such case, this License
incorporates the limitation as if written in the body of this
License.
9. The Free Software Foundation may publish revised and/or new
versions of the General Public License from time to time. Such new
versions will be similar in spirit to the present version, but may
differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the
Program specifies a version number of this License which applies to
it and "any later version", you have the option of following the
terms and conditions either of that version or of any later version
published by the Free Software Foundation. If the Program does not
specify a version number of this License, you may choose any version
ever published by the Free Software Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the
author to ask for permission. For software which is copyrighted by
the Free Software Foundation, write to the Free Software Foundation;
we sometimes make exceptions for this. Our decision will be guided
by the two goals of preserving the free status of all derivatives of
our free software and of promoting the sharing and reuse of software
generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND
PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE
DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR
CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
SUCH DAMAGES.
END OF TERMS AND CONDITIONS
*/
|