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 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136
|
#!/usr/bin/perl
#
# pwget -- batch download files possibly with configuration file
#
# Copyright
#
# Copyright (C) 1996-2016 Jari Aalto <jari.aalto@cante.net>
#
# License
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Documentation
#
# To read manual, start this program with option: --help
# "Named Capture Buffers" are used
use 5.10.0;
# ****************************************************************************
#
# Globals
#
# ****************************************************************************
use vars qw ( $VERSION );
# This is for use of Makefile.PL and ExtUtils::MakeMaker
#
# The following variable is updated by Emacs setup whenever
# this file is saved.
$VERSION = '2016.1019.1354';
# ****************************************************************************
#
# Standard perl modules
#
# ****************************************************************************
use strict;
use autouse 'Carp' => qw( croak carp cluck confess );
use autouse 'Text::Tabs' => qw( expand );
use autouse 'File::Copy' => qw( copy move );
use autouse 'File::Path' => qw( mkpath rmtree );
use autouse 'Pod::Html' => qw( pod2html );
#use autouse 'Pod::Text' => qw( pod2text );
use Cwd;
use Env;
use English;
use File::Basename;
use Getopt::Long;
use Net::FTP;
IMPORT:
{
use Env;
use vars qw
(
$PATH
$HOME
$TEMP
$TEMPDIR
$SHELL
);
}
# ****************************************************************************
#
# Modules from CPAN
#
# ****************************************************************************
use LWP::UserAgent;
# ****************************************************************************
#
# DESCRIPTION
#
# Set global variables for the program
#
# INPUT PARAMETERS
#
# none
#
# RETURN VALUES
#
# none
#
# ****************************************************************************
sub Initialize ()
{
use vars qw
(
$PROGNAME
$LIB
$LICENSE
$AUTHOR
$URL
$WIN32
$CYGWIN_PERL
);
$LIB = basename $PROGRAM_NAME;
$PROGNAME = $LIB;
$LICENSE = "GPL-2+";
$AUTHOR = "Jari Aalto";
$URL = "http://freecode.com/projects/perl-webget";
$WIN32 = 1 if $OSNAME =~ /win32/i;
if ( $OSNAME =~ /cygwin/i )
{
# We need to know if this perl is Cygwin native perl?
use vars qw( %Config );
eval "use Config";
$EVAL_ERROR and die "$EVAL_ERROR";
if ( $main::Config{osname} =~ /cygwin/i )
{
$CYGWIN_PERL = 1;
}
}
$OUTPUT_AUTOFLUSH = 1;
# This variable holds the current tag line being used.
use vars qw( $CURRENT_TAG_LINE );
}
# ***************************************************************** &help ****
#
# DESCRIPTION
#
# Print help and exit.
#
# INPUT PARAMETERS
#
# $msg [optional] Reason why function was called.
#
# RETURN VALUES
#
# none
#
# ****************************************************************************
=pod
=head1 NAME
pwget - Perl Web URL fetch program
=head1 SYNOPSIS
pwget http://example.com/ [URL ...]
pwget --config $HOME/config/pwget.conf --tag linux --tag emacs ..
pwget --verbose --overwrite http://example.com/
pwget --verbose --overwrite --Output ~/dir/ http://example.com/
pwget --new --overwrite http://example.com/package-1.1.tar.gz
=head1 DESCRIPTION
Automate periodic downloads of files and packages.
If you retrieve latest versions of certain program blocks
periodically, this is the Perl script for you. Run from cron job or
once a week to upload newest versions of files around the net. Note:
=head2 Wget and this program
At this point you may wonder, where would you need this perl program
when wget(1) C-program has been the standard for ages. Well, 1) Perl
is cross platform and more easily extendable 2) You can record file
download criteria to a configuration file and use perl regular
expressions to select downloads 3) the program can analyze web-pages
and "search" for the download only links as instructed 4) last but not
least, it can track newest packages whose name has changed since last
download. There are heuristics to determine the newest file or package
according to file name skeleton defined in configuration.
This program does not replace pwget(1) because it does not offer as
many options as wget, like recursive downloads and date comparing. Use
wget for ad hoc downloads and this utility for files that change (new
releases of archives) or which you monitor periodically.
=head2 Short introduction
This small utility makes it possible to keep a list of URLs in a
configuration file and periodically retrieve those pages or files with
simple commands. This utility is best suited for small batch jobs to
download e.g. most recent versions of software files. If you use an URL
that is already on disk, be sure to supply option B<--overwrite> to allow
overwriting existing files.
While you can run this program from command line to retrieve individual
files, program has been designed to use separate configuration file via
B<--config> option. In the configuration file you can control the
downloading with separate directives like C<save:> which tells to save the
file under different name. The simplest way to retrieve the latest version
of a package from a FTP site is:
pwget --new --overwrite --verbose \
http://www.example.com/package-1.00.tar.gz
Do not worry about the filename C<package-1.00.tar.gz>. The latest
version, say, C<package-3.08.tar.gz> will be retrieved. The option
B<--new> instructs to find newer version than the provided URL.
If the URL ends to slash, then directory list at the remote machine
is stored to file:
!path!000root-file
The content of this file can be either index.html or the directory listing
depending on the used http or ftp protocol.
=head1 OPTIONS
=over 4
=item B<-A, --regexp-content REGEXP>
Analyze the content of the file and match REGEXP. Only if the regexp
matches the file content, then download file. This option will make
downloads slow, because the file is read into memory as a single line
and then a match is searched against the content.
For example to download Emacs lisp file (.el) written by Mr. Foo in
case insensitive manner:
pwget -v -r '\.el$' -A "(?i)Author: Mr. Foo" \
http://www.emacswiki.org/elisp/index.html
=item B<-C, --create-paths>
Create paths that do not exist in C<lcd:> directives.
By default, any LCD directive to non-existing directory will interrupt
program. With this option, local directories are created as needed making
it possible to re-create the exact structure as it is in configuration
file.
=item B<-c, --config FILE>
This option can be given multiple times. All configurations are read.
Read URLs from configuration file. If no configuration file is given, file
pointed by environment variable is read. See ENVIRONMENT.
The configuration file layout is available in section CONFIGURATION FILE
=item B<--chdir DIRECTORY>
Do a chdir() to DIRECTORY before any URL download starts. This is
like doing:
cd DIRECTORY
pwget http://example.com/index.html
=item B<-d, --debug [LEVEL]>
Turn on debug with positive LEVEL number. Zero means no debug.
This option turns on B<--verbose> too.
=item B<-e, --extract>
Unpack any files after retrieving them. The command to unpack typical
archive files are defined in a program. Make sure these programs are
along path. Win32 users are encouraged to install the Cygwin utilities
where these programs come standard. Refer to section SEE ALSO.
.tar => tar
.tgz => tar + gzip
.gz => gzip
.bz2 => bzip2
.xz => xz
.zip => unzip
=item B<-F, --firewall FIREWALL>
Use FIREWALL when accessing files via ftp:// protocol.
=item B<-h, --help>
Print help page in text.
=item B<--help-html>
Print help page in HTML.
=item B<--help-man>
Print help page in Unix manual page format. You want to feed this output to
c<nroff -man> in order to read it.
Print help page.
=item B<-m, --mirror SITE>
If URL points to Sourceforge download area, use mirror SITE for downloading.
Alternatively the full full URL can include the mirror information. And
example:
--mirror kent http://downloads.sourceforge.net/foo/foo-1.0.0.tar.gz
=item B<-n, --new>
Get newest file. This applies to datafiles, which do not have extension
.asp or .html. When new releases are announced, the version
number in filename usually tells which is the current one so getting
hardcoded file with:
pwget -o -v http://example.com/dir/program-1.3.tar.gz
is not usually practical from automation point of view. Adding
B<--new> option to the command line causes double pass: a) the whole
http://example.com/dir/ is examined for all files and b) files
matching approximately filename program-1.3.tar.gz are examined,
heuristically sorted and file with latest version number is retrieved.
=item B<--no-lcd>
Ignore C<lcd:> directives in configuration file.
In the configuration file, any C<lcd:> directives are obeyed as they
are seen. But if you do want to retrieve URL to your current
directory, be sure to supply this option. Otherwise the file will end
to the directory pointer by C<lcd:>.
=item B<--no-save>
Ignore C<save:> directives in configuration file. If the URLs have
C<save:> options, they are ignored during fetch. You usually want to
combine B<--no-lcd> with B<--no-save>
=item B<--no-extract>
Ignore C<x:> directives in configuration file.
=item B<-O, --output DIR>
Before retrieving any files, chdir to DIR.
=item B<-o, --overwrite>
Allow overwriting existing files when retrieving URLs.
Combine this with B<--skip-version> if you periodically update files.
=item B<--proxy PROXY>
Use PROXY server for HTTP. (See B<--Firewall> for FTP.). The port number is
optional in the call:
--proxy http://example.com.proxy.com
--proxy example.com.proxy.com:8080
=item B<-p, --prefix PREFIX>
Add PREFIX to all retrieved files.
=item B<-P, --postfix POSTFIX >
Add POSTFIX to all retrieved files.
=item B<-D, --prefix-date>
Add iso8601 ":YYYY-MM-DD" prefix to all retrieved files.
This is added before possible B<--prefix-www> or B<--prefix>.
=item B<-W, --prefix-www>
Usually the files are stored with the same name as in the URL dir, but
if you retrieve files that have identical names you can store each
page separately so that the file name is prefixed by the site name.
http://example.com/page.html --> example.com::page.html
http://example2.com/page.html --> example2.com::page.html
=item B<-r, --regexp REGEXP>
Retrieve file matching at the destination URL site. This is like "Connect
to the URL and get all files matching REGEXP". Here all gzip compressed
files are found form HTTP server directory:
pwget -v -r "\.gz" http://example.com/archive/
Caveat: currently works only for http:// URLs.
=item B<-R, --config-regexp REGEXP>
Retrieve URLs matching REGEXP from configuration file. This cancels
B<--tag> options in the command line.
=item B<-s, --selftest>
Run some internal tests. For maintainer or developer only.
=item B<--sleep SECONDS>
Sleep SECONDS before next URL request. When using regexp based
downloads that may return many hits, some sites disallow successive
requests in within short period of time. This options makes program
sleep for number of SECONDS between retrievals to overcome 'Service
unavailable'.
=item B<--stdout>
Retrieve URL and write to stdout.
=item B<--skip-version>
Do not download files that have version number and which already exists on
disk. Suppose you have these files and you use option B<--skip-version>:
package.tar.gz
file-1.1.tar.gz
Only file.txt is retrieved, because file-1.1.tar.gz contains version number
and the file has not changed since last retrieval. The idea is, that in
every release the number in in distribution increases, but there may be
distributions which do not contain version number. In regular intervals
you may want to load those packages again, but skip versioned files. In short:
This option does not make much sense without additional option B<--new>
If you want to reload versioned file again, add option B<--overwrite>.
=item B<-t, --test, --dry-run>
Run in test mode.
=item B<-T, --tag NAME [NAME] ...>
Search tag NAME from the config file and download only entries defined
under that tag. Refer to B<--config FILE> option description. You can give
Multiple B<--tag> switches. Combining this option with B<--regexp>
does not make sense and the consequences are undefined.
=item B<-v, --verbose [NUMBER]>
Print verbose messages.
=item B<-V, --version>
Print version information.
=back
=head1 EXAMPLES
Get files from site:
pwget http://www.example.com/dir/package.tar.gz ..
Display copyright file for package GNU make from Debian pages:
pwget --stdout --regexp 'copyright$' http://packages.debian.org/unstable/make
Get all mailing list archive files that match "gz":
pwget --regexp gz http://example.com/mailing-list/archive/download/
Read a directory and store it to filename YYYY-MM-DD::!dir!000root-file.
pwget --prefix-date --overwrite --verbose http://www.example.com/dir/
To update newest version of the package, but only if there is none at disk
already. The B<--new> option instructs to find newer packages and the
filename is only used as a skeleton for files to look for:
pwget --overwrite --skip-version --new --verbose \
ftp://ftp.example.com/dir/packet-1.23.tar.gz
To overwrite file and add a date prefix to the file name:
pwget --prefix-date --overwrite --verbose \
http://www.example.com/file.pl
--> YYYY-MM-DD::file.pl
To add date and WWW site prefix to the filenames:
pwget --prefix-date --prefix-www --overwrite --verbose \
http://www.example.com/file.pl
--> YYYY-MM-DD::www.example.com::file.pl
Get all updated files under configuration file's tag updates:
pwget --verbose --overwrite --skip-version --new --tag updates
pwget -v -o -s -n -T updates
Get files as they read in the configuration file to the current directory,
ignoring any C<lcd:> and C<save:> directives:
pwget --config $HOME/config/pwget.conf /
--no-lcd --no-save --overwrite --verbose \
http://www.example.com/file.pl
To check configuration file, run the program with non-matching regexp and
it parses the file and checks the C<lcd:> directives on the way:
pwget -v -r dummy-regexp
-->
pwget.DirectiveLcd: LCD [$EUSR/directory ...]
is not a directory at /users/foo/bin/pwget line 889.
=head1 CONFIGURATION FILE
=head2 Comments
The configuration file is NOT Perl code. Comments start with hash character
(#).
=head2 Variables
At this point, variable expansions happen only in B<lcd:>. Do not try
to use them anywhere else, like in URLs.
Path variables for B<lcd:> are defined using following notation, spaces are
not allowed in VALUE part (no directory names with spaces). Variable names
are case sensitive. Variables substitute environment variables with the
same name. Environment variables are immediately available.
VARIABLE = /home/my/dir # define variable
VARIABLE = $dir/some/file # Use previously defined variable
FTP = $HOME/ftp # Use environment variable
The right hand can refer to previously defined variables or existing
environment variables. Repeat, this is not Perl code although it may
look like one, but just an allowed syntax in the configuration file. Notice
that there is dollar to the right hand> when variable is referred, but no
dollar to the left hand side when variable is defined. Here is example
of a possible configuration file content. The tags are hierarchically
ordered without a limit.
Warning: remember to use different variables names in separate
include files. All variables are global.
=head2 Include files
It is possible to include more configuration files with statement
INCLUDE <path-to-file-name>
Variable expansions are possible in the file name. There is no limit how
many or how deep include structure is used. Every file is included only
once, so it is safe to to have multiple includes to the same file.
Every include is read, so put the most important override includes last:
INCLUDE <etc/pwget.conf> # Global
INCLUDE <$HOME/config/pwget.conf> # HOME overrides it
A special C<THIS> tag means relative path of the current include file,
which makes it possible to include several files form the same
directory where a initial include file resides
# Start of config at /etc/pwget.conf
# THIS = /etc, current location
include <THIS/pwget-others.conf>
# Refers to directory where current user is: the pwd
include <pwget-others.conf>
# end
=head2 Configuration file example
The configuration file can contain many <directives:>, where
each directive end to a colon. The usage of each directory is best explained
by examining the configuration file below and reading the commentary
near each directive.
# $HOME/config/pwget.conf F- Perl pwget configuration file
ROOT = $HOME # define variables
CONF = $HOME/config
UPDATE = $ROOT/updates
DOWNL = $ROOT/download
# Include more configuration files. It is possible to
# split a huge file in pieces and have "linux",
# "win32", "debian", "emacs" configurations in separate
# and manageable files.
INCLUDE <$CONF/pwget-other.conf>
INCLUDE <$CONF/pwget-more.conf>
tag1: local-copies tag1: local # multiple names to this category
lcd: $UPDATE # chdir directive
# This is show to user with option --verbose
print: Notice, this site moved YYYY-MM-DD, update your bookmarks
file://absolute/dir/file-1.23.tar.gz
tag1: external
lcd: $DOWNL
tag2: external-http
http://www.example.com/page.html
http://www.example.com/page.html save:/dir/dir/page.html
tag2: external-ftp
ftp://ftp.com/dir/file.txt.gz save:xx-file.txt.gz login:foo pass:passwd x:
lcd: $HOME/download/package
ftp://ftp.com/dir/package-1.1.tar.gz new:
tag2: package-x
lcd: $DOWNL/package-x
# Person announces new files in his homepage, download all
# announced files. Unpack everything (x:) and remove any
# existing directories (xopt:rm)
http://example.com/~foo pregexp:\.tar\.gz$ x: xopt:rm
# End of configuration file pwget.conf
=head1 LIST OF DIRECTIVES IN CONFIGURATION FILE
All the directives must in the same line where the URL is. The programs
scans lines and determines all options given in line for the URL.
Directives can be overridden by command line options.
=over 4
=item B<cnv:CONVERSION>
Currently only B<conv:text> is available.
Convert downloaded page to text. This option always needs either B<save:>
or B<rename:>, because only those directives change filename. Here is
an example:
http://example.com/dir/file.html cnv:text save:file.txt
http://example.com/dir/ pregexp:\.html cnv:text rename:s/html/txt/
A B<text:> shorthand directive can be used instead of B<cnv:text>.
=item B<cregexp:REGEXP>
Download file only if the content matches REGEXP. This is same as option
B<--Regexp-content>. In this example directory listing Emacs lisp packages
(.el) are downloaded but only if their content indicates that the Author is
Mr. Foo:
http://example.com/index.html cregexp:(?i)author:.*Foo pregexp:\.el$
=item B<lcd:DIRECTORY>
Set local download directory to DIRECTORY (chdir to it). Any environment
variables are substituted in path name. If this tag is found, it replaces
setting of B<--Output>. If path is not a directory, terminate with error.
See also B<--Create-paths> and B<--no-lcd>.
=item B<login:LOGIN-NAME>
Ftp login name. Default value is "anonymous".
=item B<mirror:SITE>
This is relevant to Sourceforge only which does not allow direct
downloads with links. Visit project's Sourceforge homepage and see
which mirrors are available for downloading.
An example:
http://sourceforge.net/projects/austrumi/files/austrumi/austrumi-1.8.5/austrumi-1.8.5.iso/download new: mirror:kent
=item B<new:>
Get newest file. This variable is reset to the value of B<--new> after the
line has been processed. Newest means, that an C<ls> command is run in the
ftp, and something equivalent in HTTP "ftp directories", and any files that
resemble the filename is examined, sorted and heuristically determined
according to version number of file which one is the latest. For example
files that have version information in YYYYMMDD format will most likely to
be retrieved right.
Time stamps of the files are not checked.
The only requirement is that filename C<must> follow the universal version
numbering standard:
FILE-VERSION.extension # de facto VERSION is defined as [\d.]+
file-19990101.tar.gz # ok
file-1999.0101.tar.gz # ok
file-1.2.3.5.tar.gz # ok
file1234.txt # not recognized. Must have "-"
file-0.23d.tar.gz # warning, letters are problematic
Files that have some alphabetic version indicator at the end of
VERSION may not be handled correctly. Contact the developer and inform
him about the de facto standard so that files can be retrieved
more intelligently.
I<NOTE:> In order the B<new:> directive to know what kind of files to
look for, it needs a file template. You can use a direct link to some
filename. Here the location "http://www.example.com/downloads" is
examined and the filename template used is took as "file-1.1.tar.gz"
to search for files that might be newer, like "file-9.1.10.tar.gz":
http://www.example.com/downloads/file-1.1.tar.gz new:
If the filename appeared in a named page, use directive B<file:> for
template. In this case the "download.html" page is examined for files
looking like "file.*tar.gz" and the latest is searched:
http://www.example.com/project/download.html file:file-1.1.tar.gz new:
=item B<overwrite:> B<o:>
Same as turning on B<--overwrite>
=item B<page:>
Read web page and apply commands to it. An example: contact the root page
and save it:
http://example.com/~foo page: save:foo-homepage.html
In order to find the correct information from the page, other
directives are usually supplied to guide the searching.
1) Adding directive C<pregexp:ARCHIVE-REGEXP> matches the A HREF links
in the page.
2) Adding directive B<new:> instructs to find newer VERSIONS of the file.
3) Adding directive C<file:DOWNLOAD-FILE> tells what template to use
to construct the downloadable file name. This is needed for the
C<new:> directive.
4) A directive C<vregexp:VERSION-REGEXP> matches the exact location in
the page from where the version information is extracted. The default
regexp looks for line that says "The latest version ... is ... N.N".
The regexp must return submatch 2 for the version number.
AN EXAMPLE
Search for newer files from a HTTP directory listing. Examine page
http://www.example.com/download/dir for model C<package-1.1.tar.gz>
and find a newer file. E.g. C<package-4.7.tar.gz> would be downloaded.
http://www.example.com/download/dir/package-1.1.tar.gz new:
AN EXAMPLE
Search for newer files from the content of the page. The directive
B<file:> acts as a model for filenames to pay attention to.
http://www.example.com/project/download.html new: pregexp:tar.gz file:package-1.1.tar.gz
AN EXAMPLE
Use directive B<rename:> to change the filename before storing it on
disk. Here, the version number is attached to the actual filename:
file.txt-1.1
file.txt-1.2
The directive needed would be as follows; entries have been broken to
separate lines for legibility:
http://example.com/files/
pregexp:\.el-\d
vregexp:(file.el-([\d.]+))
file:file.el-1.1
new:
rename:s/-[\d.]+//
This effectively reads: "See if there is new version of something that
looks like file.el-1.1 and save it under name file.el by deleting the
extra version number at the end of original filename".
AN EXAMPLE
Contact absolute B<page:> at http://www.example.com/package.html and
search A HREF urls in the page that match B<pregexp:>. In addition, do
another scan and search the version number in the page from the
position that match B<vregexp:> (submatch 2).
After all the pieces have been found, use template B<file:> to make
the retrievable file using the version number found from B<vregexp:>.
The actual download location is combination of B<page:> and A HREF
B<pregexp:> location.
The directive needed would be as follows; entries have been broken to
separate lines for legibility:
http://www.example.com/~foo/package.html
page:
pregexp: package.tar.gz
vregexp: ((?i)latest.*?version.*?\b([\d][\d.]+).*)
file: package-1.3.tar.gz
new:
x:
An example of web page where the above would apply:
<HTML>
<BODY>
The latest version of package is <B>2.4.1</B> It can be
downloaded in several forms:
<A HREF="download/files/package.tar.gz">Tar file</A>
<A HREF="download/files/package.zip">ZIP file
</BODY>
</HTML>
For this example, assume that C<package.tar.gz> is a symbolic link
pointing to the latest release file C<package-2.4.1.tar.gz>. Thus the
actual download location would have been
C<http://www.example.com/~foo/download/files/package-2.4.1.tar.gz>.
Why not simply download C<package.tar.gz>? Because then the program
can't decide if the version at the page is newer than one stored on
disk from the previous download. With version numbers in the file
names, the comparison is possible.
=item B<page:find>
FIXME: This option is obsolete. do not use.
THIS IS FOR HTTP only. Use Use directive B<regexp:> for FTP protocols.
This is a more general instruction than the B<page:> and B<vregexp:>
explained above.
Instruct to download every URL on HTML page matching B<pregexp:RE>. In
typical situation the page maintainer lists his software in the
development page. This example would download every tar.gz file in the
page. Note, that the REGEXP is matched against the A HREF link
content, not the actual text that is displayed on the page:
http://www.example.com/index.html page:find pregexp:\.tar.gz$
You can also use additional B<regexp-no:> directive if you want to exclude
files after the B<pregexp:> has matched a link.
http://www.example.com/index.html page:find pregexp:\.tar.gz$ regexp-no:desktop
=item B<pass:PASSWORD>
For FTP logins. Default value is C<nobody@example.com>.
=item B<pregexp:RE>
Search A HREF links in page matching a regular expression. The regular
expression must be a single word with no whitespace. This is
incorrect:
pregexp:(this regexp )
It must be written as:
pregexp:(this\s+regexp\s)
=item B<print:MESSAGE>
Print associated message to user requesting matching tag name.
This directive must in separate line inside tag.
tag1: linux
print: this download site moved 2002-02-02, check your bookmarks.
http://new.site.com/dir/file-1.1.tar.gz new:
The C<print:> directive for tag is shown only if user turns on --verbose
mode:
pwget -v -T linux
=item B<rename:PERL-CODE>
Rename each file using PERL-CODE. The PERL-CODE must be full perl program
with no spaces anywhere. Following variables are available during the
eval() of code:
$ARG = current file name
$url = complete url for the file
The code must return $ARG which is used for file name
For example, if page contains links to .html files that are in fact
text files, following statement would change the file extensions:
http://example.com/dir/ page:find pregexp:\.html rename:s/html/txt/
You can also call function C<MonthToNumber($string)> if the filename
contains written month name, like <2005-February.mbox>.The function will
convert the name into number. Many mailing list archives can be downloaded
cleanly this way.
# This will download SA-Exim Mailing list archives:
http://lists.merlins.org/archives/sa-exim/ pregexp:\.txt$ rename:$ARG=MonthToNumber($ARG)
Here is a more complicated example:
http://www.contactor.se/~dast/svnusers/mbox.cgi pregexp:mbox.*\d$ rename:my($y,$m)=($url=~/year=(\d+).*month=(\d+)/);$ARG="$y-$m.mbox"
Let's break that one apart. You may spend some time with this example
since the possibilities are limitless.
1. Connect to page
http://www.contactor.se/~dast/svnusers/mbox.cgi
2. Search page for URLs matching regexp 'mbox.*\d$'. A
found link could match hrefs like this:
http://svn.haxx.se/users/mbox.cgi?year=2004&month=12
3. The found link is put to $ARG (same as $_), which can be used
to extract suitable mailbox name with a perl code that is
evaluated. The resulting name must appear in $ARG. Thus the code
effectively extract two items from the link to form a mailbox
name:
my ($y, $m) = ( $url =~ /year=(\d+).*month=(\d+)/ )
$ARG = "$y-$m.mbox"
=> 2004-12.mbox
Just remember, that the perl code that follows C<rename:> directive
B<must> must not contain any spaces. It all must be readable as one
string.
=item B<regexp:REGEXP>
Get all files in ftp directory matching regexp. Directive B<save:> is
ignored.
=item B<regexp-no:REGEXP>
After the C<regexp:> directive has matched, exclude files that match
directive B<regexp-no:>
=item B<Regexp:REGEXP>
This option is for interactive use. Retrieve all files from HTTP or FTP
site which match REGEXP.
=item B<save:LOCAL-FILE-NAME>
Save file under this name to local disk.
=item B<tagN:NAME>
Downloads can be grouped under C<tagN> so that e.g. option B<--tag1> would
start downloading files from that point on until next C<tag1> is found.
There are currently unlimited number of tag levels: tag1, tag2 and tag3, so
that you can arrange your downloads hierarchically in the configuration file.
For example to download all Linux files that you monitor, you would give
option B<--tag linux>. To download only the NT Emacs latest binary, you
would give option B<--tag emacs-nt>. Notice that you do not give the
C<level> in the option, program will find it out from the configuration
file after the tag name matches.
The downloading stops at next tag of the C<same level>. That is, tag2 stops
only at next tag2, or when upper level tag is found (tag1) or or until end of
file.
tag1: linux # All Linux downloads under this category
tag2: sunsite tag2: another-name-for-this-spot
# List of files to download from here
tag2: ftp.funet.fi
# List of files to download from here
tag1: emacs-binary
tag2: emacs-nt
tag2: xemacs-nt
tag2: emacs
tag2: xemacs
=item B<x:>
Extract (unpack) file after download. See also option B<--unpack> and
B<--no-extract> The archive file, say .tar.gz will be extracted the file in
current download location. (see directive B<lcd:>)
The unpack procedure checks the contents of the archive to see if
the package is correctly formed. The de facto archive format is
package-N.NN.tar.gz
In the archive, all files are supposed to be stored under the proper
subdirectory with version information:
package-N.NN/doc/README
package-N.NN/doc/INSTALL
package-N.NN/src/Makefile
package-N.NN/src/some-code.java
C<IMPORTANT:> If the archive does not have a subdirectory for all files, a
subdirectory is created and all items are unpacked under it. The default
subdirectory name in constructed from the archive name with correct date
stamp in format:
package-YYYY.MMDD
If the archive name contains something that looks like a version number,
the created directory will be constructed from it, instead of current date.
package-1.43.tar.gz => package-1.43
=item B<xx:>
Like directive B<x:> but extract the archive C<as is>, without
checking content of the archive. If you know that it is ok for the archive
not to include any subdirectories, use this option to suppress creation
of an artificial root package-YYYY.MMDD.
=item B<xopt:rm>
This options tells to remove any previous unpack directory.
Sometimes the files in the archive are all read-only and unpacking the
archive second time, after some period of time, would display
tar: package-3.9.5/.cvsignore: Could not create file:
Permission denied
tar: package-3.9.5/BUGS: Could not create file:
Permission denied
This is not a serious error, because the archive was already on disk and
tar did not overwrite previous files. It might be good to inform the
archive maintainer, that the files have wrong permissions. It is customary
to expect that distributed packages have writable flag set for all files.
=back
=head1 ERRORS
Here is list of possible error messages and how to deal with them.
Turning on B<--debug> will help to understand how program has
interpreted the configuration file or command line options. Pay close
attention to the generated output, because it may reveal that
a regexp for a site is too lose or too tight.
=over 4
=item B<ERROR {URL-HERE} Bad file descriptor>
This is "file not found error". You have written the filename incorrectly.
Double check the configuration file's line.
=back
=head1 BUGS AND LIMITATIONS
C<Sourceforge note>: To download archive files from Sourceforge
requires some trickery because of the redirections and load balancers
the site uses. The Sourceforge page have also undergone many changes
during their existence. Due to these changes there exists an ugly hack
in the program to use wget(1) to get certain information from the site.
This could have been implemented in pure Perl, but as of now the
developer hasn't had time to remove the wget(1) dependency. No doubt,
this is an ironic situation to use wget(1). You you have Perl skills,
go ahead and look at UrlHttGet(). UrlHttGetWget() and sen patches.
The program was initially designed to read options from one line. It
is unfortunately not possible to change the program to read
configuration file directives from multiple lines, e.g. by using
backslashes (\) to indicate continued line.
=head1 ENVIRONMENT
Variable C<PWGET_CFG> can point to the root configuration file. The
configuration file is read at startup if it exists.
export PWGET_CFG=$HOME/conf/pwget.conf # /bin/hash syntax
setenv PWGET_CFG $HOME/conf/pwget.conf # /bin/csh syntax
=head1 EXIT STATUS
Not defined.
=head1 DEPENDENCIES
External utilities:
wget(1) only needed for Sourceforge.net downloads
see BUGS AND LIMITATIONS
Non-core Perl modules from CPAN:
LWP::UserAgent
Net::FTP
The following modules are loaded in run-time only if directive
B<cnv:text> is used. Otherwise these modules are not loaded:
HTML::Parse
HTML::TextFormat
HTML::FormatText
This module is loaded in run-time only if HTTPS scheme is used:
Crypt::SSLeay
=head1 SEE ALSO
lwp-download(1)
lwp-mirror(1)
lwp-request(1)
lwp-rget(1)
wget(1)
=head1 AUTHOR
Jari Aalto
=head1 LICENSE AND COPYRIGHT
Copyright (C) 1996-2016 Jari Aalto
This program is free software; you can redistribute and/or modify
program under the terms of GNU General Public license either version 2
of the License, or (at your option) any later version.
=cut
sub Help (;$ $)
{
my $id = "$LIB.Help";
my $msg = shift; # optional arg, why are we here...
my $type = shift; # optional arg, type
if ( $type eq -html )
{
pod2html $PROGRAM_NAME;
}
elsif ( $type eq -man )
{
eval { require Pod::Man; 1 }
or die "$id: Cannot generate Man: $EVAL_ERROR";
# Other option: name, section, release
#
my %options;
$options{center} = 'Perl pwget URL fetch utility';
my $parser = Pod::Man->new(%options);
$parser->parse_from_file ($PROGRAM_NAME);
}
else
{
system "pod2text $PROGRAM_NAME";
}
exit 0;
}
# ****************************************************************************
#
# DESCRIPTION
#
#
#
# INPUT PARAMETERS
#
# $path Path name
# $type -win32 convert to win32 path.
# -cygwin convert to cygwin path.
#
# RETURN VALUES
#
#
#
# ****************************************************************************
sub PathConvert ($;$)
{
my $id = "$LIB.PathConvertDosToCygwin";
local $ARG = shift;
my $type = shift;
my $ret = $ARG;
if ( /^([a-z]):(.*)/i and $type eq -cygwin )
{
my $dir = $1;
my $path = $2;
$ret = "/cygdrive/\L$dir\E$path";
$ret =~ s,\\,/,g;
}
$ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Determine OS and convert path to correct Win32 environment.
# Cygwin perl or to Win32 Activestate perl
#
# INPUT PARAMETERS
#
#
#
# RETURN VALUES
#
#
#
# ****************************************************************************
sub PathConvertSmart ($)
{
my $id = "$LIB.PathConvertSmart";
local ($ARG) = @ARG;
if ( $CYGWIN_PERL )
{
# In win32, you could define environment variables as
# C:\something\like, but that's not understood under cygwin.
$ARG = PathConvert $ARG, -cygwin;
}
my $home = $ENV{HOME};
s,~,$home,;
s,\Q$HOME,$home,;
$ARG;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Return version string
#
# INPUT PARAMETERS
#
# none
#
# RETURN VALUES
#
# string
#
# ****************************************************************************
sub Version ()
{
"$VERSION";
}
sub VersionInfo ()
{
Version() . " $AUTHOR $LICENSE $URL"
}
sub VersionPrint ()
{
print( VersionInfo() . "\n");
exit 0;
}
# ************************************************************** &args *******
#
# DESCRIPTION
#
# Read and interpret command line arguments
#
# INPUT PARAMETERS
#
# none
#
# RETURN VALUES
#
# none
#
# ****************************************************************************
sub HandleCommandLineArgs ()
{
# ............................................... local variables ...
my $id = "$LIB.HandleCommandLineArgs";
# .......................................... command line options ...
use vars qw # declare global variables
(
$CFG_FILE_NEEDED
$CHECK_NEWEST
$CONTENT_REGEXP
$DIR_DATE
$EXTRACT
$FIREWALL
$LCD_CREATE
$MIRROR
$NO_EXTRACT
$NO_LCD
$NO_SAVE
$OUT_DIR
$OVERWRITE
$POSTFIX
$PREFIX
$PREFIX_DATE
$PREFIX_WWW
$PROXY
$PWGET_CFG
$SITE_REGEXP
$SKIP_VERSION
$SLEEP_SECONDS
$STDOUT
$TAG_REGEXP
$URL_REGEXP
@CFG_FILE
@TAG_LIST
$debug
$test
$verb
);
$CFG_FILE_NEEDED = 0;
$FIREWALL = "";
$OVERWRITE = 0;
# .................................................... read args ...
Getopt::Long::config( qw
(
no_ignore_case
no_ignore_case_always
));
my ( $version, $help, $helpHTML, $helpMan, $selfTest, $chdir );
GetOptions # Getopt::Long
(
"A|regexp-content=s" => \$CONTENT_REGEXP
, "chdir=s" => \$chdir
, "c|config:s" => \@CFG_FILE
, "C|create-paths" => \$LCD_CREATE
, "dry-run" => \$test
, "d|debug:i" => \$debug
, "D|prefix-date" => \$PREFIX_DATE
, "extract" => \$EXTRACT
, "firewall=s" => \$FIREWALL
, "help-html" => \$helpHTML
, "help-man" => \$helpMan
, "h|help" => \$help
, "mirror=s" => \$MIRROR
, "no-extract" => \$NO_EXTRACT
, "no-lcd" => \$NO_LCD
, "no-save" => \$NO_SAVE
, "n|new" => \$CHECK_NEWEST
, "output:s" => \$OUT_DIR
, "overwrite" => \$OVERWRITE
, "postfix:s" => \$POSTFIX
, "prefix:s" => \$PREFIX
, "proxy=s" => \$PROXY
, "r|regexp=s" => \$SITE_REGEXP
, "R|config-regexp=s" => \$URL_REGEXP
, "selftest" => \$selfTest
, "skip-version" => \$SKIP_VERSION
, "sleep:i" => \$SLEEP_SECONDS
, "stdout" => \$STDOUT
, "t|tag=s" => \@TAG_LIST
, "T|test" => \$test
, "v|verbose:i" => \$verb
, "V|version" => \$version
, "W|prefix-www" => \$PREFIX_WWW
);
if ( defined $debug )
{
$debug = 1 unless $debug;
}
if ( defined $verb )
{
$verb = 1 unless $verb;
}
$verb = 5 if $debug;
# set verbose to 1, if debug is on. Set to full verbose if
# debug is higher than 2.
$debug and $verb == 0 and $verb = 1;
$debug > 2 and $verb = 10;
$version and VersionPrint();
$helpHTML and Help( undef, -html );
$helpMan and Help( undef, -man );
$help and Help();
$selfTest and SelfTest();
$NO_LCD = 0 unless defined $NO_LCD;
$NO_SAVE = 0 unless defined $NO_SAVE;
$NO_EXTRACT = 0 unless defined $NO_EXTRACT;
if ( $chdir )
{
unless ( chdir $chdir )
{
die "$id: CHDIR [$chdir] fail. $ERRNO";
}
}
if ( defined $URL_REGEXP or @TAG_LIST )
{
$CFG_FILE_NEEDED = -yes;
}
if ( defined $URL_REGEXP and @TAG_LIST )
{
die "You can't use both --tag and --regexp options.";
}
if ( defined $PROXY )
{
$ARG = $PROXY;
if ( not m,^http://, )
{
$debug and print "$id: Adding http:// to proxy $PROXY\n";
$ARG = "http://" . $ARG;
}
if ( not m,/$, )
{
$debug and print "$id: Adding trailing / to proxy $PROXY\n";
$ARG .= "/";
}
$PROXY = $ARG;
$debug and print "$id: PROXY $PROXY\n";
}
if ( @TAG_LIST )
{
# -s -t -n tag --> whoopos....
if ( grep /^-/ , @TAG_LIST )
{
die "$id: --tag option argument was an option: @TAG_LIST\n";
}
$TAG_REGEXP = '\btag(\d+):\s*(\S+)';
}
if (
not @CFG_FILE
and ( @TAG_LIST or $URL_REGEXP )
)
{
unless ( defined $PWGET_CFG )
{
die "$id: No environment variable PWGET_CFG defined. "
, "Need --config FILE where to search."
;
}
my $file = PathConvertSmart $PWGET_CFG;
unless ( -r $file )
{
die "$id: PWGET_CFG is not readable [$file]"
. " for regexp match 'URL_REGEXP'";
}
$verb and print "$id: Using default config file $file\n";
push @CFG_FILE, $file;
}
$debug and @CFG_FILE and print "$id: Config file [@CFG_FILE]\n";
# Do not remove this comment, it is for Emacs font-lock-mode
# to handle hairy perl fontification right.
#font-lock * s/*/
}
# ****************************************************************************
#
# DESCRIPTION
#
# Convert month names to numbers in string.
#
# INPUT PARAMETERS
#
# $str Like "2005-February.txt"
#
# RETURN VALUES
#
# $ Like "2005-02.txt" is no changes.
#
# ****************************************************************************
sub MonthToNumber ($)
{
local $ARG = shift;
my %hash =
(
'Jan(uary)?' => '01'
, 'Feb(ruary)?' => '02'
, 'Mar(ch)?' => '03'
, 'Apr(il)?' => '04'
, 'May' => '05'
, 'Jun(e)?' => '06'
, 'Jul(y)?' => '07'
, 'Aug(ust)?' => '08'
, 'Oct(ober)?' => '09'
, 'Sep(tember)?' => '10'
, 'Nov(ember)?' => '11'
, 'Dec(ember)?' => '12'
);
while ( my($re, $month) = each %hash )
{
s/$re/$month/i;
}
$ARG;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Find out the temporary directory
#
# INPUT PARAMETERS
#
# none
#
# RETURN VALUES
#
# $ temporary directory
#
# ****************************************************************************
sub TempDir ()
{
my $id = "$LIB.TempDir";
local $ARG;
if ( defined $TEMPDIR and -d $TEMPDIR)
{
$ARG = $TEMPDIR;
}
elsif ( defined $TEMP and -d $TEMP)
{
$ARG = $TEMP;
}
elsif ( -d "/tmp" )
{
$ARG = "/tmp";
}
elsif ( -d "c:/temp" )
{
$ARG = "c:/temp"
}
elsif ( -d "$HOME/temp" )
{
$verb and
print "$id: [WARNING] using HOME/tmp, make sure you have disk space";
$ARG = "$HOME/temp";
}
else
{
die "$id: Can't find temporary directory. Please set TEMPDIR."
}
if ( $ARG and not -d )
{
die "$id: Temporary directory found is invalid: [$ARG]";
}
s,[\\/]$,,; # Delete trailing slash
s,\\,/,g; # Unix slashes in this Perl code
$debug and print "$id: $ARG\n";
$ARG;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Remove duplicate entries from list. Empty values are removed too.
#
# INPUT PARAMETERS
#
# @list
#
# RETURN VALUES
#
# @list
#
# ****************************************************************************
sub ListRemoveDuplicates (@)
{
my $id = "$LIB.FilterDuplicates";
my @list = @ARG;
$debug and print "$id: [@list]\n";
if ( @list )
{
my %hash;
@hash{ @list }++;
@list = grep /\S/, keys %hash;
}
@list;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Return temporary process file
#
# INPUT PARAMETERS
#
# none
#
# RETURN VALUES
#
# $ temporary filename
#
# ****************************************************************************
sub TempFile ()
{
my $id = "$LIB.TempFile";
my $ret = TempDir() . basename($PROGRAM_NAME) . "-" . $PROCESS_ID;
$debug and print "$id: $ret\n";
$ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Write file to stdout
#
# INPUT PARAMETERS
#
# $file
#
# RETURN VALUES
#
# none
#
# ****************************************************************************
sub Stdout ( $ )
{
my $id = "$LIB.Stdout";
my($file) = @ARG;
local *FILE;
unless ( open FILE, "< $file" )
{
warn "$id: Can't STDOUT $file $ERRNO";
}
else
{
print <FILE>;
close FILE;
}
}
# ****************************************************************************
#
# DESCRIPTION
#
# Fix the filename to correct OS version ( win32 /Cygwin / DOS )
# This is needed when calling external programs that take file arguments.
#
# INPUT PARAMETERS
#
# $file
#
# RETURN VALUES
#
# $file Converted file
#
# ****************************************************************************
sub MakeOSfile ( $ )
{
my $id = "$LIB.MakeOSfile";
local($ARG) = @ARG;
if ( $WIN32 )
{
if ( defined $SHELL )
{
$debug and print "$id: SHELL = $SHELL\n";
if ( $SHELL =~ /sh/i ) # bash.exe
{
# This is Win32/Cygwin, which needs c:/ --> /cygdrive/c/
if ( /^(.):(.*)/ ) #font s/
{
$ARG = "/cygdrive/$1/$2";
s,\\,/,g;
s,//,/,g;
}
}
}
else
{
s,/,\\,g; # Win32 likes backslashes more
}
}
$debug and print "$id: $ARG\n";
$ARG;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Return ISO 8601 date YYYY-MM-DD
#
# INPUT PARAMETERS
#
# $format [optional] If "-version", return in format YYYY.MMDD
#
# RETURN VALUES
#
# $str Date string
#
# ****************************************************************************
sub DateYYYY_MM_DD (; $)
{
my $id = "$LIB.DateYYYY_MM_DD";
my ($format) = @ARG;
my (@time) = localtime(time);
my $YY = 1900 + $time[5];
my ($DD, $MM) = @time[3,4];
# my ($mm, $hh) = @time[1,2];
$debug > 3 and print "$id: @time\n";
# Month(MM) counts from zero
my $ret;
if ( defined $format and $format eq -version )
{
$ret = sprintf "%d.%02d%02d", $YY, $MM + 1, $DD;
}
else
{
$ret = sprintf "%d-%02d-%02d", $YY, $MM + 1, $DD;
}
$debug > 3 and print "$id: RET $ret\n";
$ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Print variables in hash
#
# INPUT PARAMETERS
#
# $name name of the hash
# %hash content of hash
#
# RETURN VALUES
#
# none
#
# ****************************************************************************
sub PrintHash ( $ % )
{
my $id = "$LIB.PrintHash";
my ($name, %hash ) = @ARG;
print "$id: hash [$name] contents\n";
for my $key ( sort keys %hash )
{
my $val = $hash{ $key };
printf "%-20s = %s\n", $key, $val;
}
}
# ****************************************************************************
#
# DESCRIPTION
#
# Remove duplicates.
#
# INPUT PARAMETERS
#
# @ List of values.
#
# RETURN VALUES
#
# @ List of values.
#
# ****************************************************************************
sub ListUnique ( @ )
{
my $id = "$LIB.ListUnique";
$debug > 2 and print "$id: INPUT\n", join("\n", @ARG), "\n";
my %hash;
local $ARG;
# does no longer work in latest Perl:
# @hash{ @ARG }++;
for (@ARG)
{
$hash{$ARG} = 1;
}
my @ret = sort keys %hash;
$debug > 1 and print "$id: RET\n", join("\n", @ret), "\n";
@ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Print download progress
#
# INPUT PARAMETERS
#
# $url Site from where to download
# $prefix String to print
# $index current count
# $total total
#
# RETURN VALUES
#
# string indicator message
#
# ****************************************************************************
{
my %staticDone;
sub DownloadProgress ($$ $$ $)
{
my $id = "$LIB.DownloadProgress";
my ( $site, $url, $prefix, $index, $total ) = @ARG;
if ( $verb )
{
if ( $total > 1 )
{
sprintf $prefix . " %3d%% (%2d/%d) "
, int ( $index * 100 / $total )
, $index
, $total
;
}
else
{
unless ( exists $staticDone{$site} )
{
$staticDone{$site} = 1;
$prefix;
}
}
}
}}
# ****************************************************************************
#
# DESCRIPTION
#
# Expand variable by substituting any Environment variables in it.
#
# INPUT PARAMETERS
#
# $string Path information, like $HOME/.example
# $str Original line; full string from configuration file.
#
# RETURN VALUES
#
# string Expanded path.
#
# ****************************************************************************
sub ExpandVars ($; $)
{
my $id = "$LIB.ExpandVars";
local $ARG = shift;
my $origline = shift;
$debug > 2 and print "$id: input $ARG [$origline]\n";
return $ARG unless /\$[a-z]/i; # nothing to do
my $orig = $ARG;
# We must substitute environment variables so that the
# longest are handled first. An example of the problem using
# variable: $FTP_DIR_THIS/here.
#
# FTP_DIR = one
# FTP_DIR_THIS = two
#
# --> one_THIS/here
my @keys = sort { length $b <=> length $a } keys %ENV;
my $value;
for my $key ( @keys )
{
next unless $key =~ /[a-z]/i; # ignore odd "__" env vars
$value = $ENV{$key};
if ( /$key/ and $value ne "" ) #font s/
{
$debug > 2 and print "$id $ARG substituting key $key => $value\n";
s/\$$key/$value/; #font s/;
}
}
# The env variables may contain leading slashes, get rid of them.
# Or there may be "doubles"; fix them.
#
# [$ENV = /dir/ ]
# $ENV/path --> /dir//path
#
s,//+,/, unless /(http|ftp):/i;
$debug > 2 and print "$id: after loop $orig ==> $ARG\n";
if ( /\$/ )
{
# PrintHash "ENV", %ENV;
die "$id: [ERROR]. Check environment. Expansion did not "
, "find variable(s): $orig\n";
}
# Convert to Unix paths
s,\\,/,g;
$ARG;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Evaluate perl code and return result.
#
# INPUT PARAMETERS
#
# $url text to put variable $url
# $text The text to be placed to variable $ARG
# $code Perl code to manipulate $ARG
# $flag non-empty: Do not return empty values, if the perl
# code didn]t set ARG at all, then return original TEXT
#
# RETURN VALUES
#
# $text
#
# ****************************************************************************
sub EvalCode ($ $ ; $)
{
my $id = "$LIB.EvalCode";
my ($url, $text, $code, $flag ) = @ARG;
my $ret = $text;
# Variable $url is seen to CODE if it wants to use it
$debug and print "$id: ARG $ARG EVAL $code\n";
return $ret unless $code;
# Wrap this inside private block, so that user defined
# $code can execute in safe environment. E.g he can
# define his own variables and they willnot affect the program
# afterwards.
{
local $ARG = $text;
eval $code;
if ( $EVAL_ERROR )
{
warn "$id: eval-fail ARG [$ARG] CODE [$code] $EVAL_ERROR";
$ARG = $text;
}
if ( not $ARG and $flag )
{
$debug and print "$id: ARG [$ARG] is empty [$code]\n";
$ARG = $text;
}
elsif ( $ARG )
{
$ret = $ARG;
}
}
$debug and print "$id: RET $ret\n";
$ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Check sourceforge special. The sourceoforge site does not publish
# direct download links. Check if this is one of those. And example:
#
# INPUT PARAMETERS
#
# string
#
# RETURN VALUES
#
# 0 No
# 1 Yes
#
# ****************************************************************************
sub IsSourceforgeDownload ($)
{
my $id = "$LIB.IsSourceforgeDownload";
local $ARG = shift;
m,(?:sourceforge|sf)\.net.*/download$,;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Check if HTML::Parse and HTML::FormatText libraries are available
#
# INPUT PARAMETERS
#
# none
#
# RETURN VALUES
#
# 0 Error
# 1 Ok, support present
#
# ****************************************************************************
sub IsLibHTML ()
{
my $id = "$LIB.IsLibHTML";
my $error = 0;
$EVAL_ERROR = '';
local *LoadLib = sub ($)
{
my $lib = shift;
eval "use $lib";
if ( $EVAL_ERROR )
{
warn "$id: $lib not available [$EVAL_ERROR]\n";
$error++;
}
};
LoadLib( "HTML::Parse");
LoadLib( "HTML::FormatText");
return 0 if $error;
1;
}
# ****************************************************************************
#
# DESCRIPTION
#
# convert html into ascii
#
# INPUT PARAMETERS
#
# @lines
#
# RETURN VALUES
#
# @txt
#
# ****************************************************************************
{
my $staticLibCheck = 0;
my $staticLibOk = 0;
sub Html2txt (@)
{
my $id = "$LIB.Html2txt";
my (@list) = @ARG;
my @ret = @list;
unless ( $staticLibCheck )
{
$staticLibOk = IsLibHTML();
$staticLibCheck = 1;
unless ( $staticLibOk )
{
warn "$id: No HTML to TEXT conversion available.";
}
}
# Library was not found, nothing to do
return unless $staticLibOk;
if ( not @list )
{
$verb and print "$id: Empty content";
}
elsif ( $staticLibCheck )
{
my $formatter = new HTML::FormatText
( leftmargin => 0, rightmargin => 76);
# my $parser = HTML::Parser->new();
# $parser->parse( join '', @list );
# $parser-eof();
# $HTML::Parse::WARN = 1;
my $html = parse_html( join '', @list );
$verb and print "$id: Making conversion\n";
@ret = ( $formatter->format($html) );
$html->delete(); # mandatory to free memory
}
@ret;
}}
# ****************************************************************************
#
# DESCRIPTION
#
# Return File content
#
# INPUT PARAMETERS
#
# $file
# $join [optional] If set, then the file is read as one big
# string. The value is the first argument in return array.
#
# RETURN VALUES
#
# (\@lines, $status)
#
# ****************************************************************************
sub FileRead ( $; $ )
{
my $id = "$LIB.FileRead";
my $file = shift;
my $join = shift;
my $status = 0;
my @ret;
local *FILE;
# Convert path to Unix format
$file =~ s,\\,/,g;
if ( $file =~ /^~/ )
{
# must expand ~/dir and ~user/dir contructs with shell
my $expanded = qx(echo $file);
$debug and print "$id: EXPANDED by shell: $file => $expanded\n";
$file = $expanded;
}
unless ( open FILE, "< $file" )
{
$status = $ERRNO;
warn "$id: FILE [$file] $ERRNO";
}
else
{
if ( $join )
{
$ret[0] = join '', <FILE>;
}
else
{
@ret = <FILE>;
}
close FILE;
}
$debug and print "$id: [$file] status [$status]\n";
\@ret, $status;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Write File content
#
# INPUT PARAMETERS
#
# $file
# @lines
#
# RETURN VALUES
#
# $status true, ERROR
#
# ****************************************************************************
sub FileWrite ( $ @)
{
my $id = "$LIB.FileWrite";
my ($file, @lines ) = @ARG;
my $status = 0;
local *FILE;
unless ( open FILE, "> $file" )
{
$status = $ERRNO;
warn "$id: FILE [$file] $ERRNO";
}
else
{
print FILE @lines;
close FILE;
}
$debug and print "$id: [$file] status [$status]\n";
$status;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Convert HTML file to text
#
# INPUT PARAMETERS
#
# $file
#
# RETURN VALUES
#
# $status
#
# ****************************************************************************
sub FileHtml2txt ($)
{
my $id = "$LIB.FileHtml2txt";
my $file = shift;
my( $lineArrRef, $status ) = FileRead $file;
if ( $status )
{
$debug and print "$id: Can't convert\n";
}
else
{
my @text = Html2txt @$lineArrRef;
$status = FileWrite $file, @text;
}
$debug and print "$id: [$file] status [$status]\n";
$status;
}
# ****************************************************************************
#
# DESCRIPTION
#
# analyze FILE to match to match content. If content is not found,
# delete file.
#
# INPUT PARAMETERS
#
# $file
# $regexp [optional] content match regexp.
#
# RETURN VALUES
#
# $status True if file is okay
#
# ****************************************************************************
sub FileContentAnalyze ($ $)
{
my $id = "$LIB.FileContentAnalyze";
my $file = shift;
my $re = shift;
return -noregexp unless $re;
my( $lineArrRef, $status ) = FileRead $file, '-join';
my $ret;
if ( $status )
{
$debug and print "$id: Can't Analyze $file\n";
}
else
{
local $ARG = $$lineArrRef[0];
my $match = $MATCH if /$re/;
if ( $match )
{
$ret = -matched;
}
else
{
unless ( $test )
{
$debug > 1 and
print "$id: [$re] content not found, deleting $file\n";
unless ( unlink $file )
{
$verb and print "$id: $ERRNO";
}
}
}
$debug and print "$id: [$file] status [$ret] RE [$re]=[$match]\n";
}
$ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Append slash to the end. Optionally remove
#
# INPUT PARAMETERS
#
# $path Add slash to path
# $flag [optional] Remove slash
#
# RETURN VALUES
#
# $path
#
# ****************************************************************************
sub Slash ($; $)
{
my $id = "$LIB.Slash";
local $ARG = shift;
my $remove = shift;
if ( $remove )
{
s,/$,,;
}
{
$ARG .= '/' unless m,/$,;
}
$ARG;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Split url to components. http://some.com/1/2page.html would be seen as
#
# http some.com 1/2 page.html
#
# INPUT PARAMETERS
#
# $url
#
# RETURN VALUES
#
# @list Component list
#
# ****************************************************************************
sub SplitUrl ($)
{
my $id = "$LIB.SplitUrl";
local $ARG = shift;
my($protocol, $site, $dir, $file ) = ("") x 4;
$protocol = lc $1 if m,^([a-z][a-z]+):/,i;
$site = lc $1 if m,://?([^/]+),i;
$dir = lc $1 if m,://?[^/]+(/.*/),i;
$file = lc $1 if m,^.*/(.*),i;
if ( $file and $file !~ /[.]/ )
{
$debug and print "$id: [WARNING] ambiguous [$ARG], dir or file?\n";
unless ( $dir )
{
$dir = $file;
$file = "";
}
}
$debug and print "$id:"
, " PROTOCOL <$protocol>"
, " SITE <$site>"
, " DIR <$dir>"
, " FILE <$file>"
, "\n"
;
$protocol, $site, $dir, $file;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Return basename from URL. This drops the possible
# filename from the end. The extra file is detected
# from the file extension, perriod(.)
#
# http://some.com/~foo ok
# http://some.com/foo ok treated as directory
# http://some.com/page.html nok
#
# INPUT PARAMETERS
#
# $base
#
# RETURN VALUES
#
# $string The url will not contain trailing slash
#
# ****************************************************************************
sub BaseUrl ($)
{
my $id = "$LIB.BaseUrl";
local $ARG = shift;
if ( m,/~[^/]+$, )
{
$debug and print "$id: ~foo\n";
# ok
}
elsif ( m,^(.*/)([^/]+)$, )
{
my ( $base, $rest ) = ( $1, $2 );
$debug and print "$id: [$base] [$rest]\n";
$ARG = $base if $rest =~ /[.]/;
}
s,/$,,;
$ARG;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Return basename of the archive.
#
# file.tar,gz => file
# file-1.2.tar.gz => file-1.2
# file-1_2.tar.gz => file-1.2
#
# INPUT PARAMETERS
#
# $file
#
# RETURN VALUES
#
# $string
#
# ****************************************************************************
sub BaseArchive ($)
{
my $id = "$LIB.BaseArchive";
local $ARG = shift;
if ( /^(.*-\d+[-_.]\d+[-_.\d]*)/ )
{
# delete last trailing - or . or _
( $ARG = $1 ) =~ s/[-_.]$//;
}
elsif ( /^(.*)\.[a-z].*$/ )
{
# some-archive.bz2 --> some-archive
# some-archive.zip --> some-archive
$ARG = $1;
}
s/_/-/g;
$ARG;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Try to make sense of relative paths when Base is known.
# This function is very simplistic.
#
# INPUT PARAMETERS
#
# $base
# $relative
#
# RETURN VALUES
#
# $path
#
# ****************************************************************************
sub RelativePath ($ $)
{
my $id = "$LIB.RelativePath";
my $base = shift;
local $ARG = shift;
$base = Slash $base;
my $ret = $base;
unless ( $ARG )
{
$debug and print "$id: second arg is empty [$base]";
}
else
{
if ( m,^/.*, ) # /root/somewhere/file.txt
{
my ($proto, $site, $dir, $file) = SplitUrl $base;
$ret = "$proto://$site$ARG";
}
elsif ( m,^\./(.*), ) # ./somewhere/file.txt
{
$ret = $base . $1;
}
elsif ( m,^[^/\\#?=], ) # this/path/file.txt
{
$ret = $base . $ARG;
}
else
{
chomp; # make warn display line number, remove \n
warn "$id: [ERROR] Can't resolve relative $base + $ARG";
}
}
$debug and print "$id: BASE $base ARG $ARG RET $ret\n";
$ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Return decompress command for file.
#
# INPUT PARAMETERS
#
# $file
# $type -list return listng command
# -extract return unpack command
#
# RETURN VALUES
#
# lines as listed in file
#
# ****************************************************************************
sub FileDeCompressedCmd ($; $)
{
my $id = "$LIB.FileDeCompressedCmd";
local $ARG = shift;
my $type = shift;
$type = '-extract' unless defined $type;
my $opt = "--decompress --stdout";
my $cmd;
my $decompress;
if ( /\.rar$/ )
{
die "$id: $ARG Can't handle. No free rar uncompress program exists.";
}
if ( $type eq -extract )
{
if ( /\.(tar|tgz)(.*)/ )
{
my $ext = $2;
/\.(gz|tgz)$/i and $decompress = "gzip $opt";
/\.(bzip|bz2)$/i and $decompress = "bzip2 $opt";
/\.(xz)$/i and $decompress = "xz $opt";
$cmd = "$decompress $ARG | tar xvf -";
if ( length $ext and not $decompress)
{
warn "[WARN] Unknown compress extension: $ext ($ARG)";
$cmd = "";
}
}
elsif ( /\.gz$/ )
{
$cmd = "gzip -f -d $ARG";
}
elsif ( /\.(bz2|bzip)$/ )
{
$cmd = "bzip2 -f -d $ARG";
}
elsif ( /\.zip$/ )
{
$cmd = $decompress = "unzip -o $ARG";
}
}
else
{
if ( /tar/ )
{
SWITCH:
{
/\.(gz|tgz)$/i and $decompress = "gzip $opt", last;
/\.(bzip|bz2)$/i and $decompress = "bzip2 $opt", last;
/\.(xz)$/i and $decompress = "xz $opt", last;
}
if ( defined $decompress )
{
$cmd = "$decompress $ARG | tar tvf -";
}
else
{
$cmd = "tar tvf $ARG";
}
}
elsif ( /\.zip$/ )
{
$cmd = "unzip -l $ARG";
}
}
$debug and print "$id:\n\tARG = $ARG\n"
, "\tTYPE $type\n"
, "\tRET [$cmd]\n"
;
$cmd;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Return decompress file listing
#
# INPUT PARAMETERS
#
# $file
#
# RETURN VALUES
#
# \@files Files from the archive
# $error If "-noarchive" , then file is not an archive.
#
# ****************************************************************************
sub FileDeCompressedListing ( $ )
{
my $id = "$LIB.FileDeCompressedListing";
my $file = shift;
$debug and print "$id: BEGIN $file CWD ", cwd(), "\n";
my ($cmd, @result, $status);
if ( -f $file )
{
$cmd = FileDeCompressedCmd $file, -list ;
$debug and print "$id: running [$cmd] CWD ", cwd(), "\n";
$ERRNO = 0;
@result = qx($cmd) if $cmd;
if ( $ERRNO )
{
$verb and print "$id: [WARN] $ERRNO\n";
$status = -externalerror;
}
$debug and print "$id: CMD [$cmd] => \n[@result]\n";
}
else
{
$verb and warn "[ERROR] file not found ", cwd(), "$file";
$status = -file-not-found;
}
my @ret = ();
local $ARG;
if ( $status )
{
# Nothing to do here. Error
}
elsif ( $file =~ /tar/ )
{
# Get last elements in the line
#
# .. 0 2000-11-18 16:18 semantic-1.3.2
# .. 23688 2000-11-18 16:18 semantic-1.3.2/semantic-bnf.el
# .. 50396 2000-11-18 16:18 semantic-1.3.2/semantic.el
# .. 36176 2000-11-18 16:18 semantic-1.3.2/semantic-util.el
#
# This comment fixes Emacs fontification: m///
for ( @result )
{
my $file = (reverse split)[0];
chomp $file;
push @ret, (reverse split)[0];
}
$debug and print "$id: TAR [@result]\n";
}
elsif ( $file =~ /zip/ )
{
# Length Date Time Name
# ------ ---- ---- ----
# 4971 03-22-00 21:14 1/gnus-ml.el
# 0 10-03-99 01:33 tmp/1/tpu/
# ------ -------
# 25036 8 files
for ( @result )
{
my @split = reverse split;
chomp $split[0];
next unless /^\s+\d+\s/;
push @ret, $split[0] if @split == 4;
}
$debug and print "$id: ZIP [@result]\n";
}
else
{
$debug and print "$id: -noarchive $file\n";
$status = -noarchive;
}
$debug and print "$id: RET $file status [$status] [@ret]\n";
\@ret, $status;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Return the subdirectory where the files are in compressed archive.
# There may not be any directory or there may be several directories
# that do not share one ROOT directory.
#
# INPUT PARAMETERS
#
# $file
#
# RETURN VALUES
#
# $dir The topmost COMMON root directory. If not all files
# have common root, return nothing.
#
# $status -noarchive The file was not archive.
# \@file reference to file list
#
# ****************************************************************************
sub FileDeCompressedRootDir ( $ )
{
my $id = "$LIB.FileDeCompressedRootDir";
my $file = shift;
$debug and print "$id: BEGIN $file CWD ", cwd(), "\n";
my ( $fileArrRef, $status ) = FileDeCompressedListing $file;
# If there is directory it must be in front of every file
local $ARG;
my %seen;
my @nodir;
for ( @$fileArrRef )
{
if ( m,^([^/]+)/, )
{
# Do not accept "./" directory
$seen{ $1 } = 1 if $1 ne ".";
}
else
{
push @nodir, $ARG;
}
}
my @roots = keys %seen;
my $ret;
if ( @roots )
{
my $root = $roots[0];
if ( @roots == 1 and @nodir == 1 and $root eq $nodir[0])
{
# Special case. The directory itself is always "alone" entry
# drwxrwxrwx foo/users 0 2006-07-22 14:18 package-0.5.6
$ret = $root
}
elsif ( @roots == 1 and @nodir == 0 )
{
$ret = $root;
}
}
$debug and print "$id: RET [$ret] status [$status]; "
. "roots [@roots] no-dirs [@nodir]\n";
$ret, $status, $fileArrRef ;
}
# ****************************************************************************
#
# DESCRIPTION
#
# If archive does not have root directory, return the
# filename which is bet used for archive root dir.
#
# package.tar.gz --> package-YYYY.MMDD
#
# INPUT PARAMETERS
#
# $file
#
# RETURN VALUES
#
# $root Returned, If archive does not have natural ROOT
#
# ****************************************************************************
sub FileRootDirNeedeed ( $ )
{
my $id = "$LIB.FileRootDirNeedeed";
my $file = shift;
$debug and print "$id: BEGIN $file CWD ", cwd(), "\n";
my ($root, $status, $fileArrRef) = FileDeCompressedRootDir $file;
local $ARG;
if ( $status eq -noarchive ) # Single file.txt.gz, not package
{
$debug and print "$id: -noarchive $file\n";
$ARG = '';
}
elsif ( @$fileArrRef == 0 )
{
$debug and print "$id: EMPTY $file\n";
$ARG = '';
}
elsif ( @$fileArrRef == 1 )
{
$debug and print "$id: SINGLE FILE $file\n";
$ARG = '';
}
elsif ( $root eq '' )
{
$ARG = basename $file;
my $base = BaseArchive $ARG;
# If there is no numbers left, assume that we got barebones
# and not name like "package-1.11". Add date postfix
unless ( $base =~ /\d/ )
{
$base .= "-" . DateYYYY_MM_DD(-version);
}
$ARG = $base;
}
$debug and print "$id: $file --> need dir [$ARG]\n";
$ARG;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Create root directory if it is necessary in order to unpack
# the file. If the archive does not contain ROOT, contruct one
# from the filename and current date.
#
# If directory was created or it already exists, return full path
#
# INPUT PARAMETERS
#
# $file
# $path Under this directory the creation
# $opt -rm Delete previous unpack directory
#
# RETURN VALUES
#
# $path If directory was created
#
# ****************************************************************************
sub FileRootDirCreate ( $ $; $ )
{
my $id = "$LIB.FileRootDirCreate";
my ($file, $path, $opt) = @ARG;
not defined $opt and $opt = '';
$debug and print "$id: BEGIN $file PATH $path\n";
local $ARG = FileRootDirNeedeed $file;
my $ret = '';
if ( $ARG )
{
$ARG = "$path/$ARG";
if ( -e )
{
$verb and print "$id: Unpack dir already exists $ARG\n";
if ( $opt =~ /rm/i )
{
$verb and print "$id: deleting old unpack dir\n";
unless ( rmtree($ARG, $debug) )
{
warn "$id: Could not rmtree() $ARG\n";
}
}
}
unless ( -e )
{
unless ( $test )
{
mkpath( $ARG ) or die "$id: mkdir() fail $ARG $ERRNO";
$verb and warn "$id: [WARNING] archive $file"
, " has no root-N.NN directory."
, " Report this to archive maintainer. CREATED $ARG"
, "\n"
;
}
}
$ret = $ARG;
}
$debug and print "$id:\n\tFILE $file\n"
, "\tPATH $path\n"
, "\tRET --> created [$ret]\n"
;
$ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Unpack list of files recursively (If package contains more
# archives)
#
# INPUT PARAMETERS
#
# \@array List of files
# \%hash Unpack command hash table: REGEXP => COMMAND, where
# %s is substituted for filename
# $check "-noroot", will not check the archive content
# $opt "-rm", will remove any existing unpack dir
#
# RETURN VALUES
#
# none
#
# ****************************************************************************
sub Unpack($ $; $ $); # must be introduced due to recursion
sub Unpack ($ $; $ $)
{
my $id = "$LIB.Unpack";
my ( $filesArray, $cmdHash, $check, $opt ) = @ARG;
$check = 1 if not defined $check;
$check = 0 if $check eq -noroot;
$opt = '' if not defined $opt;
local $ARG;
my $origCwd = cwd();
$debug and print "$id: entry cwd = $origCwd OPT [$opt]\n";
my @array = sort { length $b <=> length $a } keys %$cmdHash;
$debug and print "$id: SORTED decode array @array\n";
for ( @$filesArray )
{
$debug and warn "$id: unpacking $ARG\n";
if ( -d )
{
$verb and print "$id: $ARG is directory, skipped.\n";
next;
}
elsif ( not -f )
{
$verb and print "$id: $ARG is not a file (not exist), skipped.\n";
next;
}
# The filename may look lik test/tar.gz
my $gocwd = dirname($ARG) || '.' ;
chdir $gocwd or die "$id: [for] Can't chdir [$gocwd] $ERRNO";
# Check only archives that do not contains some kind
# of numbering for missing ROOT directories.
my $cwd = cwd();
my $chdir = 0;
my $file = basename $ARG;
# ............................................ check ...
# Must contain root directory in archive
# We check every archive. Regexp \d would have
# skipped names looking like package-1.34.tar.gz
if ( $check ) # and not /-[\d]/ ) #font s/
{
$debug and print "##\n";
my $newDir = FileRootDirCreate basename($ARG), $cwd, $opt;
$debug and print "## $newDir\n";
if ( $newDir )
{
$debug and print "$id: cd newdir $newDir\n";
unless ( chdir $newDir )
{
print "$id: [ERROR] chdir $newDir $ERRNO\n";
next;
}
$file = "../$file";
$chdir = 1;
}
}
# ........................................... unpack ...
$debug and print ">>\n";
my $cmd = FileDeCompressedCmd $file;
$debug and print "$id: unpacking CWD ", cwd(), " [$cmd]\n";
my @response = qx($cmd) unless $test;
print "@response\n" if $verb;
# ........................................ recursive ...
for my $entry ( @response ) # Make this recursive
{
local $ARG = $entry;
chomp;
if ( /\.(bz2|gz|z|zip)$/i ) # s/
{
$debug and print "$id: >> RESCURSIVE [$ARG]\n";
Unpack( [ $ARG ], $cmdHash, -noroot, $opt );
}
}
chdir $cwd if $chdir; # Go back to original dir
}
$debug and print "EXIT chdir $origCwd\n";
chdir $origCwd or die "$id: [exit] Can't chdir [$origCwd] $ERRNO";
}
# ****************************************************************************
#
# DESCRIPTION
#
# Read directory content
#
# INPUT PARAMETERS
#
# $path
#
# RETURN VALUES
#
# @ list of files
#
# ****************************************************************************
sub DirContent ($)
{
my $id = "$LIB.DirContent";
my ( $path ) = @ARG;
$debug and warn "$id: $path\n";
local *DIR;
unless ( opendir DIR, $path )
{
print "$id: can't read $path $ERRNO\n";
next;
}
my @tmp = readdir DIR;
closedir DIR;
$debug > 1 and warn "$id: @tmp";
@tmp;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Scan until valid tag line shows up. Return line if it is under the
# TAG
#
# INPUT PARAMETERS
#
# $line line content
# $tag Tag name to look for
# $reset If set, do nothing but reset state variables.
# You should call with this if you start a new round.
#
# RETURN VALUES
#
# ($LINE, $stop) The $LINE is non-empty if it belongs to the TAG.
# The $stop flag is non-zero if TAG has ended.
#
# ****************************************************************************
{
my
(
$staticTagLevel
, $staticTagName
, $staticTagFound
);
sub TagHandle ($$ ; $)
{
my $id = "$LIB.TagHandle";
local $ARG = shift;
my ( $tag , $reset) = @ARG;
my $info = ( $verb > 1 || $debug > 1 ) ? 1 : 0;
# ........................................................ reset ...
if ( $reset )
{
$debug > 1 and print "$id: RESET\n";
$staticTagLevel = $staticTagName = $staticTagFound = "";
return $ARG;
}
# ...................................................... tag ...
my $stop;
# The line may have multiple tags and the $1 is number, second
# is the tag name. However we can't put them in that order
# to the hash, because the number is "key". Use reverse here
#
# tag2: A tag2: B
#
# 2 => A
# 2 => B
# |
# The key, only last would be in hash
my %choices = reverse /$TAG_REGEXP/go;
if( $info and keys %choices > 0 )
{
print "$id: TAG CHOICES: ", join( ' ', %choices), "\n"
}
unless ( $staticTagFound )
{
while ( my($tagN, $tagNbr) = each %choices )
{
if ( $info and $tagNbr )
{
print "$id: [$tagNbr] '$tagN' eq '$tag'\n";
}
if ( $tagNbr and $tagN eq $tag )
{
$staticTagLevel = $tagNbr;
$staticTagName = $tagN;
$staticTagFound = 1;
$debug > 0 and warn "$id: TAG FOUND [$staticTagName] $ARG\n"
}
}
$ARG = "" unless $staticTagFound; # Read until TAG
}
else
{
# We're reading lines after the tag was found.
# Terminate on next found tag name
while ( my($tagN, $tagNbr) = each %choices )
{
if ( $tagNbr and $tagNbr <= $staticTagLevel )
{
$info and print "$id: End at [$staticTagName] $ARG\n";
$stop = 1;
}
}
}
$debug > 1 and print "$id: RETURN [$ARG] stop [$stop]\n";
$ARG, $stop;
}}
# ****************************************************************************
#
# DESCRIPTION
#
# Handle Local directory change and die if can't checnge to
# directory.
#
# INPUT PARAMETERS
#
# $dir Where to chdir
# $make [optional] Flag, if allowed to create directory
#
# RETURN VALUES
#
# none
#
# ****************************************************************************
sub DirectiveLcd (%)
{
my $id = "$LIB.DirectiveLcd";
my %arg = @ARG;
my $dir = $arg{-dir} or die "$id: No DIR argument";
my $mkdir = $arg{-mkdir} || 0;
$verb > 2 and PrintHash "$id: input", %arg;
my $lcd = ExpandVars $dir;
$verb > 2 and print "$id: LCD original $lcd\n";
$lcd = PathConvertSmart $lcd;
$verb > 2 and print "$id: LCD converted $lcd\n";
my $isDir = -d $lcd ? 1 : 0 ;
unless ( $isDir )
{
my $TEST = "[test mode] " if $test;
$verb and print STDERR "$id: ${TEST}Creating directory $lcd\n";
not $mkdir and die "$id: [$dir] => lcd [$lcd] is not a directory";
unless ($test)
{
mkpath($lcd, $verb) or die "$id: mkpath $lcd failed $ERRNO";
}
}
if ( -d $lcd )
{
$debug > 2 and print "$id: chdir $lcd\n";
chdir $lcd or die "$id: chdir $lcd $ERRNO";
}
}
# ****************************************************************************
#
# DESCRIPTION
#
# Examine list of files and return the newest file that match FILE
# the idea is that we divide the filename into 3 parts
#
# PREFIX VERSION REST
#
# So that for example filename
#
# emacs-20.3.5.1-lisp.tar.gz
#
# is exploded to parts
#
# emacs -20.3.5.1- lisp.tar.gz
#
# After this, the VERSION part is examined and all the numbers from
# it are read and converted to zero filled keys, so that sorting
# between versions is possible:
#
# (20 3 5 1) --> "0020.0003.0005.0001"
#
# A hash table for each file is build according to this version key
#
# VERSION-KEY => FILE-NAME
#
# When we finally sort the has by key, we get the latest version number
# and the associated file.
#
# INPUT PARAMETERS
#
# $file File to use as base.
# \@files List of files to compare.
#
# RETURN VALUES
#
# $file File that is newest, based on version number.
#
# ****************************************************************************
sub LatestVersion ( $ $ )
{
my $id = "$LIB.LatestVersion";
my ( $file , $array ) = @ARG;
$debug > 1 and
print "$id: INPUT file [$file] ARRAY =>\n",
join ("\n", @$array),
"\n";
! $file and die "$id: <file:> argument missing";
# ................................................ write regexps ...
# NN.NN YYYY-MM-DD
# 1.2beta23
# 1.1-beta1
# 1.1a
# file_150b6_en.zip
#
# Difficult names are like4this-1.1.gz
#
# Prevent 1.1.tar.gz --> "1.1.t" with negative lookahead
my $ext = '(?!(?i)tar|gz|bzip|bz2|tgz|tbz2|zip|rar|z$)';
my $add = '(?:[-_]?(?:alpha|beta|rc)\d*|' . $ext . '[a-z])';
my $regexp = '^(?<prefix>.*?[-_]|\D*\d+\D+|\D+)' # $1
. '(?<version>'
. '[-_.\db]*\d' # $2
. $add
. '?)'
. '(?<rest>\S+)' # $3
;
$debug and
print "$id: file [$file] REGEXP /$regexp/ "
, "ARRAY OF FILENAMES TO EXAMINE:\n"
, join("\n", @$array)
, "\n"
;
@$array = ListUnique @$array;
# .......................................................... sub ...
my ( %hash, %hash2, $max );
local *VersionPush = sub ( $ $ )
{
my $id = "$id.VersionPush";
local $ARG = shift; # filename
my $verStr = shift;
my $key = "";
my @v = /(\d+)/g ; # explode all digits
if ( $verStr =~ /(?<ascii>[a-z])$/ ) # "1.1a"
{
# 1.1a => 1.1.97 use a's ascii code
# 1.1 => 1.1.0
push @v, ord $+{ascii}; # get character ASCII code
}
$debug > 1 and print "$id: [Version pure] \@v = @v\n";
# Sometimes the version number is really unorthodox
# like foo-1.020.el.gz When compared with 1.2.3, that
# would say:
#
# 1. 20
# 1. 2
#
# And giving false priority to "020". The leading
# zeroes must be treated as it the number was:
#
# 1. 0 20
LOCALIZE:
{ # Use inner block to localize @ver
my @ver;
for my $nbr ( @v )
{
if ( $nbr =~ /^(?<zeros>0+)(?<digits>\d+)/ )
{
push @ver, (0) x length $+{zeros};
push @ver, $+{digits};
}
else
{
push @ver, $nbr;
}
}
@v = @ver;
}
$debug > 1 and print "$id: [Version fixed] \@v = @v\n";
# Record how many separate digits was found.
$max = @v if @v > $max;
# fill until 8 version digit elements in array
push @v, 0 while @v < 8 ;
for my $version ( @v )
{
# 1.0 --> 0001.0000.0000.0000.0000.0000
$key .= sprintf "%015d.", $version;
}
$hash { $key } = $ARG;
$hash2 { $v[0] } = $ARG;
};
# .......................................................... sub ...
local *DebugHash = sub ()
{
if ( $debug > 1 )
{
while ( my($key, $val) = each %hash )
{
printf "$id: HASH1 $key => $val\n";
}
while ( my($key, $val) = each %hash2 )
{
printf "$id: HASH2 $key => $val\n";
}
}
};
# .......................................................... sub ...
local *ParseVersion = sub ($ $ $)
{
my $id = "$id.ParseVersion";
my ($pfx, $post, $ver) = @ARG;
my $ret;
# If date is used as version number:
#
# wemi-199802080907.tar.gz
# wemi-19980804.tar.gz
# wemi-199901260856.tar.gz
# wemi-199901262204.tar.gz
#
# then sort directly by the %hash2, which only contains direct
# NUMBER key without prefixed zeroes. For multiple numbers we
# sort according to %hash
my @try;
if ( $max == 1 )
{
@try = sort { $b <=> $a } keys %hash2;
%hash = %hash2;
}
else
{
@try = sort { $b cmp $a } keys %hash;
}
if ( $debug )
{
print "$id: Sorted choices: $ver $pfx.*$post\n";
for my $arg ( @try )
{
print "\t$hash{$arg}\n";
}
}
# If SINGLE answer, then use that. Or if we grepped versioned
# files, take the sorted one from the beginning
if ( @try )
{
$ret = $hash{ $try[0] };
}
$ret;
};
# ........................................... search version [1] ...
# APACHE project uses underscores in filenames:
# apache_1_3_9_win32.exe
local $ARG = $file;
my $ret = $file;
if ( /$regexp/o )
{
my $pfx = $+{prefix};
my $version = $+{version};
my $rest = $+{rest};
$pfx =~ s/[-_]$//; # package-name- => package-name
$pfx =~ s,([][{}+.?*]),\\$1,g; # In RE, quote special characters.
# Examine 150b6, 1.50, 1_15
my $ver = '[-_]([-._\db]+ ' . $add . '?)';
# NOTE: Sourceforge is on format <URL>/file.tar.gz/download
my $post = qr/\Q$rest\E/i . '($|[&?][a-z]|/download)';
$debug and print "$id: ORIGINAL ARG '$ARG'"
. " INITIAL PFX: [$pfx]"
. " MIDDLE: [$version] regexp /$ver/"
. " POSTFIX: [$rest] regexp /$post/"
. "\n"
;
# .................................................. arrange ...
# If there are version numbers, then sort all according
# to version.
for ( @$array )
{
$debug > 1 and print "$id: FOR [$ARG]\n";
# If the filename is like file.txt-1.1, then there is
# no point to use $post, because it would reject files
# because it requires extension "1$", but file.txt-1.2
# has extension "2$"
unless ( ( ( /\.[a-z]+[^-.]+$/ and /$pfx.*$post/)
or /$pfx/ )
and /$regexp/o
)
{
$debug > 1 and print "$id: REJECTED\t\t$ARG\n";
next;
}
unless ( /$pfx.*$post/ )
{
$debug > 1 and print "$id: REJECTED no ",
"prefix '$pfx'",
"postfix '$post'",
"\t$ARG\n"
;
next;
}
unless ( /$regexp/o )
{
$debug > 1
and print "$id: REJECTED, no regexp match\t$ARG\n";
}
my ($BEG, $vver, $END) = ($+{prefix}, $+{version}, $+{rest});
$debug > 1 and print "$id: MATCH: [$BEG] [$vver] [$END]\n";
VersionPush( $ARG, $vver);
}
DebugHash();
$ret = ParseVersion( $pfx, $post, $ver );
}
elsif ( /(?<REST>.*)-[\d.]+$/ )
{
$debug and print "$id: plan B, non-standard version-N.NN\n";
my $pfx = $+{REST};
my $ver = '(?<version>-[\d.]+)$';
my $post = "";
$debug > 1
and print "$id: (B) PFX: [$pfx] POSTFIX: [$post] [$ARG]\n";
for ( @$array )
{
unless ( /(?<beg>$pfx)-$ver/ )
{
$debug and print "$id: REJECTED\t\t$ARG\n";
next;
}
my ($BEG, $vver) = ($+{beg}, $+{version});
$debug > 1 and print "$id: MATCH: [$BEG] [$vver]\n";
VersionPush( $ARG, $vver);
}
DebugHash();
$ret = ParseVersion( $pfx, $post, $ver );
}
elsif ( /^(?<beg>\D+)[\d.]+[a-h]+[\d.]+(?<REST>.*)/ ) # WinCvs11b14.zip
{
$debug > 1 and print "$id: plan C, non-standard version-N.NN\n";
my $pfx = $+{beg};
my $ver = '(?<version>[\d.]+[a-h]+[\d.]+)';
my $post = $+{REST};
$debug > 1
and print "$id: (C) PFX: [$pfx] POSTFIX: [$post] [$ARG]\n";
for ( @$array )
{
unless ( /(?<beg>$pfx)$ver$post/ )
{
$debug > 1 and print "$id: REJECTED\t\t$ARG\n";
next;
}
my ($BEG, $vver) = ($+{beg}, $+{version});
$debug > 1 and print "$id: MATCH: [$BEG] [$vver] $ARG\n";
VersionPush( $ARG, $vver);
}
DebugHash();
$ret = ParseVersion( $pfx, $post, $ver );
}
elsif ( /^(?<prefix>\D+)[\d.]+/ ) # WinCvs136.zip
{
$debug > 1 and print "$id: plan D, non-standard version-N.N\n";
my $pfx = $+{prefix};
my $ver = '(?<version>[\d.]+)';
my $post = "";
$debug > 1 and print "$id: (D) PFX: [$pfx] POSTFIX: [$post]\n";
for ( @$array )
{
unless ( /(?<beg>$pfx)$ver/ )
{
$debug > 1 and print "$id: REJECTED\t\t$ARG\n";
next;
}
my ($BEG, $vver) = ($+{beg}, $+{version});
$debug > 1 and print "$id: MATCH: [$BEG] [$vver] $ARG\n";
VersionPush( $ARG, $vver);
}
DebugHash();
$ret = ParseVersion( $pfx, $post, $ver );
}
else
{
if ( $verb )
{
print << "EOF";
$id: Unknown version format in filename. Cannot parse according to skeleton [$ARG]
The most usual reason for this error is, that you have supplied
<pregexp:> and <new:>. Please examine your URL and try removing <new:>
Another reason may be that the filename is in format that is not standard
NAME-VERSION.EXTENSION, like package-1.34.4.tar.gz. In that case please
contact the developer of the package and let him know about the de facto
packaging format.
EOF
}
}
$debug and warn "$id: RETURN model was:$file --> [$ret]\n";
if ( $ret eq '' )
{
die << "EOF";
$id: Internal error, Run with --debug on to pinpoint the details.
Cannot find anything suitable for download. This may be due to
non-matching file <regexp:> or <regexp:> is too limiting or
<no-regexp:> filtered everything. If you used <new:>, it may
be possible that the heuristics could not determine what were the
links to examine. In that case, please let the program know what
kind of file it should search by providing template directive
<file:NAME-VERSION.tar.gz>.
Check also that the <new:> file extension looks the same as what
<pregexp:> found from the page.
[$CURRENT_TAG_LINE]
EOF
}
$ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Check if file is compressed once. This means that
# if file is decompressed it would possibly overwrite original file.
#
# file.txt.gz => is simple compressed
# package.tar.gz => is NOT simple compressed.
#
# INPUT PARAMETERS
#
# $ FILENAME
#
# RETURN VALUES
#
# $ True value if file is simple compressed
#
# ****************************************************************************
sub FileSimpleCompressed ( $ )
{
my $id = "FileSimpleCompressed";
my ($file) = @ARG;
my @list =
(
'.gz'
, '.bz2'
, '.Z'
, '.z'
, '.xz'
);
my $ret;
unless ( $file =~ /\.(tar|zip|rar)/ ) # must not be multi-archive format
{
my @suffixlist = map { my $f = $ARG; $f =~ s/\./\\./g; $f } @list;
my ($name,$path,$suffix) = fileparse( $file , @suffixlist);
$ret = $suffix;
}
$debug and print "$id: [$file] RET [$ret]\n";
$ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Check If file exists. Checks also the name without compression
# extension.
#
# INPUT PARAMETERS
#
# $ FILENAME
# $ [optional] UNPACK. If set, then the file will be
# $ decompressed and the original file under it should be checked.
# That is, if FILENAME is "file.txt.gz", check also "file.txt"
#
# RETURN VALUES
#
# @ List of files that exist on disk
#
# ****************************************************************************
sub FileExists ( % )
{
my $id = "FileExists";
my %arg = @ARG;
local $ARG = $arg{file}; # REQUIRED
my $unpack = $arg{unpack} || 0;
my $file = $ARG;
if ( /^(.+)\?use_mirror/ )
{
$debug and print "$id: FILE [$file] sf fixed => [$1]\n";
$file = $1;
}
elsif ( /\?.*=(.+[a-zA-Z])/ )
{
# download.php?file=this.tar.gz
$debug and print "$id: FILE [$file] fixed => [$1]\n";
$file = $1;
}
my @list = qw
(
.bz2
.gz
.lzma
.lzop
.rar
.rzip
.z
.Z
.zip
.xz
);
my @suffixlist = map { my $f = $ARG; $f =~ s/\./\\./g; $f } @list;
my ($name, $path, $suffix) = fileparse( $file , @suffixlist);
my %ret; # hash filters out duplicates
if ( $suffix )
{
my @try = ( $suffix );
push @try, '' if $unpack; # '' => file itself
for my $try ( @try )
{
$debug and print "$id: trying: [$path] + [$name] + [$try]\n";
my $file = $path . $name . $try;
$ret{$file} = 1 if -e $file;
}
}
elsif ( not $unpack and -e $file )
{
$ret{$file} = 1;
}
$debug and print "$id: ", cwd(), " [$file] RET [", keys %ret, "]\n";
keys %ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Check Invalid filename characters
#
# INPUT PARAMETERS
#
# $ filename
#
# RETURN VALUES
#
# $ URL file
#
# ****************************************************************************
sub FileNameFix ( $ )
{
my $id = "FileNameFix";
local ($ARG) = @ARG;
$debug and print "$id: INPUT [$ARG]\n";
if ( /\?.+=(.+\.(?:gz|bz2|tar|zip|rar|pak|lhz|iso))/ )
{
# download.php?id=file.tar.gz
$debug and
print "$id: A: chararcter [?] - fixing [$ARG] => [$1]\n";
$ARG = $1;
}
elsif ( m,^(?:sourceforge|sf)\.net.*/([^/?]+), )
{
$ARG = $1;
}
elsif ( /^(.*viewcvs.*)\?/ )
{
# http://cvs.someorg/cgi-bin/viewcvs.cgi/~checkout~/file?rev=HEAD
# => file "file?rev=HEAD"
$debug and
print "$id: B: chararcter [?] - fixing [$ARG] => [$1]\n";
$ARG = $1;
}
$debug and print "$id: RET [$ARG]\n";
$ARG;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Make latest filename with possible version numbers
#
# INPUT PARAMETERS
#
# $file Template, how the file looks like
# @ Array of possible verion numbers
#
# RETURN VALUES
#
# @ Versioned files
#
# ****************************************************************************
sub MakeLatestFiles ( $ @ )
{
my $id = "$LIB.MakeLatestFiles";
local $ARG = shift;
my @versions = @ARG;
my @ret;
if ( /^(.*?)-([\d.]+[\d])(.*)/ )
{
my ( $pre, $middle, $rest ) = ( $1, $2, $3 );
$debug and print "$id: Exploded [$pre] [$middle] [$rest]\n";
for my $ver ( @versions )
{
my $file = $pre . "-" . $ver . $rest;
push @ret, $file;
}
}
else
{
$verb and print "$id: Can't parse version from FILE $ARG\n";
# Suppose that all the files in @versions are versioned
#
# file.txt-1.2 file.txt-1.3 and the model file was
# file.txt
@ret = ( LatestVersion $versions[0], \@versions );
}
$debug and print "$id: FILE $ARG RET => [@ret]\n";
@ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Selct file or files from LIST. GETFILE and REGEXP are
# mutually exclusive
#
# INPUT PARAMETERS
#
# regexp Select files according to regexp.
# regexpno Files not to include after REGEXP has matched.
#
# getfile If newest file is wanted, here is sample.
# If this variable is empty; then no newest file is searched.
#
# list @, candidate file list
#
# RETURN VALUES
#
# @ List of selected files
#
# ****************************************************************************
sub FileListFilter ( % )
{
my $id = "$LIB.FileListFilter";
my %arg = @ARG;
my $regexp = $arg{regexp};
my $regexpNo = $arg{regexpno};
my $getFile = $arg{getfile};
my $list = $arg{list};
my @list = @$list;
$debug and print "$id: INPUT REGEXP [$regexp]"
, " REGEXPNO [$regexpNo]"
, " GETFILE [$getFile]"
, " LIST => "
, join("\n", @list)
, "\n"
;
# ......................................................... args ...
local *Filter = sub ( $ @)
{
my ($regexpNo, @list) = @ARG;
my @new = grep ! /$regexpNo/, @list;
$debug and print "$id: [$regexpNo] FILTERED "
, join(' ', grep /$regexpNo/, @list), "\n"
;
if ( $verb and not @new )
{
print "$id: [WARNING] regexpNo [$regexpNo] rejected everything\n";
}
@list = @new;
};
@list = Filter( $regexpNo, @list ) if $regexpNo and @list;
if ( $regexp )
{
@list = sort grep /$regexp/, @list;
}
$debug and print "$id: after regexp\n",
join("\n", @list),
"\n";
if ( $getFile )
{
my $name = basename $getFile;
$debug and print "$id: getfile basename [$name]\n";
my $file = LatestVersion $name, \@list;
if ( $verb )
{
print "$id: ... Getting latest version: $file DIR: ", cwd(), "\n";
}
@list = ( $file );
}
@list = Filter( $regexpNo, @list ) if $regexpNo and @list;
$debug and print "$id: RET\n",
join("\n", @list),
"\n";
@list;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Get file via FTP
#
# INPUT PARAMETERS
#
# $site Site to connect
# $path dir in SITE
#
# $getFile File to get
# $saveFile File to save on local disk
# $regexp
# $regexpNo
#
# $firewall
#
# $new Flag, Should only the newest file retrieved?
# $stdout Print to stdout
#
# RETURN VALUES
#
# () RETURN LIST whose elements are:
#
# $stat Error reason or "" => ok
# @ list of retrieved files
#
# ****************************************************************************
sub UrlFtp ( % )
{
my $id = "$LIB.UrlFtp";
my %arg = @ARG;
# ......................................................... args ...
# check mandatory
not exists $arg{site} and die "$id: SITE missing";
not exists $arg{path} and die "$id: PATH missing";
not exists $arg{getFile} and die "$id: FILE missing";
not exists $arg{saveFile} and die "$id: SAVE missing";
# Defaults. Note: login 'ftp' is still not known to every
# FTP server.
not $arg{login} and $arg{login} = 'anonymous';
not $arg{pass} and $arg{pass} = 'nobody@example.com';
# Read values
my $url = $arg{url};
my $site = $arg{site};
my $path = $arg{path};
my $getFile = $arg{getFile};
my $saveFile = $arg{saveFile};
my $regexp = $arg{regexp};
my $regexpNo = $arg{regexpNo};
my $firewall = $arg{firewall};
my $login = $arg{login};
my $pass = $arg{pass};
my $new = $arg{new} || 0;
my $stdout = $arg{stdout} || 0 ;
my $conversion = $arg{conversion} || '';
my $rename = $arg{rename} || '';
my $origLine = $arg{origLine} || '';
my $unpack = $arg{unpack} || '';
my $overwrite = $arg{overwrite} || '';
# ............................................ private functions ...
my @files;
local *PUSH = sub ($)
{
local ( $ARG ) = @ARG;
if ( $stdout )
{
Stdout $ARG;
}
else
{
unless ( m,[/\\], )
{
$ARG = cwd() . "/" . $ARG ;
}
push @files, $ARG if not $stdout;
}
};
# ............................................ private variables ...
my $timeout = 120;
my $singleTransfer;
if ( (not defined $regexp or $regexp eq '') and ! $new )
{
$singleTransfer = 1;
}
local $ARG;
$stdout and $saveFile = TempFile();
if ( $debug )
{
print "$id:\n"
, "\tsingleTransfer: $singleTransfer\n"
, "\tSITE : $site\n"
, "\tPATH : $path\n"
, "\tLOGIN : $login PASS $pass\n"
, "\tgetFile : $getFile\n"
, "\tsaveFile : $saveFile\n"
, "\trename : $rename\n"
, "\tconversion : $conversion\n"
, "\tregexp : $regexp\n"
, "\tregexp-no : $regexpNo\n"
, "\tfirewall : $firewall\n"
, "\tnew : $new\n"
, "\tcwd : ", cwd(), "\n"
, "\tOVERWRITE : $overwrite\n"
, "\tSKIP_VERSION: $SKIP_VERSION\n"
, "\tstdout : $stdout\n"
;
}
$verb and print
"$id: Connecting to ftp://$site$getFile --> $saveFile\n";
$debug and print "$id:\n"
, "REGEXP: $regexp \n"
, "LOGIN : $login\n"
, "PASSWD: $pass\n"
, "SITE : $site\n"
, "PATH : $path\n"
;
# One file would be transferred, but it already exists and
# we are not allowed to overwrite --> do nothing.
if ( $singleTransfer and -e $saveFile
and not $overwrite
and not $stdout
)
{
$verb and print "$id: [ignored, exists] $saveFile\n";
return;
}
# .................................................. make object ...
my $ftp;
if ( $firewall ne '' )
{
$ftp = Net::FTP->new
(
$site,
(
Firewall => $firewall,
Timeout => $timeout
)
);
}
else
{
$ftp = Net::FTP->new
(
$site, ( Timeout => $timeout )
);
}
unless ( defined $ftp )
{
print "$id: Cannot make route to $site $ERRNO\n";
return;
}
# ........................................................ login ...
$debug and print "$id: Login to $site ..\n";
unless ( $ftp->login($login, $pass) )
{
print "$id: Login failed $login, $pass\n";
goto QUIT;
}
$ftp->binary();
my $cd = $path;
$cd = dirname $path unless $path =~ m,/$, ;
if ( $cd ne '' )
{
unless ( $ftp->cwd($cd) )
{
print "$id: Remote cd $cd failed [$path]\n";
goto QUIT;
}
}
# .......................................................... get ...
my $stat;
$ftp->binary();
$ftp->hash( $verb ? "on" : undef ); # m"
if ( $singleTransfer )
{
$verb and print "$id: Getting file... [$getFile]\n"; # m:
$rename and do{$saveFile = EvalCode $url, $saveFile, $rename};
unless ( $ftp->get($getFile, $saveFile) )
{
warn "$id: ** [ERROR] SingleFile [$getFile] $ERRNO\n"
, "\tMaybe the URL on the line is invalid? [$origLine]"
;
}
else
{
PUSH($saveFile);
}
}
else
{
my (@list, $i);
$verb and print "$id: Getting list of files $site ...\n";
$i = 0;
$debug and warn "$id: Running ftp dir ls()\n";
@list = $ftp->ls();
@list = FileListFilter regexp => $regexp,
regexpno => $regexpNo,
getfile => $getFile,
list => [@list];
$debug and warn "$id: List length ", scalar @list, " --> @list\n";
if ( $verb and not @list )
{
print "$id: No files to download."
, " Run with debug to investigate the problem.\n"
;
}
for ( @list )
{
$i++;
my $progress = DownloadProgress $site . $cd, $ARG, "$id: ..."
, $i, scalar @list;
print $progress if $progress;
my $saveFile = $ARG;
$saveFile = TempFile() if $stdout;
$rename and do{$saveFile = EvalCode $url, $saveFile, $rename};
$verb and print " $ARG [$saveFile]\n";
unless ( $stdout )
{
my $onDisk;
my $simpleZ = FileSimpleCompressed $saveFile;
if ( $simpleZ )
{
($onDisk) = FileExists file => $saveFile,
unpack => -forceUnpackCheck;
if ( $verb > 1 )
{
print "$id: Uncompressed file; use --overwrite\n";
}
}
else
{
($onDisk) = FileExists file => $saveFile,
unpack => $unpack;
}
$debug and print "$id: On disk? [$ARG] [save $saveFile] .. "
, -e $onDisk ? "[yes]" : "[no]"
, cwd()
, "\n"
;
if ( $onDisk )
{
if ( $SKIP_VERSION and /-\d[\d.]*\D+/ )
{
$verb and print "$id: [skip version/already on disk] "
, " $ARG => $onDisk\n";
next;
}
elsif ( not $overwrite )
{
$verb and print "$id: [no overwrite/already on disk] "
, " $ARG => $onDisk\n";
next;
}
}
}
unless ( $stat = $ftp->get($saveFile) )
{
print "$id: ... ** error $ARG $ERRNO $stat\n";
}
else
{
PUSH($saveFile);
}
sleep $SLEEP_SECONDS if $SLEEP_SECONDS;
}
}
QUIT:
{
$ftp->quit() if defined $ftp;
}
($stat, @files);
}
# ****************************************************************************
#
# DESCRIPTION
#
# Download URL using external program wget(1)
#
# INPUT PARAMETERS
#
# $ URL
#
# RETURN VALUES
#
# $ content in string if success
#
# ****************************************************************************
sub UrlHttGetWget ( $ )
{
my $id = "$LIB.UrlHttpGetWget";
my $url = shift;
$debug and print "$id: GET $url ...\n";
my $ret = qx(wget --quiet --output-document=- '$url' 2> /dev/null);
return $ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Download URL by using Perl only.
#
# INPUT PARAMETERS
#
# $ URL
#
# RETURN VALUES
#
# $ Content in string if success
# $ Headers
#
# ****************************************************************************
sub UrlHttGetPerl ( $ )
{
my $id = "$LIB.UrlHttpGetPerl";
my $url = shift;
# http://www.useragentstring.com/pages/Firefox/
my $agent = "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.3) Gecko/20090913 Firefox/3.5.3";
my $ua = new LWP::UserAgent;
$debug and print "$id: GET $url ...\n";
my $request = new HTTP::Request( 'GET' => $url );
$request->user_agent($agent);
my $obj = $ua->request($request);
my $stat = $obj->is_success;
unless ( $stat )
{
print " ** error: $url ", $obj->message, "\n";
return;
}
my $content = $obj->content();
my $head = $obj->headers_as_string();
$debug > 4 and print "$id: RET head [$head]\n";
$debug > 4 and print "$id: RET ARG [$content]\n";
$debug and print "$id: RET success [$stat]\n";
$content, $head;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Download URL by using Perl only.
#
# INPUT PARAMETERS
#
# $ URL
#
# RETURN VALUES
#
# $ Content as string if success
# $ [optional] Header (not always available)
#
# ****************************************************************************
sub UrlHttGet ( $ )
{
my $id = "$LIB.UrlHttpGet";
my $url = shift;
$debug and print "$id: INPUT: $url\n";
# Sourceforge is tricky, it automatically tries to start download
# and pure Perl method doesn't do it. We need to get page content
# only, not to start the file download.
#
# FIXME: if there is a way to do this with LWP::UserAgent, please
# let me know and this wget(1) dependency is gladly removed.
if ( m,(?:sourceforge|sf)\.net.*/download, )
{
UrlHttGetWget $url;
}
else
{
UrlHttGetPerl $url;
}
}
# ****************************************************************************
#
# DESCRIPTION
#
# Try to find the latest version number from the page.
# Normally indicated by "The latest version of XXX is N.N.N"
#
# INPUT PARAMETERS
#
# $ String, the Url page
# $ [optional] regexp, what words to lok for
#
# RETURN VALUES
#
# % ver => string, List of versions and text matches
#
# ****************************************************************************
sub UrlHttPageParse ( $ ; $ )
{
my $id = "$LIB.UrlHttpPageParse";
local $ARG = shift;
my $regexp = shift;
my %hash;
if ( defined $regexp and $regexp ne '' )
{
while ( /$regexp/g )
{
$hash{$2} = $1 if $1 and $2;
}
unless ( scalar keys %hash )
{
print "$id: [ERROR] version regexp [$regexp] "
, " didn't find versions. "
, "Please check or define correct <vregexp:>\n";
}
}
elsif ( 0 and /(latest.*?version.*?\b([\d][\d.]+[\d]).*)/ )
{
$debug and print "$id: Using DEFAULT page regexp => [$MATCH]\n";
$hash{$2} = $1;
}
$debug and print "$id: RET regexp = [$regexp] HASH = ["
, join ( ' => ', %hash)
, "]\n"
;
%hash;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Parse all HREFs in the page and return the locations. If there is
# <BASE> tag, it is always obeyed and not filtered
#
# INPUT PARAMETERS
#
# content The html page
# regexp [optional] Return only HREFs matching regexp.
# unique [optional] Filter out duplicates.
#
# RETURN VALUES
#
# @urls
#
# ****************************************************************************
sub UrlHttpParseHref ( % )
{
my $id = "$LIB.UrlHttpParseHref";
my %arg = @ARG;
local $ARG = $arg{content}; # REQUIRED
my $regexp = $arg{regexp} || '';
my $unique = $arg{unique} || 0;
$debug > 3 and print "$id: INPUT ARG [$ARG]\n";
$debug and print "$id: INPUT regexp [$regexp]\n";
$debug and print "$id: INPUT unique [$unique]\n";
# Some HTML pages do not use double quotes
#
# <A HREF=http://example.com>
#
# The strict way is to use double quotes
#
# <A HREF="http://example.com">
#
# URLs do not necessarily stop after HREF
#
# <a href="dir/file.txt" target="_top">
my (@ret, $base);
if ( /<\s*BASE\s+href\s*=[\s\"]*([^\">]+)/i )
{
$base = $1;
$base =~ s,/$,,; # Remove trailing slash.
$debug and print "$id: BASE $base\n";
}
while ( /HREF\s*=[\s\"']*([^\"'>]+)/ig )
{
my $file = $1;
$debug and print "$id: FILE $file\n";
if ( $base and $file eq $base )
{
$debug and print "$id: FILTERED BY BASE $base\n";
next;
}
if ( $base and $file !~ m,//, )
{
$file = "$base/$file";
}
if ( $regexp ne '' and $file !~ /$regexp/ )
{
$debug and print "$id: FILTERED BY REGEXP $file\n";
next;
}
if ( $file =~ /^#/i )
{
$debug and print "$id: FILTERED [#] $file\n";
next;
}
# code.google.com: detail?archive.tar.bz2&can=2&q=
if ( $file =~ /;q=$/i )
{
$debug and print "$id: FILTERED [google] $file\n";
next;
}
# http://www.emacswiki.org/emacs?action=admin;id=twit.el
if ( $file =~ /emacswiki.*;.*=/ )
{
$debug and print "$id: FILTERED [emacswiki] $file\n";
next;
}
if ( $file =~ m,^\?|/$|mailto, )
{
$debug and print "$id: FILTERED OTHER [mailto] $file\n";
next;
}
if ( $file =~ m,mirror_picker, ) # Sourceforge
{
$debug and print "$id: FILTERED OTHER [mirror_picker] $file\n";
next;
}
push @ret, $file;
}
@ret = ListUnique @ret if $unique;
$debug and print "$id: RET REGEXP = [$regexp] "
, " LIST =>\n"
, join("\n", @ret), "\n";
@ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# If you connect to http page, that shows directory, this
# Function tries to parse the HTML and extract the filenames
#
# INPUT PARAMETERS
#
# $ String, The Url page
# $ [optional] boolean, if non-zero, filter out
# non-interesting files like directories.
#
# RETURN VALUES
#
# @ List of files
#
# ****************************************************************************
sub UrlHttpDirParse ( $ ; $ )
{
my $id = "$LIB.UrlHttpDirParse";
local $ARG = shift;
my $filter = shift;
$debug and print "$id: $filter\n";
my @files;
if ( /Server:\s+apache/i )
{
# Date: Wed, 16 Feb 2000 16:26:08 GMT
# Server: Apache/1.3.11 (Win32)
# Connection: close
# Content-Type: text/html m:
#
# <IMG SRC="/icons/folder.gif" ALT="[DIR]"> <A HREF="/">
# <IMG SRC="/icons/image2.gif" ALT="[IMG]"> <A HREF="apache_pb.gif">
# <IMG SRC="/icons/text.gif" ALT="[TXT]"> <A HREF="index.html.ca">
# Anything special to know? No?
}
# Filter out directories and non interesting files
#
# ?N=D ?M=A
# manual/
@files = UrlHttpParseHref content => $ARG;
@files;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Parse list of mirrors from sourceforge's download page.
#
# INPUT PARAMETERS
#
# $ HTML
#
# RETURN VALUES
#
# % Hash of hash. Mirrors and their locations
# MIRROR_NAME => { location, continent }
#
# ****************************************************************************
sub UrlSfMirrorParse ($)
{
my $id = "$LIB.UrlSfMirrorParse";
local($ARG) = @ARG;
my %hash;
$debug > 3 and print "$id: INPUT $ARG\n";
while ( m,<td>\s*([a-z].*?)</td> \s+
<td>(.*?)</td> \s+
.*?use_mirror=([a-z]+)
,gsmix
)
{
my( $location, $continent, $mirror) = ($1, $2, $3);
$debug > 2 and print "$id: location [$location] "
, "continent [$continent] "
, "mirror $mirror\n"
;
$hash{$mirror} = { location => $location, continent => $continent };
}
%hash;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Determine Sourceforge project ID based on project name.
#
# INPUT PARAMETERS
#
# $ project name
#
# RETURN VALUES
#
# $ URL
#
# ****************************************************************************
sub SourceforgeProjectId ($)
{
my $id = "$LIB.SourceforgeProjectId";
my($name) = @ARG;
$debug and print "$id: INPUT name [$name]\n";
my $url = "http://sourceforge.net/projects/$name";
local ($ARG) = UrlHttGet $url;
# href="/project/showfiles.php?group_id=88346#downloads">Jump to downloads for FOO</a></li>
my $ret;
if ( m,href\s*=.*group_id=(?<id>\d+),i )
{
$ret = $+{id};
}
$debug and print "$id: RET name [$name] id [$ret]\n";
$ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Parse Sourceforge project name from URL
#
# INPUT PARAMETERS
#
# $ URL
#
# RETURN VALUES
#
# $ Project name
#
# ****************************************************************************
sub SourceforgeProjectName ($)
{
my $id = "$LIB.SourceforgeProjectName";
local($ARG) = @ARG;
$debug and print "$id: INPUT $ARG\n";
my $name;
# http://sourceforge.net/projects/emacs-jabber
# http://prdownloads.sourceforge.net/emacs-jabber/emacs-jabber-0.6.1.tar.gz
if ( m,(?:sourceforge|sf)\.net/project/([^/]+),
or
m,downloads\.(?:sourceforge|sf)\.net/([^/]+),
)
{
$name = $1;
}
elsif (m, http://(?:www\.)?(?:sourceforge|sf).net/([^/]+), )
{
$name = $1;
}
$debug and print "$id: RET [$name]\n";
$name;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Parse downloads from Sourceforge page
#
# INPUT PARAMETERS
#
# $ URL
#
# RETURN VALUES
#
# % hash: "string" => URL
#
# ****************************************************************************
sub SopurceforgeParseDownloadPage ($)
{
my $id = "$LIB.UrlSfManipulate";
local($ARG) = @ARG;
# <td colspan="6"><a href="showfiles.php?group_id=88346&package_id=92339&release_id=482983" onclick="report_expand('pkg0_1rel0'); void(0); return false;" class="tup" id="pkg0_1rel0_0">0.7.1</a> <span class="notes"><a href="shownotes.php?release_id=482983&group_id=88346" title="View notes">Notes</a></span> <small>(2007-01-31 22:46) </small></td>
# <td ><a id="showfiles_download_file_pkg0_1rel0_1" class="sfx_qalogger_element sfx_qalogger_clickable" href="http://downloads.sourceforge.net/emacs-jabber/emacs-jabber-0.7.1.tar.gz?modtime=1170287319&big_mirror=0" onClick="window.location='/project/downloading.php?group_id=88346&use_mirror=heanet&filename=emacs-jabber-0.7.1.tar.gz&'+Math.floor(Math.random()*100000000); return false;">emacs-jabber-0.7.1.tar.gz</a>
# Emacs font-lock no-op comment "'
}
# ****************************************************************************
#
# DESCRIPTION
#
# Manipulate http://prdownloads.sourceforge.net address
#
# INPUT PARAMETERS
#
# $ URL
#
# RETURN VALUES
#
# $ URL
#
# ****************************************************************************
sub UrlManipulateSfOld ($)
{
my $id = "$LIB.UrlManipulateSfOld";
my ($url) = @ARG;
$debug and print "$id: INPUT [$url]\n";
my $project = SourceforgeProjectName $url;
unless ( $project )
{
die "$id: [FATAL] Cannot parse project name from $url\n";
}
my $gid = SourceforgeProjectId $project;
unless ( $gid )
{
die "$id: [FATAL] Cannot get group ID for [$project] $url\n";
}
# Download URL
my $base = "http://sourceforge.net";
my $durl = "$base/project/platformdownload.php?group_id=$gid";
local ($ARG) = UrlHttGet $durl;
unless ( $ARG )
{
die "$id: [FATAL] Cannot get SF page [$durl]";
}
# <td class="download" style="text-align: center;"><a href="/project/showfiles.php?group_id=88346&package_id=92339&release_id=482983">Download</a></td>
if ( m,class\s*=\s*.download.\s.*?href\s*=\s*.([^\"\'<>]+),ism )
{
$durl = $base . $1;
}
else
{
die "$id: [FATAL] Cannot parse SF page [$durl]";
}
local ($ARG) = UrlHttGet $durl;
unless ( $ARG )
{
die "$id: [FAIL] Cannot read SF page [$durl]";
}
# <td><a href="/project/showfiles.php?group_id=88346&package_id=92339&release_id=482983">0.7.1</a></td>
# /project/showfiles.php?group_id=88346&package_id=92339&release_id=482983
if ( m,\shref\s*=\s*[\"\']\s*
(
/project/
showfiles.php\?
group_id=$gid[^;]+;
package_id=\d+[^;]+;
release_id=(\d+)
)
,isx
)
{
$durl = $base . $1;
}
else
{
die "$id: [FATAL] Cannot parse release_id (for final page) [$durl]";
}
$durl =~ s/&/&/g;
$debug and print "$id: RET [$durl]\n";
$durl;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Manipulate address like:
# http://sourceforge.net/projects/clonezilla/files/clonezilla_live_testing/clonezilla-live-1.2.2-30.iso/download
#
# INPUT PARAMETERS
#
# $ URL
#
# RETURN VALUES
#
# $ URL
#
# ****************************************************************************
sub UrlManipulateSf ($ ; $)
{
my $id = "$LIB.UrlManipulateSf";
my ($url, $mirror ) = @ARG;
$debug and print "$id: INPUT url [$url]\n";
local ($ARG) = UrlHttGet $url;
my $ret;
if ( m,a \s+ href \s* = \"([^\"\']+) .* direct \s+ link \s* </a>,x )
{
$ret = $1;
$debug > 1 and print "$id: SF parsed direct link: $ret\n";
if ( $mirror and $ret =~ m,^(.*use_mirror=)(.*), )
{
$ret = $1 . $mirror;
$debug > 1 and print "$id: SF use mirror: $mirror\n";
}
}
else
{
die "$id: [FATAL] Cannot parse direct download from page $url";
}
$debug and print "$id: RET $ret\n";
return $ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Manipulate URLs; redirect if necessary
#
# INPUT PARAMETERS
#
# $ URL
#
# RETURN VALUES
#
# $ URL
#
# ****************************************************************************
sub UrlManipulateMain ($ ; $ )
{
my $id = "$LIB.UrlManipulateMain";
my ($url, $mirror ) = @ARG;
$debug and print "$id: INPUT $url MIRROR $mirror\n";
# http://downloads.sourceforge.net/project/clonezilla/clonezilla_live_testing/clonezilla-live-1.2.2-30.iso?use_mirror=sunet
if ( $url =~ m,prdownloads\.(?:sourceforge|sf)\.net/, )
{
$url = UrlManipulateSfOld $url;
}
if ( $url =~ m,(?:sourceforge|sf)\.net.*/download, )
{
$url = UrlManipulateSf $url, $mirror;
}
$debug and print "$id: RET $url\n";
$url;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Check if file is already on disk and do not overwrite if that is
# not allowed. Check also if there is skip version option active.
#
# GLOBAL VARIABLES
#
# $ARG
# $SKIP_VERSION
#
# INPUT PARAMETERS
#
# $file
# $unpack
#
# RETURN VALUES
#
# True if it's ok to download file.
# -skipversion
# -noovrt
#
# ****************************************************************************
sub UrlHttpFileCheck ( % )
{
my $id = "$LIB.UrlHttpFileCheck";
my %arg = @ARG;
my $saveFile = $arg{savefile};
my $unpack = $arg{unpack};
my $overwrite = $arg{overwrite};
my $ret;
my $onDisk;
my $simpleZ = FileSimpleCompressed $saveFile;
if ( $simpleZ )
{
($onDisk) = FileExists file => $saveFile,
unpack => -forceUnpackCheck;
if ( $verb > 1 )
{
print "$id: Uncompressed file found (use --overwrite)\n";
}
}
else
{
($onDisk) = FileExists file => $saveFile,
unpack => $unpack;
}
$debug and print "$id: file on disk? .. "
, -e($saveFile) ? "[yes]" : "[no]"
, "\n"
;
if ( $onDisk )
{
# If the filename contains version number
# AND skipping is on, then ignore download
if ( $SKIP_VERSION and /-\d[\d.]*\D+/ )
{
$verb and print "$id: [already on disk]"
, " $ARG => $onDisk\n";
$ret = -skipversion;
}
elsif ( not $overwrite )
{
$verb and print "$id: [no overwrite/already on disk]"
, " $ARG => $onDisk\n";
$ret = -noovrt;
}
}
$debug and print "$id: RET [$ret]\n";
$ret;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Search download ULRs from page.
#
# INPUT PARAMETERS
#
# %
#
# RETURN VALUES
#
# @list List of URLs found
#
# ****************************************************************************
sub UrlHttpSearchPage ( % )
{
my $id = "$LIB.UrlHttpSearchPage";
my %arg = @ARG;
my $url = $arg{url} || die "$id: missing arg URL";
my $regexpNo = $arg{regexpno};
my $baseUrl = $arg{baseurl};
my $thisPageRegexp = $arg{pageregexp};
if ( $debug )
{
print "$id: INPUT\n"
, "\turl : $url\n"
, "\tregexpNo : $regexpNo\n"
, "\tbaseUrl : $baseUrl\n"
, "\tthisPageRegexp: $thisPageRegexp\n"
;
}
my ($content, $head) = UrlHttGet $url;
my @list;
if ( $content )
{
@list = UrlHttpParseHref content => $content,
regexp => $thisPageRegexp;
if ( $regexpNo )
{
$debug > 2 and print "$id: filter before [$regexpNo] [@list]\n";
@list = grep ! /$regexpNo/, @list;
$debug > 2 and print "$id: filter after [@list]\n";
}
# Filter out FRAGMENTs that are not part of the file names:
#
# http://localhost/index.html#section1
local $ARG;
for ( @list )
{
if ( /#.*/ )
{
$debug and print "$id: filtering out FRAGMENT-SPEC $ARG\n";
s/#.*//;
}
}
$debug and print "$id: -find regexpNo [$regexpNo] @list\n";
}
$debug and print "$id: RET [@list]\n";
@list;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Search Newest file form page
#
# INPUT PARAMETERS
#
# %
#
# RETURN VALUES
#
# @list List of URLs found
#
# ****************************************************************************
sub UrlHttpSearchNewest ( % )
{
my $id = "$LIB.UrlHttpSearchNewest";
my %arg = @ARG;
my $ua = $arg{useragent} || die "No UA object";
my $getPage = $arg{page};
my $thisPage = $arg{flag};
my $file = $arg{file};
my $getFile = $arg{getfile};
my $baseUrl = $arg{baseurl};
my $versionRegexp = $arg{versionRE};
my $thisPageRegexp = $arg{pageRE};
my $regexp = $arg{RE};
my $regexpNo = $arg{REno};
my @list;
$debug > 1 and PrintHash "$id: INPUT", %arg;
$debug and print "$id: Getting list of files $getPage\n";
if ( $getPage =~ /\.(gz|bz2|lzma|zip|tar|jar|iso)$/ )
{
die "[ERROR] The URL must not contain filename: $getPage";
}
my ($content, $head) = UrlHttGet $getPage or return;
if ( $thisPage )
{
$debug and print "$id: THISPAGE START $file\n";
$getFile = $file;
my %hash = UrlHttPageParse $content, $versionRegexp;
my @keys = keys %hash;
my @urls = UrlHttpParseHref content => $content,
regexp => $thisPageRegexp,
unique => 'unique'
;
if ( @urls == 1 ) # only one match
{
@list = @urls;
goto EXIT;
}
my @files;
# The filename may contain the version information,
# UNLESS this is page search condition.
if ( $getFile !~ /\d/ )
{
# Nope, this is "download.html" search with
# possible "--Regexp SEARCH" option.
$getFile = $urls[0];
$file = $getFile;
$debug and print "$id: thispage changed file [$file]";
}
$debug and print "$id: THISPAGE file [$file] "
, "getFile [$getFile] "
, "urls [@urls] "
, "version urls [@keys]\n";
if ( @keys )
{
$debug and print "$id: THISPAGE if KEYS file [$file]\n";
@files = MakeLatestFiles $file, keys %hash ;
if ( @files == 1 )
{
@list = ( RelativePath dirname($urls[0]), $files[0] );
# for my $path ( @urls )
# {
# push @list, RelativePath
# ( dirname($path), $files[0] );
# }
#
}
else
{
$debug and print "$id: THISPAGE Latest files > 1\n";
@list = ( LatestVersion $file, [@urls, @files] ) ;
# @list > 1 and $file = '';
}
}
else
{
# Try old fashioned. The filename may contain the
# version information,
$debug and
print "$id: EXAMINE latest URL model [$file] list [@urls]\n";
@list = ( LatestVersion $file, \@urls ) if @urls ;
# $file = '';
}
$debug and print "$id: FILES [@files] URLS [@urls]\n";
unless ( @urls == 1 )
{
$verb > 2 and
warn "$id: Can't parse precise latest version location [@urls] ";
@list = @urls;
}
}
else
{
$debug and print "$id: NOT THISPAGE else statement\n";
@list = UrlHttpDirParse $head . $content, "clean";
}
EXIT:
@list = FileListFilter regexp => $regexp,
regexpno => $regexpNo,
getfile => $getFile,
list => [@list];
$debug and print "$id: RET", join("\n", @list), "\n";
@list;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Download files
#
# INPUT PARAMETERS
#
# %
#
# RETURN VALUES
#
# @list List of URLs found
#
# ****************************************************************************
sub UrlHttpDownload ( % )
{
my $id = "$LIB.UrlHttpDownload";
my %arg = @ARG ;
my $ua = $arg{useragent};
my $list = $arg{list};
my $file = $arg{file};
my $stdout = $arg{stdout};
my $find = $arg{find};
my $saveopt = $arg{saveopt};
my $baseUrl = $arg{baseurl};
my $unpack = $arg{unpack};
my $rename = $arg{rename};
my $overwrite = $arg{overwrite};
my $mirror = $arg{mirror};
my $contentRegexp = $arg{contentre} || '';
my $errUrlHashRef = $arg{errhash};
my $errExplanationHashRef = $arg{errtext};
if ( $debug )
{
print <<EOF;
$id:
list = @$list
file = $arg{file}
stdout = $arg{stdout}
find = $arg{find}
saveopt = $arg{saveopt}
baseUrl = $arg{baseurl}
unpack = $arg{unpack}
rename = $arg{rename}
overwrite = $arg{overwrite}
contentRegexp = $arg{contentre}
EOF
}
# ............................................ private functions ...
my @files;
local *PUSH = sub ($)
{
local ( $ARG ) = @ARG;
if ( $stdout )
{
Stdout $ARG;
}
else
{
unless ( m,[/\\], )
{
$ARG = cwd() . "/" . $ARG ;
}
push @files, $ARG if not $stdout;
}
};
# ..................................................... download ...
my $ret;
my $i = 0;
my @list = sort @$list;
for ( @list )
{
$i++;
# sometimes the file includes a version number, which
# may be instructed to be removed by user configuration tag.
# Respect it. But if there are many files then we do not
# have a choice.
#
# save: this-name.txt
my $saveFile = $file;
$debug and print "$id: SAVEFILE-1 $saveFile\n";
# foo.txt?format=txt
$saveFile =~ s/\?.*//;
# Sourceforge special
$saveFile =~ s,/download$,,;
$debug and print "$id: SAVEFILE-1b $saveFile\n";
if ( $stdout )
{
$saveFile = TempFile();
}
elsif ( @list == 1 and $saveopt )
{
$saveFile = $saveopt;
}
elsif ( @list > 1 or $file eq '' or ($find and not $saveopt) )
{
# Sourceforge special
my $tmp = $ARG;
$tmp =~ s,/download$,,;
$saveFile = basename $tmp;
$debug and print "$id: SAVEFILE-1c $saveFile [@list]\n";
}
my $relative = $ARG || $baseUrl;
$debug and print "$id: SAVEFILE-2 $saveFile RELATIVE $relative\n";
if ( $ARG and not m,://, )
{
# If the ARG is NOT ABSOLUTE reference ftp:// or http://
# Then glue together the base site + relative reference found
# from page
$debug and print "$id: glue [$baseUrl] + [$ARG]\n";
$relative = RelativePath BaseUrl($baseUrl), $ARG;
# The whole URL is now known, strip PATH from savefile.
$saveFile = basename $saveFile;
}
unless ( $relative )
{
warn "$id: [ERROR] Can't resolve relative $baseUrl + [$ARG]";
next;
}
my $url = $relative;
$url = UrlManipulateMain $url, $mirror;
if ( $rename )
{
$saveFile = EvalCode $url, $saveFile, $rename
}
$saveFile = FileNameFix $saveFile;
unless ( $stdout )
{
next if UrlHttpFileCheck savefile => $saveFile
, unpack => $unpack
, overwrite => $overwrite
;
}
my $progress = DownloadProgress $baseUrl, $ARG, "$id: ..."
, $i, scalar @list;
my $request = new HTTP::Request('GET' => $url );
my $obj = $ua->request($request , $saveFile );
my $stat = $obj->is_success;
if ( $debug )
{
print "$id: content-type:\n\t", $obj->content_type, "\n"
, "\tsuccess status ", $stat, "\n"
, map { $ARG = "\t$ARG\n" } $obj->headers_as_string
;
}
# ........................................... file downloaded ...
if ( $stat )
{
PUSH($saveFile);
my $contentStatus = FileContentAnalyze $saveFile, $contentRegexp;
my $err;
$err = "[no match] " unless $contentStatus;
if ( (not $contentStatus and $verb > 1)
or
($contentStatus and $verb)
)
{
$verb and print "$progress ${err}$url => $saveFile\n";
}
}
else
{
$verb and print "$progress $url => $saveFile\n";
$errUrlHashRef->{ $url } = $obj->code;
# There is new error code, record it.
if ( not defined $errUrlHashRef->{ $obj->code } )
{
$errExplanationHashRef->{ $obj->code } = $obj->message;
}
$ret = $errUrlHashRef->{ $obj->code };
warn " ** error: $url ", $obj->message, "\n";
}
}
$ret, @files;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Get content of URL
#
# INPUT PARAMETERS
#
# $url The URL pointer
# $file
# $regexp
# $regexpNo
# $proxy
# \%errUrlHashRef Hash where to store the URL-ERROR_CODE
# \%errExplanationHashRef Hash where to store ERROR_CODE-EXPLANATION
# $new Get never file
# $stdout Write to stdout
# $versionRegexp How to find the version number from page
#
# RETURN VALUES
#
# () RETURN LIST whose elements are
#
# $stat Error reason or "" => ok
# @ list of retrieved files
#
# ****************************************************************************
sub UrlHttp ( % )
{
my $id = "$LIB.UrlHttp";
my %arg = @ARG ;
# .............................................. input arguments ...
# check mandatory
not exists $arg{url} and die "$id: URL missing";
not exists $arg{file} and die "$id: FILE missing";
not exists $arg{errUrlHashRef} and die "$id: HashRef missing";
not exists $arg{errExplanationHashRef} and die "$id: errHashRef missing";
# Read values
my $url = $arg{url};
my $file = $arg{file};
my $errUrlHashRef = $arg{errUrlHashRef};
my $errExplanationHashRef = $arg{errExplanationHashRef};
my $proxy = $arg{proxy} || '';
my $regexp = $arg{regexp} || '';
my $regexpNo = $arg{regexpNo} || '';
my $new = $arg{new} || 0;
my $stdout = $arg{stdout} || 0;;
my $versionRegexp = $arg{versionRegexp} || '';
my $thisPage = $arg{plainPage} || 0;
my $thisPageRegexp = $arg{pageRegexp} || '';
my $contentRegexp = $arg{contentRegexp} || '';
my $conversion = $arg{conversion} || '';
my $rename = $arg{rename} || '';
my $saveopt = $arg{save} || '';
my $unpack = $arg{unpack} || '';
my $overwrite = $arg{overwrite} || '';
my $mirror = $arg{mirror} || '';
my $find = $thisPage eq -find ? 1 : 0;
# ......................................................... code ...
if ( $debug )
{
print "$id: INPUT\n"
, "\turl : $url\n"
, "\tfile : $file\n"
, "\trename : $rename\n"
, "\tconversion: $conversion\n"
, "\tregexp : $regexp\n"
, "\tregexp-no : $regexpNo\n"
, "\tthis page : $thisPage\n"
, "\tfind : $find\n"
, "\tvregexp : $versionRegexp\n"
, "\tpregexp : $thisPageRegexp\n"
, "\tcregexp : $contentRegexp\n"
, "\tproxy : $proxy\n"
, "\tnew : $new\n"
, "\tstdout : $stdout\n"
, "\tcwd : ", cwd(), "\n"
, "\toverwrite : $overwrite\n"
}
# FIXME: remove, this is not the final save name
# $verb and print "$id: $url --> $file\n";
my $ua = new LWP::UserAgent;
if ( defined $proxy )
{
$debug and $proxy and print "$id: Using PROXY $proxy\n";
$ua->proxy( "http", "$proxy" );
}
my ($baseUrl, $getFile) = ($url,"");
unless ( $thisPage )
{
($baseUrl, $getFile) = ( $url =~ m,^(.*/)(.*), );
}
$baseUrl = UrlManipulateMain $url, $mirror;
if ( $getFile eq ''
and ($regexp eq '' or $thisPageRegexp eq '')
and not $thisPage
)
{
die "$id: [ERROR] invalid URL $url. No file name part found."
, " Did you forgot to use <page:> or <pregexp:> ?"
;
}
my @list = ( $getFile );
if ( $new ) # Directory lookup
{
my $getPage = $thisPage ? $url : $baseUrl ;
$debug and print "$id: getPage 1 $getPage\n";
if ( $file )
{
$getPage =~ s/\Q$file//;
$debug and print "$id: getPage 2 file [$file] $getPage\n";
}
@list = UrlHttpSearchNewest
useragent => $ua
, page => $getPage
, flag => $thisPage
, file => $file
, baseurl => $baseUrl
, getfile => $getFile
, versionRE => $versionRegexp
, pageRE => $thisPageRegexp
, RE => $regexp
, REno => $regexpNo
;
}
elsif ( $find )
{
@list = UrlHttpSearchPage
url => $url
, regexpno => $regexpNo
, baseurl => $baseUrl
, pageregexp => $thisPageRegexp
;
}
# ............................................ get list of files ...
# Multiple links to the same destination
@list = ListRemoveDuplicates @list;
local $ARG;
$debug and print "$id: FILE LIST [@list]\n";
$verb and !@list and print "$id: No matching files [$regexp]\n";
if ( @list > 1 and $file and not $new)
{
$file = '';
$debug and
print "$id: Clearing FILE: [$file] because many/new"
, " files to load. "
, "\@list = count, ", scalar @list, ", [@list]\n"
;
}
$file = $getFile;
if ( $new )
{
local $ARG = $list[0];
$file = $ARG unless /[?&]/; # Ignore PHP and exotic paths
}
my ($ret, @files) = UrlHttpDownload
useragent => $ua
, list => \@list
, file => $file
, stdout => $stdout
, find => $find
, saveopt => $saveopt
, baseurl => $baseUrl
, unpack => $unpack
, rename => $rename
, errhash => $errUrlHashRef
, contentre => $contentRegexp
, errtext => $errExplanationHashRef
, overwrite => $overwrite
, mirror => $mirror
;
$ret, @files;
}
# ****************************************************************************
#
# DESCRIPTION
#
# Copy content of PATH to FILE.
#
# INPUT PARAMETERS
#
# $path From where to read. If this is directory, read files
# in directory. If this is file, copy file.
#
# $file Where to put results.
# $prefix [optional] Filename prefix
# $postfif [optional] postfix
#
# RETURN VALUES
#
# () RETURN LIST whose elements are:
#
# $stat Error reason or "" => ok
# @ list of retrieved files
#
#
# ****************************************************************************
sub UrlFile (%)
{
my $id = "$LIB.UrlFile";
my %arg = @ARG;
my $path = $arg{path} || die "$id: Missing arg PATH";
my $file = $arg{file} || die "$id: Missing arg FILE";
my $prefix = $arg{prefix} || '';
my $postfix = $arg{postfix} || '';
my $overwrite = $arg{overwrite} || '';
my ( $stat, @files );
$debug and warn "$id: PATH $path, FILE $file\n";
if ( -f $path and not -d $path )
{
if ( $CHECK_NEWEST )
{
my @dir = DirContent dirname( $path );
if ( @dir )
{
my $base = dirname($path);
$file = LatestVersion basename($path) , \@dir;
$path = $base . "/" . $file;
}
else
{
$verb and print "$id: Can't set newest $file";
}
}
$file = $prefix . $file . $postfix;
$debug and warn "$id: FileCopy $path => $file\n";
unless ( copy($path, $file) )
{
$verb and print "$id: FileCopy $path => $file $ERRNO";
}
else
{
push @files, $file;
}
}
else
{
my @tmp = DirContent $path;
local *FILE;
$file =~ s,/,!,g;
if ( -e $file and not $overwrite )
{
$verb and print "$id: [ignored, exists] $file\n";
return;
}
unless ( open FILE, "> $file" )
{
warn "$id: can't write $file $ERRNO\n";
return;
}
print FILE join "\n", @tmp;
close FILE;
push @files, $file;
}
( $stat, @files );
}
# ****************************************************************************
#
# DESCRIPTION
#
# Run Some self tests. This is for developer only
#
# INPUT PARAMETERS
#
# none
#
# RETURN VALUES
#
# none
#
# ****************************************************************************
sub TestDriverSfMirror ()
{
my $id = "$LIB.TestDriverSfMirror";
$debug = 3 unless $debug;
my $str = << "EOF";
<caption>
Download Mirrors
</caption>
<tr>
<th>
Host
</th>
<th>
Location
</th>
<th>
Continent
</th>
<th>
Download
</th>
</tr> <tr class="odd">
<td><a href="http://www.optusnet.com.au"><img alt="optusnet logo
" border="0" src="http://images.sourceforge.net/prdownloads/optusnet_100x34.gif"
></a></td>
<td>Sydney, Australia</td>
<td>Australia</td>
<td><a href="/bogofilter/bogofilter-1.0.0.tar.bz2?use_mirror=opt
usnet"><b>Download</b></a></td>
</tr> <tr >
<td><a href="http://www.mesh-solutions.com/sf/"><img alt="mesh logo" border="0" src="http://images.sourceforge.net/prdownloads/mesh_100_34_3.gif"></a></td>
<td>Duesseldorf, Germany</td>
<td>Europe</td>
<td><a href="/bogofilter/bogofilter-1.0.0.tar.bz2?use_mirror=mesh"><b>Download</b></a></td>
</tr> <tr class="odd">
<td><a href="http://www.mirrorservice.org/help/introduction.html"><img alt="kent logo" border="0" src="http://images.sourceforge.net/prdownloads/ukms-button-100x34.png"></a></td>
<td>Kent, UK</td>
<td>Europe</td>
<td><a href="/bogofilter/bogofilter-1.0.0.tar.bz2?use_mirror=kent"><b>Download</b></a></td>
</tr> <tr >
<td><a href="http://www.heanet.ie"><img alt="heanet logo" border="0" src="http://images.sourceforge.net/prdownloads/heanet_100x34.gif"></a></td>
<td>Dublin, Ireland</td>
<td>Europe</td>
<td><a href="/bogofilter/bogofilter-1.0.0.tar.bz2?use_mirror=heanet"><b>Download</b></a></td>
</tr> <tr class="odd">
<td><a href="http://www.ovh.com"><img alt="ovh logo" border="0"
src="http://images.sourceforge.net/prdownloads/ovh_100x34.jpg"></a></td>
<td>Paris, France</td>
<td>Europe</td>
<td><a href="/bogofilter/bogofilter-1.0.0.tar.bz2?use_mirror=ovh"><b>Download</b></a></td>
</tr> <tr >
<td><a href="http://www.puzzle.ch/sourceforge.net/"><img alt="puzzle logo" border="0" src="http://images.sourceforge.net/prdownloads/puzzleitc_100x34.gif"></a></td>
<td>Bern, Switzerland</td>
<td>Europe</td>
<td><a href="/bogofilter/bogofilter-1.0.0.tar.bz2?use_mirror=puzzle"><b>Download</b></a></td>
EOF
print "$id: UrlSfMirrorParse\n";
UrlSfMirrorParse $str;
}
sub SelfTest ()
{
my $id = "$LIB.SelfTest";
$debug = 1 unless $debug;
my (@files, $file, $i);
local $ARG;
# ............................................................ X ...
$i++;
$file = "artist-1.1-beta1.tar.gz",
print "$id: [$i] LatestVersion ", "." x 40, "\n" ;
@files = qw
(
mailto:tab@lysator.liu.se
emacs-shapes.gif
emacs-shapes.html
emacs-a.gif
emacs-a.html
emacs-rydmap.gif
emacs-rydmap.html
COPYING
artist-1.2.3.tar.gz
artist.el
mailto:kj@lysator.liu.se
mailto:jdoe@example.com
http://st-www.cs.uiuc.edu/~chai/figlet.html
artist-1.2.1.tar.gz
artist-1.2.tar.gz
artist-1.1.tar.gz
artist-1.1a.tar.gz
artist-1.1-beta1.tar.gz
artist-1.0.tar.gz
artist-1.0-11.tar.gz
mailto:tab@lysator.liu.se
);
LatestVersion $file, \@files;
# ............................................................ X ...
$i++;
$file = "irchat-900625.tar.gz";
print "$id: [$i] LatestVersion ", "." x 40, "\n" ;
@files = qw
(
./dist/irchat/irchat-20001203.tar.gz
./dist/irchat/irchat-19991105.tar.gz
./dist/irchat/irchat-980625-2.tar.gz
./dist/irchat/irchat-980128.tar.gz
./dist/irchat/irchat-971212.tar.gz
./dist/irchat/irchat-3.04.tar.gz
./dist/irchat/irchat-3.03.tar.gz
./dist/irchat/irchat-3.02.tar.gz
./dist/irchat/irchat-3.01.tar.gz
./dist/irchat/irchat-3.00.tar.gz
);
LatestVersion $file, \@files;
# ............................................................ X ...
$i++;
$file = "bogofilter-0.9.1.tar.gz";
print "$id: [$i] LatestVersion ", "." x 40, "\n" ;
@files = qw
(
http://freecode.com
http://newsletters.osdn.com
http://ads.osdn.com/?ad_id=2435&alloc_id=5907&op=click
http://sourceforge.net
http://sf.net
http://sf.net/support/getsupport.php
/bogofilter/?sort_by=name&sort=desc
/bogofilter/?sort_by=size
/bogofilter/?sort_by=date
/bogofilter/..
/bogofilter/ANNOUNCE
/bogofilter/ANNOUNCE-0.94.12
/bogofilter/Judy-0.0.2-1.i386.rpm
/bogofilter/Judy-0.04-1.i386.rpm
/bogofilter/Judy-0.04-1.src.rpm
/bogofilter/Judy-devel-0.04-1.i386.rpm
/bogofilter/Judy_trial.0.0.4.src.tar.gz
/bogofilter/NEWS
/bogofilter/NEWS-0.10
/bogofilter/NEWS-0.10.0
/bogofilter/NEWS-0.11
/bogofilter/bogofilter-0.10.0-1.i586.rpm
/bogofilter/bogofilter-0.10.0-1.src.rpm
/bogofilter/bogofilter-0.10.0.tar.gz
/bogofilter/bogofilter-0.96.6-1.src.rpm
/bogofilter/bogofilter-0.96.6.tar.bz2
/bogofilter/bogofilter-0.96.6.tar.gz
/bogofilter/bogofilter-1.0.0-1.i586.rpm
/bogofilter/bogofilter-1.0.0-1.src.rpm
/bogofilter/bogofilter-1.0.0.tar.bz2
/bogofilter/bogofilter-1.0.0.tar.gz
/bogofilter/bogofilter-faq.html
/bogofilter/bogofilter-static-0.13.1-1.i586.rpm
/bogofilter/bogofilter-static-0.13.2-1.i586.rpm
/bogofilter/bogofilter-static-0.13.2.1-1.i586.rpm
);
LatestVersion $file, \@files;
# ............................................................ X ...
$i++;
print "$id: [$i] FileDeCompressedCmd ", "." x 40, "\n" ;
for ( qw
(
1.tar 1.tar.gz 1.tgz
2.bz2 2.tar.bz2
3.zip
3.rar
))
{
eval { FileDeCompressedCmd $ARG };
print $EVAL_ERROR if $EVAL_ERROR;
}
exit;
}
# ****************************************************************************
#
# DESCRIPTION
#
#
#
# INPUT PARAMETERS
#
# \@data Configuration file content
#
#
# RETURN VALUES
#
# none
#
# ****************************************************************************
sub Main ($ $)
{
my $id = "$LIB.Main";
my ( $TAG_NAME, $data ) = @ARG;
if ( $TAG_NAME )
{
$debug and warn "$id: Tag name search [$TAG_NAME]\n";
}
# This is an old relict and not used
my %EXTRACT_HASH =
(
'\.tar\.gz$' => "gzip -d -c %s | tar xvf -"
, '\.gz$' => "gzip -f -d %s"
, '\.bz2$' => "bzip2 -f -d %s"
, '\.tar$' => "tar xvf %s"
, '\.tgz$' => "tar -zxvf %s" # GNU TAR
, '\.zip$' => "unzip %s"
, '\.xz$' => "xz -d -c %s | tar xvf -"
);
# ............................................... prepare output ...
if ( $OUT_DIR )
{
$verb and print "$id: chdir $OUT_DIR\n";
chdir $OUT_DIR or die "$id: chdir $OUT_DIR $ERRNO";
}
my $date = DateYYYY_MM_DD();
my $count = 0;
my ( %URL_ERROR_HASH , %URL_ERROR_REASON_HASH );
my $TagLine;
local $ARG;
for ( @$data )
{
chomp;
my $line = $ARG;
s/^\s*[#].*$//; # Kill comments
next if /^\s*$/; # ignore empty lines
# ............................................ Variable defs ...
# todo: should be removed, this was for gz = 'command'
my %variables;
%variables = /'(\S+)'\s*=\s*(.*)/g;
while ( my($var, $val) = each %variables )
{
$debug and warn "$id:\t\t$var = $val\n";
$EXTRACT_HASH{ $var } = $val;
}
# ............................................... directives ...
my $LINE = $ARG; # make a secure copy
my $new = $CHECK_NEWEST;
my $unpack = $EXTRACT;
my $overwrite = $OVERWRITE;
my $contentRegexp = $CONTENT_REGEXP;
my $mirror = $MIRROR;
$TagLine = $ARG if /tag\d+:/; # Remember tag name
my $pass = ExpandVars($1) if /\bpass:\s*(\S+)/;
my $login = $1 if /\blogin:\s*(\S+)/;
my $regexp = $1 if /\bregexp:\s*(\S+)/;
my $regexpNo = $1 if /\bregexp-no:\s*(\S+)/;
$new = 1 if /\bnew:/;
$unpack = 1 if /\bx:/;
$unpack = -noroot if /\bxx:/;
my $xopt = $1 if /\bxopt:\s*(\S+)/;
my $lcd = $1 if /lcd:\s*(\S+)/;
$overwrite = 1 if /\bo(verwrite)?:/;
my $vregexp = $1 if /\bvregexp:\s*(\S+)/;
my $fileName = $1 if /\bfile:\s*(\S+)/;
my $rename = $1 if /\brename:\s*(\S+)/;
$mirror = $1 if /\bmirror:\s*(\S+)/;
my $pageRegexp = $1 if /\bpregexp:\s*(\S+)/;
$contentRegexp = $1 if /\bcregexp:\s*(\S+)/;
my $conversion = -text if /\btext:/;
if ( /\bco?nv:\s*(\S+)/ ) # old implementation used tag <cnv:>
{
local $ARG = $1;
if ( /te?xt/i )
{
$conversion = -text
}
else
{
warn "$id: Unknown conversion [$ARG] [$line]";
}
}
my $plainPage;
if ( /\bpage:/ )
{
$plainPage = 1;
if ( /\bpage:\s*find/i )
{
$plainPage = -find;
}
}
# "lcd-ohio" is valid tag name, but "lcd" is our
# directive. Accept word names after OUR directives.
if ( $verb and not /print:/ and /(?:^|\s)(:[-a-z]+)\b/ )
{
print "$id: [WARNING] directive, leading colon? [$1] $ARG\n";
}
if ( $lcd )
{
$debug > 2 and print "$id: LCD $lcd\n";
DirectiveLcd -dir => $lcd
, -mkdir => $LCD_CREATE unless $NO_LCD;
}
# ................................................... regexp ...
if ( defined $URL_REGEXP )
{
if ( /$URL_REGEXP/o )
{
$debug and warn "$id: REGEXP match [$URL_REGEXP] $ARG\n"
}
else
{
$debug > 3 and warn "$id: [regexp ignored] $ARG\n";
next;
}
}
if ( defined $TAG_REGEXP )
{
my $stop;
($ARG, $stop) = TagHandle $ARG, $TAG_NAME;
last if $stop;
next if $ARG eq '';
}
# ................................................. grab url ...
$ARG = ExpandVars $ARG if /\$/ and ! $rename;
if ( $verb and /(print:\s*)(.+)/ ) # Print user messages
{
print "$TagLine: $2\n";
next;
}
m,^\s*((https?|ftp|file):/?(/([^/\s]+)(\S*))),;
unless ( $1 and defined $2 )
{
if ( /https/ )
{
warn "$id: https is not supported, just http://";
}
$debug and warn "$id: [skipped] not URL: $line [$ARG]\n";
next;
}
# ............................................... components ...
my $urlOrig = $1;
my $url = $urlOrig; # may be changed
my $type = $2;
my $path = $3;
my $site = $4;
my $sitePath = $5;
# Remove leading slash if we log with real username.
# The path is usually relative to the directory under LOGIN.
#
# For anonymous, the path is absolute.
$sitePath =~ s,^/,, if $login;
my $origFile = $sitePath;
if ( $type eq 'https' )
{
eval "use Crypt::SSLeay";
if ( $EVAL_ERROR )
{
warn "HTTPS requires Crypt::SSLeay.pm [$EVAL_ERROR]";
next;
}
}
my $file;
# The page:find command may instruct to search
#
# http://some.com/~foo
# http://some.com/
#
# Do not consider those to contain filename part
if (
$plainPage ne -find
or $url !~ m,/$,
or $url !~ m,/[~][^/]+$,
)
{
($file = $url) =~ s,^\s*\S+/,,;
$file = $fileName if $fileName ne '';
}
if ( /http/ and $file eq '' and not $plainPage )
{
$file = $path . "000root-file";
}
$debug and print "$id: VARIABLES\n"
, "\tURL = $url\n"
, "\tFILE = $file\n"
, "\tFILE_NAME = $fileName\n"
, "\tTYPE = $type\n"
, "\tPATH = $path\n"
, "\tSITE = $site\n"
, "\tSITE_PATH = $sitePath\n"
, "\tCONVERSION = $conversion\n"
;
my $saveopt;
if ( $NO_SAVE == 0 and /save:\s*(\S+)/ )
{
$saveopt = $1;
$file = $1;
}
my $postfix = $POSTFIX if defined $POSTFIX;
my $prefix = $PREFIX if defined $PREFIX;
$prefix = $site . "::" . $prefix if $PREFIX_WWW;
$prefix = $date . "::" . $prefix if $PREFIX_DATE;
$file = $prefix . $file . $postfix;
# Sourceforge special
if ( IsSourceforgeDownload $url )
{
$url = $urlOrig; # reset everything
$file = "";
}
# .................................................... do-it ...
$debug and
print "$id: type <$type> site <$site>"
. "path <$path> url <$url> file <$file>\n";
$ARG = $type;
my ($stat, @files);
# ******************************************************************
# Set global which is used in error messages or if program must die.
$CURRENT_TAG_LINE = $line;
# *******************************************************************
$verb and print "$id: DIRECTORY ", cwd(), "\n";
if ( /http/ )
{
$count++;
if ( $plainPage eq -find and not $pageRegexp )
{
die "$id: no <pregexp:> directive"
, " LINE => [$line]"
;
}
if ( $pageRegexp and not $plainPage )
{
$debug and print "$id: Implicit <page:find> [$line]\n";
$plainPage = -find;
}
if ( ($plainPage ne -find) and $pageRegexp and not $file )
{
$debug and print "$id: Expecting [page:find]",
, " for non-named download file"
, " [$url]"
, " LINE => [$line]"
;
$plainPage = -find;
}
elsif ( $plainPage ne -find and $pageRegexp )
{
$plainPage = -find;
}
if ( $saveopt and $pageRegexp and $verb > 1 )
{
chomp;
warn "$id: [WARNING] mixing <save:> and <pregexp:>"
, " May give multiple answers. Use absolute filename"
, " URL with <save:> LINE => [$line]"
;
}
if ( $pageRegexp and not $plainPage )
{
warn "$id: [WARNING] no page: directive [$ARG]\n";
}
if ( $pageRegexp and not $file and ($plainPage ne -find))
{
warn "$id: [WARNING] no file: directive. [$ARG]\n";
}
if ( $pageRegexp and not $file and ($plainPage ne -find))
{
warn "$id: [WARNING] no file: directive. [$ARG]\n";
}
($stat, @files) = UrlHttp
url => $url
, file => $file
, regexp => $regexp
, regexpNo => $regexpNo
, proxy => $PROXY
, errUrlHashRef => \%URL_ERROR_HASH
, errExplanationHashRef => \%URL_ERROR_REASON_HASH
, new => $new
, stdout => $STDOUT
, versionRegexp => $vregexp
, plainPage => $plainPage
, pageRegexp => $pageRegexp
, contentRegexp => $contentRegexp
, conversion => $conversion
, rename => $rename
, save => $saveopt
, origLine => $line
, unpack => $unpack
, overwrite => $overwrite
, mirror => $mirror
;
}
elsif ( /ftp/ )
{
$count++;
my ($pproto, $ssite, $ddir, $ffile) = SplitUrl $url;
if ( $ffile and $ffile !~ /[.]/ )
{
# ftp://some.com/dir/dir
warn "$id: Did you forgot trailing slash? [$line]";
}
if ( $regexp )
{
# There can't be serched "file" if regexp is used.
$origFile = '';
$file = '';
$sitePath = Slash $sitePath;
}
if ( $fileName and ! $origFile )
{
$origFile = $fileName; # the "new" search.
}
if ( $pageRegexp )
{
chomp;
warn "$id: [WARNING] mixing ftp:// and <pregexp:>"
, " Did you mean <regexp:> instead?"
, " LINE => [$line]"
;
}
# Directory path given, so reset the file
$origFile = '' if $origFile =~ m,/$,;
($stat, @files ) = UrlFtp
site => $site
, url => $url
, path => $sitePath
, getFile => $origFile
, saveFile => $file
, regexp => $regexp
, regexpNo => $regexpNo
, firewall => $FIREWALL
, login => $login
, pass => $pass
, new => $new
, stdout => $STDOUT
, conversion => $conversion
, rename => $rename
, origLine => $line
, unpack => $unpack
, overwrite => $overwrite
;
}
elsif ( /file/ )
{
($stat, @files) = UrlFile path => $path
, file => $origFile
, prefix => $prefix
, postfix => $postfix
, overwrite => $overwrite
;
$count++;
}
# .............................................. conversion ...
if ( $conversion eq -text )
{
for my $file ( @files )
{
FileHtml2txt $file;
}
}
elsif ( $conversion )
{
warn "$id: Unknown conversion [$conversion]";
}
# .................................................. &unpack ...
if ( $unpack and not $NO_EXTRACT )
{
$debug and print "$id: extracting [@files]\n";
@files and Unpack \@files, \%EXTRACT_HASH, $unpack, $xopt;
}
}
if ( not $count and $verb)
{
$URL_REGEXP
and printf "$id: No labels matching regexp [%s]\n",
$URL_REGEXP;
@TAG_LIST
and printf "$id: Nothing to do. No tag matching [%s]\n",
join(' ', @TAG_LIST);
if ( @CFG_FILE == 0 )
{
print "$id: Nothing to do. Use config file or give URL? ",
"Did you mean --tag for [@ARGV]?\n"
;
}
}
}
# ****************************************************************************
#
# DESCRIPTION
#
# Parse VAR = VALUE statements. The values are put to %ENV
#
# INPUT PARAMETERS
#
# @lines
#
# RETURN VALUES
#
# none
#
# ****************************************************************************
sub ConfigVariableParse (@)
{
my $id = "$LIB.ConfigVariableParse";
my @data = @ARG;
local $ARG;
for ( @data )
{
s/#.*//;
next unless /\S/;
next if /rename:/; # Skip Perl command line, which may contain '='
my %variables = /(\S+)\s*=\s*(\S+)/g;
while ( my($var, $val) = each %variables )
{
# Save values to environment.
# Ignore some URLS, that look like variable assignments:
# print http://example.com/viewcvs/vc-svn.el?rev=HEAD
$debug > 2 and print "$id:[$var] = [$val] [$ARG]\n";
if ( $var =~ m![?,.&:]!i )
{
$debug > 2 and print "$id: IGNORED. Wasn't a variable\n";
next;
}
$ARG = $val;
$val = ExpandVars $ARG;
$debug > 2 and print "$id: assigning ENV $var => $val\n";
$ENV{ $var } = $val;
}
}
}
# ****************************************************************************
#
# DESCRIPTION
#
# Read Configuration file contents. The directive can be in format:
#
# include <file> Read from User's current dir
# include <$HOME/file> Expand $HOME
# include </etc/file> Absolute path
# include <THIS/file> Read from same directory where the
# current file with includes reside.
#
# INPUT PARAMETERS
#
# $file
#
# RETURN VALUES
#
# @lines
#
# ****************************************************************************
sub ConfigRead ( $ ); # Recursive call needs prototyping
{
my %staticInclude; # already included files, do not read again
my $staticPwd; # Current pwd
sub ConfigRead ( $ )
{
my $id = "$LIB.ConfigRead";
my $file = shift;
$verb > 2 and print "$id: Reading config [$file]\n";
$file = PathConvertSmart $file;
$verb > 2 and print "$id: Reading config CONVERSION [$file]\n";
if ( $debug > 1 )
{
print "$id: input FILE $file "
, ExpandVars($file)
, " ["
, join(' ', %staticInclude)
, "]\n"
;
}
# .............................................. already included ...
# In windows c:/dir is same as C:/DIR
my $check = $file;
$check = lc $file if $WIN32;
if ( exists $staticInclude{$check} )
{
$debug and print "$id: skipped, already included $file\n";
return;
}
# .......................................................... pwd ...
my $dir = dirname $file;
$staticPwd = cwd() unless $staticPwd; # set initial value
$debug > 2 and print "$id: PWD $staticPwd\n";
if ( -f $file and not $dir =~ /^.$|THIS/ )
{
my $orig = cwd();
# Peek where are we going, handles ../../ cases too
if ( chdir $dir )
{
$staticPwd = cwd(); # ok, set "THIS" location
chdir $orig; # Back to original directory
}
}
elsif ( $dir eq "THIS" )
{
$file = "$staticPwd/" . basename $file;
$debug and print "$id: THIS set to $file\n";
}
# .......................................................... read ...
my ($lineArrRef, $status);
if ( -f $file )
{
($lineArrRef, $status) = FileRead $file;
}
else
{
$status = "File does not exist $file";
}
$staticInclude{ $file } = 1;
if ( $status )
{
$verb > 0 and warn "$id: SKIPPED, Can't include $file";
return;
}
if ( @$lineArrRef )
{
ConfigVariableParse @$lineArrRef;
local $ARG;
my @lines;
for my $line ( @$lineArrRef )
{
push @lines, $line;
# Skip INCLUDE statements that have been commented out.
$ARG = $line;
s/#.*//;
next unless /[a-z]/i;
# include <this>
# include <this>
# include < this/here >
# include < c:/Progrm Files/this/here >
if ( /include\s+<\s*(.*[^\s]+)\s*>/i )
{
my $inc = $1;
my $path = ExpandVars $inc;
my $already = exists $staticInclude{$path};
$debug > 1 and
print "$id: RECURSIVE INCLUDE [$path] [$inc]"
, " already flag [$already]\n";
unless ( $already )
{
push @lines, ConfigRead $path;
$path = lc $path if $WIN32;
$staticInclude{ $path } = 1;
}
}
}
@$lineArrRef = @lines;
$debug > 4 and print "$id: READ config $file\n@lines\n\n";
}
else
{
$debug and print "$id: Nothing found from $file\n";
}
@$lineArrRef;
}}
# }}}
# {{{ more
# ****************************************************************************
#
# DESCRIPTION
#
# Start, the start of the program.
#
# INPUT PARAMETERS
#
# None
#
# RETURN VALUES
#
# None
#
# ****************************************************************************
sub Boot ()
{
Initialize();
HandleCommandLineArgs();
my $id = "$LIB.Boot";
# ......................................................... args ...
$debug > 2 and PrintHash "$id: begin ENV", %ENV;
unless ( @ARGV )
{
$debug > 1 && warn "$id: No plain command line arguments\n";
}
# Convert any command line arguments as if they would appear
# in configuration file:
#
# --site-regexp gz http://there.at/
#
# --> http://there.at page: pregexp:gz
for my $arg ( @ARGV )
{
if ( $SITE_REGEXP )
{
if ( /ftp/i )
{
$arg .= " regexp:$SITE_REGEXP";
}
else
{
$arg .= " page: pregexp:$SITE_REGEXP";
}
}
}
my @data;
if ( $CFG_FILE_NEEDED and @CFG_FILE )
{
for my $arg ( @CFG_FILE )
{
my @lines = ConfigRead $arg;
push @data, @lines;
}
if ( $debug > 4 )
{
print "$id: CONFIG-FILE-CONTENT-BEGIN\n"
, @data
, "$id: CONFIG-FILE-CONTENT-END\n"
;
}
}
$debug > 4 and PrintHash "$id: end ENV", %ENV;
push @data, @ARGV if @ARGV; # Add command line URLs
if ( @TAG_LIST )
{
for my $arg ( @TAG_LIST )
{
TagHandle undef, undef, "1-reset";
Main $arg, \@data;
}
}
else
{
Main "", \@data;
}
}
sub Test ()
{
my $str = join '', <>;
$debug = 1;
print UrlHttpParseHref content => $str, regexp => "tar.gz";
}
Boot();
# }}}
0;
__END__
|