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
|
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal. All Rights Reserved. */
/* bgopal: (1993-4) redesigned/rewritten using agrep's library interface */
#include <sys/param.h>
#include <errno.h>
#include "glimpse.h"
#include "defs.h"
#include <fcntl.h>
#include "checkfile.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/file.h> /* for flock definition */
#if ISO_CHAR_SET
#include <locale.h> /* support for 8bit character set */
#endif
#define CLIENTSERVER 1
#define USE_MSGHDR 0
#define USE_UNIXDOMAIN 0
#define DEBUG 0
#define DEF_SERV_PORT 2001
#define MIN_SERV_PORT 1024
#define MAX_SERV_PORT 30000
#define SERVER_QUEUE_SIZE 10 /* number of requests to buffer up while processing one request = 5 */
/* Borrowed from C-Lib */
extern char **environ;
extern int errno;
#if CLIENTSERVER
#include "communicate.c"
#endif /*CLIENTSERVER*/
/* For client-server protocol */
CHAR *SERV_HOST = NULL;
int SERV_PORT;
char glimpse_reqbuf[MAX_ARGS*MAX_NAME_LEN];
extern int glimpse_clientdied; /* set if signal received about dead socket: need agrep variable so that exec() can return quickly */
int glimpse_reinitialize = 0;
/* Borrowed from agrep.c */
extern int D_length; /* global variable in agrep */
extern int D; /* global variable in agrep */
extern int pattern_index;
/* These are used for byte level index search */
extern CHAR CurrentFileName[MAX_LINE_LEN];
extern int SetCurrentFileName;
extern int CurrentByteOffset;
extern int SetCurrentByteOffset;
extern long CurrentFileTime;
extern int SetCurrentFileTime;
extern int execfd;
extern int agrep_initialfd;
extern CHAR *agrep_inbuffer;
extern int agrep_inlen;
extern int agrep_inpointer;
extern FILE *agrep_finalfp;
extern CHAR *agrep_outbuffer;
extern int agrep_outlen;
extern int agrep_outpointer;
extern int glimpse_call; /* prevent agrep from printing out its usage */
extern int glimpse_isserver; /* prevent agrep from asking for user input */
int first_search = 1; /* intra/interaction in process_query() and glimpse_search() */
#if ISSERVER
int RemoteFiles = 0; /* Are the files present locally or remotely? If on, then -NQ is automatically added to all search options for each query */
#endif
/* Borrowed from index/io.c */
extern int InfoAfterFilename;
extern int OneFilePerBlock;
extern int StructuredIndex;
extern unsigned int *dest_index_set;
extern unsigned char *dest_index_buf;
extern unsigned int *src_index_set;
extern unsigned char *src_index_buf;
extern unsigned char *merge_index_buf;
extern int mask_int[32];
extern int indexable_char[256];
int test_indexable_char[256];
extern int p_table[MAX_PARTITION];
extern int GMAX_WORD_SIZE;
extern int IndexNumber; /* used in getword() */
extern int InterpretSpecial; /* used to "not-split" agrep-regexps */
extern int UseFilters; /* defined in build_in.c, used for filtering routines in io.c */
extern int ByteLevelIndex;
extern int RecordLevelIndex;
extern int rdelim_len;
extern char rdelim[MAX_LINE_LEN];
extern char old_rdelim[MAX_LINE_LEN];
extern int file_num;
extern int REAL_PARTITION, REAL_INDEX_BUF, MAX_ALL_INDEX, FILEMASK_SIZE;
/* Borrowed from get_filename.c */
extern int bigbuffer_size;
extern char *bigbuffer;
extern char *outputbuffer;
/* OPTIONS/FLAGS */
int veryfast = 0;
int CONTACT_SERVER = 0; /* Should client try to call server at all or just process query on its own? */
int NOBYTELEVEL = 0; /* Some cases where we cannot do byte level fast-search: ALWAYS 0 if !ByteLevelIndex */
int OPTIMIZEBYTELEVEL = 0; /* Some cases where we don't want to do byte level search since number of files is small */
int GCONSTANT = 0; /* should pattern be taken as-is or parsed? */
int GLIMITOUTPUT = 0; /* max no. of output lines: 0=>infinity=default=nolimit */
int GLIMITTOTALFILE = 0; /* max no. of files to match: 0=>infinity=default=nolimit */
int GLIMITPERFILE = 0; /* not used in glimpse */
int GBESTMATCH = 0; /* Should I change -B to -# where # = no. of errors? */
int GRECURSIVE = 0;
int GNOPROMPT = 0;
int GBYTECOUNT = 0;
int GPRINTFILENUMBER = 0;
int GPRINTFILETIME = 0;
int GOUTTAIL = 0;
int GFILENAMEONLY = 0; /* how to do it if it is an and expression in structured queries */
int GNOFILENAME=0;
int GPRINTNONEXISTENTFILE = 0; /* if filename is not there in index, then at least let user know its name */
int MATCHFILE = 0;
int PRINTATTR = 0;
int PRINTINDEXLINE = 0;
int Pat_as_is=0;
int Only_first=0; /* Do index search only */
int PRINTAPPXFILEMATCH=0; /* Print places in file where match occurs: useful with -b only to analyse the index */
int GCOUNT=0; /* print number of matches rather than actual matches: used only when PRINTAPPX = 1 */
int HINTSFROMUSER=0; /* The user gives the hints about where we should search (result of adding -EQNgy) */
int WHOLEFILESCOPE=0; /* used only when foundattr is NOT set: otherwise, scope is whole file anyway */
int foundattr=0; /* set in split.c -- != 0 only when StructuredIndex AND query is structured */
int foundnot=0; /* set in split.c -- != 0 only when the not operator (~) is present in the pattern */
int FILENAMESINFILE=0; /* whether the user is providing an explicit list of filenames to be searched for pattern (if absent, then means all files) */
int BITFIELDFILE=0; /* Based on contribution From ada@mail2.umu.se Fri Jul 12 01:56 MST 1996; Christer Holgersson, Sen. SysNet Mgr, Umea University/SUNET, Sweden */
int BITFIELDOFFSET=0;
int BITFIELDLENGTH=0;
int BITFIELDENDIAN=0;
int GNumDays = 0; /* whether the user wants files modified within these many days before creating the index: only >0 makes sense */
/* structured queries */
CHAR ***attr_vals; /* matrix of char pointers: row=max #of attributes, col=max possible values */
CHAR **attr_found; /* did the expression corr. to each value in attr_vals match? */
ParseTree *GParse; /* what kind of expression corr. to attr are we looking for */
/* arbitrary booleans */
ParseTree terminals[MAXNUM_PAT]; /* parse tree's terminal node pointers pt. to elements of this array; also used outside */
char matched_terminals[MAXNUM_PAT]; /* ...[i] is 1 if i'th terminal matched: used in filter_output and eval_tree */
int num_terminals; /* number of terminal patterns */
int ComplexBoolean=0; /* 1 if we need to use parse trees and the eval function */
/* index search */
CHAR *pat_list[MAXNUM_PAT]; /* complete words within global pattern */
int pat_lens[MAXNUM_PAT]; /* their lengths */
int pat_attr[MAXNUM_PAT]; /* set of attributes */
int is_mgrep_pat[MAXNUM_PAT];
int mgrep_pat_index[MAXNUM_PAT];
int num_mgrep_pat;
CHAR pat_buf[(MAXNUM_PAT + 2)*MAXPAT];
int pat_ptr = 0;
extern char INDEX_DIR[MAX_LINE_LEN];
char *TEMP_DIR = NULL; /* directory to store glimpse temporary files, usually /tmp unless -T is specified */
char indexnumberbuf[256]; /* to read in first few lines of the index */
char *index_argv[MAX_ARGS];
int index_argc = 0;
int bestmatcherrors=0; /* set during index search, used later on */
int patindex;
int patbufpos = -1;
char tempfile[MAX_NAME_LEN];
char *filenames_file = NULL;
char *bitfield_file = NULL;
/* agrep search */
char *agrep_argv[MAX_ARGS];
int agrep_argc = 0;
CHAR *FileOpt; /* the option list after -F */
int fileopt_length;
CHAR GPattern[MAXPAT];
int GM;
CHAR APattern[MAXPAT];
int AM;
CHAR GD_pattern[MAXPAT];
int GD_length;
CHAR **GTextfiles;
CHAR **GTextfilenames;
int *GFileIndex;
int GNumfiles;
int GNumpartitions;
CHAR GProgname[MAXNAME];
/* persistent file descriptors */
#if BG_DEBUG
FILE *debug; /* file descriptor for debugging output */
#endif /*BG_DEBUG*/
FILE *timesfp = NULL;
FILE *timesindexfp = NULL;
FILE *indexfp = NULL; /* glimpse index */
FILE *partfp = NULL; /* glimpse partitions */
FILE *minifp = NULL; /* glimpse turbo */
FILE *nullfp = NULL; /* to discard output: agrep -s doesn't work properly */
int svstdin = 0, svstdout = 1, svstderr = 2;
static int one = 1; /* to set socket option so that glimpseserver releases socket after death */
/* Index manipulation */
struct offsets **src_offset_table;
struct offsets **multi_dest_offset_table[MAXNUM_PAT];
unsigned int *multi_dest_index_set[MAXNUM_PAT];
extern free_list();
struct stat index_stat_buf, file_stat_buf;
int timesindexsize = 0;
int last_Y_filenumber = 0;
/* Direct agrep access for bytelevel-indices */
extern int COUNT, INVERSE, TCOMPRESSED, NOFILENAME, POST_FILTER, OUTTAIL, BYTECOUNT, SILENT, NEW_FILE,
LIMITOUTPUT, LIMITPERFILE, LIMITTOTALFILE, PRINTRECORD, DELIMITER, SILENT, FILENAMEONLY, num_of_matched, prev_num_of_matched, FILEOUT;
CHAR matched_region[MAX_REGION_LIMIT*2 + MAXPATT*2];
int RegionLimit=DEFAULT_REGION_LIMIT;
/* Returns number of matched records/lines. Uses agrep's options to output stuff nicely; never called with RecordLevelIndex set */
int
glimpse_search(AM, APattern, GD_length, GD_pattern, realfilename, filename, fileindex, src_offset_table, outfp)
int AM;
unsigned char APattern[];
int GD_length;
unsigned char GD_pattern[];
char *realfilename;
char *filename;
int fileindex;
struct offsets *src_offset_table[];
FILE *outfp;
{
FILE *infp;
char sig[SIGNATURE_LEN];
struct offsets **p1, *tp1;
CHAR *text, *curtextend, *curtextbegin, c;
int times;
int num, ret, totalret = 0;
int prevoffset = 0, begininterval = 0, endinterval = -1;
CHAR *beginregionptr = 0, *endregionptr = 0;
int beginpage = 0, endpage = -1;
static int MAXTIMES, MAXPGTIMES, pagesize;
static int first_time = 1;
/*
* If can't open file for read, quit
* For each offset for that file:
* seek to that point
* go back until delimiter, go forward until delimiter, output it: MAX_REGION_LIMIT is 16K on either side.
* read in units of RegionLimit
* before outputting matched record, use options to put prefixes (or use memagrep which does everything?)
* Algorithm changed: don't read same page in twice.
*/
if (first_time) {
pagesize = DISKBLOCKSIZE;
MAXTIMES = ((MAX_REGION_LIMIT / RegionLimit) > 1) ? (MAX_REGION_LIMIT / RegionLimit) : 1;
MAXPGTIMES = ((MAX_REGION_LIMIT / pagesize) > 1) ? (MAX_REGION_LIMIT / pagesize) : 1;
first_time = 0;
}
/* Safety: must end/begin with delim */
memcpy(matched_region, GD_pattern, GD_length);
memcpy(matched_region+MAXPATT+2*MAX_REGION_LIMIT, GD_pattern, GD_length);
text = &matched_region[MAX_REGION_LIMIT+MAXPATT];
if ((infp = my_fopen(filename, "r")) == NULL) return 0;
NEW_FILE = ON;
#if 0
/* Cannot search in .CZ files since offset computations will be incorrect */
TCOMPRESSED = ON;
if (!tuncompressible_filename(file_list[i], strlen(file_list[i]))) TCOMPRESSED = OFF;
num_read = fread(sig, 1, SIGNATURE_LEN, infp);
if ((TCOMPRESSED == ON) && tuncompressible(sig, num_read)) {
EASYSEARCH = sig[SIGNATURE_LEN-1];
if (!EASYSEARCH) {
fprintf(stderr, "not compressed for easy-search: can miss some matches in: %s\n", CurrentFileName); /* not filename!!! */
}
}
else TCOMPRESSED = OFF;
#endif /*0*/
p1 = &src_offset_table[fileindex];
while (*p1 != NULL) {
if ( (begininterval <= (*p1)->offset) && (endinterval > (*p1)->offset) ) { /* already covered this area */
#if DEBUG
printf("ignoring %d in [%d,%d]\n", (*p1)->offset, begininterval, endinterval);
#endif /*DEBUG*/
tp1 = *p1;
*p1 = (*p1)->next;
my_free(tp1, sizeof(struct offsets));
continue;
}
TCOMPRESSED = OFF;
#if 1
if ( (beginpage <= (*p1)->offset) && (endpage >= (*p1)->offset) && (text + ((*p1)->offset - prevoffset) + GD_length < endregionptr)) {
/* beginregionptr = curtextend - GD_length; /* prevent next curtextbegin to go behind previous curtextend (!) */
text += ((*p1)->offset - prevoffset);
prevoffset = (*p1)->offset;
if (!((curtextend = forward_delimiter(text, endregionptr, GD_pattern, GD_length, 1)) < endregionptr))
goto fresh_read;
if (!((curtextbegin = backward_delimiter(text, beginregionptr, GD_pattern, GD_length, 0)) > beginregionptr))
goto fresh_read;
}
else { /* NOT within an area already read: must read another page: if record overlapps page, might read page twice: no time to fix */
fresh_read:
prevoffset = (*p1)->offset;
text = &matched_region[MAX_REGION_LIMIT+MAXPATT]; /* middle: points to occurrence of pattern */
endpage = beginpage = ((*p1)->offset / pagesize) * pagesize;
/* endpage = (((*p1)->offset + pagesize) / pagesize) * pagesize */
endregionptr = beginregionptr = text - ((*p1)->offset - beginpage); /* overlay physical place starting from this logical point */
/* endregionptr = text + (endpage - (*p1)->offset); */
curtextbegin = curtextend = text;
times = 0;
while (times < MAXPGTIMES) {
fseek(infp, endpage, 0);
num = (&matched_region[MAX_REGION_LIMIT*2+MAXPATT] - endregionptr < pagesize) ? (&matched_region[MAX_REGION_LIMIT*2+MAXPATT] - endregionptr) : pagesize;
if ((num = fread(endregionptr, 1, num, infp)) <= 0) break;
endpage += num;
endregionptr += num;
if (endregionptr <= text) {
curtextend = text; /* error in value of offset: file was modified and offsets no longer true: your RISK! */
break;
}
if (((curtextend = forward_delimiter(text, endregionptr, GD_pattern, GD_length, 1)) < endregionptr) ||
(endregionptr >= &matched_region[MAX_REGION_LIMIT*2 + MAXPATT])) break;
times ++;
}
times = 0;
while (times < MAXPGTIMES) { /* I have already read the initial page since endpage is beginpage initially */
if ((curtextbegin = backward_delimiter(text, beginregionptr, GD_pattern, GD_length, 0)) > beginregionptr) break;
if (beginpage > 0) {
if (beginregionptr - pagesize < &matched_region[MAXPATT]) {
if ((num = beginregionptr - &matched_region[MAXPATT]) <= 0) break;
}
else num = pagesize;
beginpage -= num;
beginregionptr -= num;
}
else break;
times ++;
fseek(infp, beginpage, 0);
fread(beginregionptr, 1, num, infp);
}
}
#else /*1*/
/* Find forward delimiter (including delimiter) */
times = 0;
fseek(infp, (*p1)->offset, 0);
while (times < MAXTIMES) {
if ((num = fread(text+RegionLimit*times, 1, RegionLimit, infp)) > 0)
curtextend = forward_delimiter(text, text+RegionLimit*times+num, GD_pattern, GD_length, 1);
if ((curtextend < text+RegionLimit*times+num) || (num < RegionLimit)) break;
times ++;
}
/* Find backward delimiter (including delimiter) */
times = 0;
while (times < MAXTIMES) {
num = ((*p1)->offset - RegionLimit*(times+1)) > 0 ? ((*p1)->offset - RegionLimit*(times+1)) : 0;
fseek(infp, num, 0);
if (num > 0) {
fread(text-RegionLimit*(times+1), 1, RegionLimit, infp);
curtextbegin = backward_delimiter(text, text-RegionLimit*(times+1), GD_pattern, GD_length, 0);
}
else {
fread(text-RegionLimit*times-(*p1)->offset, 1, (*p1)->offset, infp);
curtextbegin = backward_delimiter(text, text-RegionLimit*times-(*p1)->offset, GD_pattern, GD_length, 0);
}
if ((num <= 0) || (curtextbegin > text-RegionLimit*(times+1))) break;
times ++;
}
#endif /*1*/
/* set interval and delete the entry */
begininterval = (*p1)->offset - (text - curtextbegin);
endinterval = (*p1)->offset + (curtextend - text);
if (strncmp(curtextbegin, GD_pattern, GD_length)) {
/* always pass enclosing delimiters to agrep; since we have seen text before curtextbegin + we have space, we can overwrite */
memcpy(curtextbegin - GD_length, GD_pattern, GD_length);
curtextbegin -= GD_length;
}
#if DEBUG
c = *curtextend;
*curtextend = '\0';
printf("%s [%d < %d < %d], text = %d: %s\n", CurrentFileName, begininterval, (*p1)->offset, endinterval, text, curtextbegin);
*curtextend = c;
#endif /*DEBUG*/
tp1 = *p1;
*p1 = (*p1)->next;
my_free(tp1, sizeof(struct offsets));
if (curtextend <= curtextbegin) continue; /* error in offsets/delims */
/*
* Don't call memagrep since that is heavy weight. Call exec
* directly after doing agrep_search()'s preprocessing here.
* PS: can add agrep variable not to do delim search if called from here
* since that prevents unnecessarily scanning the buffer for the 2nd time.
*/
CurrentByteOffset = begininterval+1;
SetCurrentByteOffset = 1;
first_search = 1;
if (first_search) {
if ((ret = memagrep_search(AM, APattern, curtextend-curtextbegin, curtextbegin, 0, outfp)) > 0)
totalret ++; /* += ret */
else if ((ret < 0) && (errno == AGREP_ERROR)) {
fclose(infp);
return -1;
}
first_search = 0;
}
else { /* All agrep globals are properly set: has a bug because agrep's globals aren't properly reinitialized without agrep_search :-( */
agrep_finalfp = (FILE *)outfp;
agrep_outlen = 0;
agrep_outbuffer = NULL;
agrep_outpointer = 0;
execfd = agrep_initialfd = -1;
agrep_inbuffer = curtextbegin;
agrep_inlen = curtextend - curtextbegin;
agrep_inpointer = 0;
if ((ret = exec(-1, NULL)) > 0)
totalret ++; /* += ret; */
else if ((ret < 0) && (errno == AGREP_ERROR)) {
fclose(infp);
return -1;
}
}
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) break; /* done */
if ((totalret > 0) && FILENAMEONLY) break;
} /* while *p1 != NULL */
SetCurrentByteOffset = 0;
fclose(infp);
if (totalret > 0) { /* dirty solution: must handle part of agrep here */
if (COUNT && !FILEOUT && !SILENT) {
if(!NOFILENAME) fprintf(outfp, "%s: %d\n", CurrentFileName, totalret);
else fprintf(outfp, "%d\n", totalret);
}
else if (FILEOUT) {
file_out(realfilename);
}
}
return totalret;
}
/* Sets lastfilenumber that needs to be searched: rest must be discarded */
int
process_Y_option(num_files, num_days, fp)
int num_files, num_days;
FILE *fp;
{
CHAR arrayend[4];
last_Y_filenumber = 0;
if ((num_days <= 0) || (fp == NULL) || (timesindexsize <= 0)) return 0;
last_Y_filenumber = num_files;
if (num_days * sizeof(int) >= timesindexsize) return 0; /* everything will be within so many days */
if (fseek(fp, num_days*sizeof(int), 0) == -1) return -1;
fread(arrayend, 1, 4, fp);
if ((last_Y_filenumber = (arrayend[0] << 24) | (arrayend[1] << 16) | (arrayend[2] << 8) | arrayend[3]) > num_files) last_Y_filenumber = num_files;
if (last_Y_filenumber == 0) {
last_Y_filenumber = 1;
printf("Warning: no files modified in the last %d days were found in the index.\nSearching only the most recently modified file...\n", num_days);
}
return 0;
}
read_index(indexdir)
char indexdir[MAXNAME];
{
char *home;
char s[MAXNAME];
int ret;
if (indexdir[0] == '\0') {
if ((home = (char *)getenv("HOME")) == NULL) {
getcwd(indexdir, MAXNAME-1);
fprintf(stderr, "using working-directory '%s' to locate index\n", indexdir);
}
else strncpy(indexdir, home, MAXNAME);
}
ret = chdir(indexdir);
if (getcwd(INDEX_DIR, MAXNAME-1) == NULL) strcpy(INDEX_DIR, indexdir);
if (ret < 0) {
fprintf(stderr, "using working-directory '%s' to locate index\n", INDEX_DIR);
}
sprintf(s, "%s", INDEX_FILE);
indexfp = fopen(s, "r");
if(indexfp == NULL) {
fprintf(stderr, "can't open glimpse index-file %s/%s\n", INDEX_DIR, INDEX_FILE);
fprintf(stderr, "(use -H to give an index-directory or run 'glimpseindex' to make an index)\n");
return -1;
}
if (stat(s, &index_stat_buf) == -1) {
fprintf(stderr, "can't stat %s/%s\n", INDEX_DIR, s);
fclose(indexfp);
return -1;
}
sprintf(s, "%s", P_TABLE);
partfp = fopen(s, "r");
if(partfp == NULL) {
fprintf(stderr, "can't open glimpse partition-table %s/%s\n", INDEX_DIR, P_TABLE);
fprintf(stderr, "(use -H to specify an index-directory or run glimpseindex to make an index)\n");
fclose(indexfp);
return -1;
}
sprintf(s, "%s", DEF_TIME_FILE);
timesfp = fopen(s, "r");
sprintf(s, "%s.index", DEF_TIME_FILE);
timesindexfp = fopen(s, "r");
if (timesindexfp != NULL) {
struct stat st;
fstat(fileno(timesindexfp), &st);
timesindexsize = st.st_size;
}
/* Get options */
#if BG_DEBUG
debug = fopen(DEBUG_FILE, "w+");
if(debug == NULL) {
fprintf(stderr, "can't open file %s/%s, errno=%d\n", INDEX_DIR, DEBUG_FILE, errno);
return(-1);
}
#endif /*BG_DEBUG*/
fgets(indexnumberbuf, 256, indexfp);
if(strstr(indexnumberbuf, "1234567890")) IndexNumber = ON;
else IndexNumber = OFF;
fscanf(indexfp, "%%%d\n", &OneFilePerBlock);
if (OneFilePerBlock < 0) {
ByteLevelIndex = ON;
OneFilePerBlock = -OneFilePerBlock;
}
else if (OneFilePerBlock == 0) {
GNumpartitions = get_table(P_TABLE, p_table, MAX_PARTITION, 0);
}
fscanf(indexfp, "%%%d%s\n", &StructuredIndex, old_rdelim);
/* Set WHOLEFILESCOPE for do-it-yourself request processing at client */
WHOLEFILESCOPE = 1;
if (StructuredIndex <= 0) {
if (StructuredIndex == -2) {
RecordLevelIndex = 1;
strcpy(rdelim, old_rdelim);
rdelim_len = strlen(rdelim);
preprocess_delimiter(rdelim, rdelim_len, rdelim, &rdelim_len);
}
WHOLEFILESCOPE = 0;
StructuredIndex = 0;
PRINTATTR = 0; /* doesn't make sense: must not go into filter_output */
}
else if (-1 == (StructuredIndex = attr_load_names(ATTRIBUTE_FILE))) {
fprintf(stderr, "error in reading attribute file %s/%s\n", INDEX_DIR, ATTRIBUTE_FILE);
return(-1);
}
#if BG_DEBUG
fprintf(debug, "buf = %s OneFilePerBlock=%d StructuredIndex=%d\n", indexnumberbuf, OneFilePerBlock, StructuredIndex);
#endif /*BG_DEBUG*/
sprintf(s, "%s", MINI_FILE);
minifp = fopen(s, "r");
/* if (minifp==NULL && OneFilePerBlock) fprintf(stderr, "Can't open for reading: %s/%s --- cannot do very fast search\n", INDEX_DIR, MINI_FILE); */
if (OneFilePerBlock && glimpse_isserver && (minifp != NULL)) read_mini(indexfp, minifp);
read_filenames();
/* Once IndexNumber info is available */
set_indexable_char(indexable_char);
set_indexable_char(test_indexable_char);
set_special_char(indexable_char);
return 0;
}
#define CLEANUP \
{\
int q, k;\
if (timesfp != NULL) fclose(timesfp);\
if (timesindexfp != NULL) fclose(timesindexfp);\
if (indexfp != NULL) fclose(indexfp);\
if (partfp != NULL) fclose(partfp);\
if (minifp != NULL) fclose(minifp);\
if (nullfp != NULL) fclose(nullfp);\
indexfp = partfp = minifp = nullfp = NULL;\
if (ByteLevelIndex) {\
if (src_offset_table != NULL) for (k=0; k<OneFilePerBlock; k++) {\
free_list(&src_offset_table[k]);\
}\
for (q=0; q<MAXNUM_PAT; q++) {\
if (multi_dest_offset_table[q] != NULL) for (k=0; k<OneFilePerBlock; k++) {\
free_list(&multi_dest_offset_table[q][k]);\
}\
}\
}\
if (StructuredIndex) {\
attr_free_table();\
}\
destroy_filename_hashtable();\
my_free(SERV_HOST, MAXNAME);\
}
/* Called whenever we get SIGUSR2/SIGHUP (at the end of process_query()) */
reinitialize_server(argc, argv)
int argc;
char **argv;
{
int i, fd;
CLEANUP;
#if 0
init_filename_hashtable();
region_initialize();
indexfp = partfp = minifp = nullfp = NULL;
if ((nullfp = fopen("/dev/null", "w")) == NULL) {
return(-1);
}
src_offset_table = NULL;
for (i=0; i<MAXNUM_PAT; i++) multi_dest_offset_table[i] = NULL;
if (-1 == read_index(INDEX_DIR)) return(-1);
#if 0
#ifndef LOCK_UN
#define LOCK_UN 8
#endif
if ((fd = open(INDEX_DIR, O_RDONLY)) == -1) return -1;
flock(fd, LOCK_UN);
close(fd);
#endif
return 0;
#else
return execve(argv[0], argv, environ);
#endif
}
/* MUST CARE IF PIPE/SOCKET IS BROKEN! ALSO SIGUSR1 (hardy@cs.colorado.edu) => QUIT CURRENT REQUEST. */
int ignore_signal[32] = { 0,
0, 0, 1, 1, 1, 1, 1, 1, /* all the tracing stuff: since default action is to dump core */
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0 }; /* resource lost: since default action is to dump core */
/* S.t. sockets don't persist: they sometimes have a bad habit of doing so */
void
cleanup()
{
int i;
/* ^C in the middle of a client call */
if (svstderr != 2) {
close(2);
dup(svstderr);
}
fprintf(stderr, "server cleaning up...\n");
CLEANUP;
for (i=0; i<64; i++) close(i);
exit(3);
}
void reinitialize(s)
int s;
{
/* To force main-while loop call reinitialize_server() after do_select() */
glimpse_reinitialize = 1;
#ifdef __svr4__
/* Solaris 2.3 insists that you reset the signal handler */
(void)signal(s, reinitialize);
#endif
}
#define QUITREQUESTMSG "glimpseserver: aborting request...\n"
/* S.t. one request doesn't keep server occupied too long, when client already quits */
void quitrequest(s)
int s;
{
/*
* Don't write onto stderr, since 2 is duped to sockfd => can cause recursive signal!
* Also, don't print error message more than once for quitting one request. The
* server receives signals for EVERY write it attempts when it finds a match: I could
* not find a way to prevent it, but agrep/bitap.c/fill_buf() was fixed to limit it.
* -- bg on 16th Feb 1995
*/
if (!glimpse_clientdied && (s != SIGUSR1)) /* USR1 is a "friendly" cleanup message */
write(svstderr, QUITREQUESTMSG, strlen(QUITREQUESTMSG));
glimpse_clientdied = 1;
#ifdef __svr4__
/* Solaris 2.3 insists that you reset the signal handler */
(void)signal(s, quitrequest);
#endif
}
/* The client receives this signal when an output/input pipe is broken, etc. It simply exits from the current request */
void exitrequest()
{
glimpse_clientdied = 1;
}
main(argc, argv)
int argc;
char *argv[];
{
int ret, tried = 0;
char indexdir[MAXNAME];
char **oldargv = argv;
int oldargc = argc;
#if CLIENTSERVER
int sockfd, newsockfd, clilen, len, clpid;
int clout;
#if USE_UNIXDOMAIN
struct sockaddr_un cli_addr, serv_addr;
#else /*USE_UNIXDOMAIN*/
struct sockaddr_in cli_addr, serv_addr;
struct hostent *hp;
#endif /*USE_UNIXDOMAIN*/
int cli_len;
int clargc;
char **clargv;
int clstdin, clstdout, clstderr;
int i;
char array[4];
char *p, c;
#endif /*CLIENTSERVER*/
int quitwhile;
#if ISO_CHAR_SET
setlocale(LC_ALL,""); /* support for 8bit character set: ew@senate.be, Henrik.Martin@eua.ericsson.se */
#endif
#if CLIENTSERVER && ISSERVER
glimpse_isserver = 1; /* I am the server */
#else /*CLIENTSERVER && ISSERVER*/
if (argc <= 1) {
usage(); /* Client nees at least 1 argument */
exit(1);
}
#endif /*CLIENTSERVER && ISSERVER*/
#define RETURNMAIN(val)\
{\
CLEANUP;\
if (val < 0) exit (1);\
else exit (0);\
}
SERV_HOST = (CHAR *)my_malloc(MAXNAME);
#if !SYSCALLTESTING
/* once-only initialization */
init_filename_hashtable();
src_offset_table = NULL;
for (i=0; i<MAXNUM_PAT; i++) multi_dest_offset_table[i] = NULL;
#endif
gethostname(SERV_HOST, MAXNAME - 2);
SERV_PORT = DEF_SERV_PORT;
srand(getpid());
umask(077);
strcpy(&GProgname[0], argv[0]);
#if !SYSCALLTESTING
region_initialize();
#endif
indexfp = partfp = minifp = nullfp = NULL;
if ((nullfp = fopen("/dev/null", "w")) == NULL) {
fprintf(stderr, "%s: cannot open for writing: /dev/null, errno=%d\n", argv[0], errno);
RETURNMAIN(-1);
}
InterpretSpecial = ON;
GMAX_WORD_SIZE = MAXPAT;
#if CLIENTSERVER
#if !ISSERVER
/* Install signal handlers so that glimpse doesn't continue to run when pipes get broken, etc. */
if (((void (*)())-1 == signal(SIGPIPE, exitrequest))
#ifndef SCO
|| ((void (*)())-1 == signal(SIGURG, exitrequest))
#endif
)
{
/* Check for return values here since they ensure reliability */
fprintf(stderr, "glimpse: Unable to install signal-handlers.\n");
RETURNMAIN(-1);
}
/* Check if client has too many arguments: then it is surely running as agrep since I have < half those options! */
if (argc > MAX_ARGS) goto doityourself;
#endif /*!ISSERVER*/
#if !SYSCALLTESTING
while((--argc > 0) && (*++argv)[0] == '-' ) {
p = argv[0] + 1; /* ptr to first character after '-' */
c = *(argv[0]+1);
quitwhile = OFF;
while (!quitwhile && (*p != '\0')) {
c = *p;
switch(c) {
/* Look for -H option at server (only one that makes sense); if client has a -H, then it goes to doityourself */
case 'H' :
if (*(p + 1) == '\0') {/* space after - option */
if (argc <= 1) {
fprintf(stderr, "%s: a directory name must follow the -H option\n", GProgname);
RETURNMAIN(usageS());
}
argv ++;
strcpy(indexdir, argv[0]);
argc --;
}
else {
strcpy(indexdir, p+1);
}
quitwhile = ON;
break;
/* Recognized by both client and server */
case 'J' :
if (*(p + 1) == '\0') {/* space after - option */
if (argc <= 1) {
fprintf(stderr, "%s: the server host name must follow the -J option\n", GProgname);
#if ISSERVER
RETURNMAIN(usageS());
#else /*ISSERVER*/
RETURNMAIN(usage());
#endif /*ISSERVER*/
}
argv ++;
strcpy(SERV_HOST, argv[0]);
argc --;
}
else {
strcpy(SERV_HOST, p+1);
}
quitwhile = ON;
break;
/* Recognized by both client and server */
case 'K' :
if (*(p + 1) == '\0') {/* space after - option */
if (argc <= 1) {
fprintf(stderr, "%s: the server port must follow the -C option\n", GProgname);
#if ISSERVER
RETURNMAIN(usageS());
#else /*ISSERVER*/
RETURNMAIN(usage());
#endif /*ISSERVER*/
}
argv ++;
SERV_PORT = atoi(argv[0]);
argc --;
}
else {
SERV_PORT = atoi(p+1);
}
if ((SERV_PORT < MIN_SERV_PORT) || (SERV_PORT > MAX_SERV_PORT)) {
fprintf(stderr, "Bad server port %d: must be in [%d, %d]: using default %d\n",
SERV_PORT, MIN_SERV_PORT, MAX_SERV_PORT, DEF_SERV_PORT);
SERV_PORT = DEF_SERV_PORT;
}
quitwhile = ON;
break;
#if ISSERVER
#if SFS_COMPAT
case 'R' :
RemoteFiles = ON;
break;
case 'Z' :
/* No op */
break;
#endif
/* server cannot recognize any other option */
default :
fprintf(stderr, "%s: server cannot recognize option: '%s'\n", GProgname, p);
RETURNMAIN(usageS());
#else /*ISSERVER*/
/* These have 1 argument each, so must do quitwhile */
case 'd' :
case 'e' :
case 'f' :
case 'k' :
case 'D' :
case 'F' :
case 'I' :
case 'L' :
case 'R' :
case 'S' :
case 'T' :
case 'Y' :
case 'p' :
if (argv[0][2] == '\0') {/* space after - option */
if(argc <= 1) {
fprintf(stderr, "%s: the '-%c' option must have an argument\n", GProgname, c);
RETURNMAIN(usage());
}
argv++;
argc--;
}
quitwhile = ON;
break;
/* These are illegal */
case 'm' :
case 'v' :
fprintf(stderr, "%s: illegal option: '-%c'\n", GProgname, c);
RETURNMAIN(usage());
/* They can't be patterns and filenames since they start with a -, these don't have arguments */
case '!' :
case 'a' :
case 'b' :
case 'c' :
case 'h' :
case 'i' :
case 'j' :
case 'l' :
case 'n' :
case 'o' :
case 'q' :
case 'r' :
case 's' :
case 't' :
case 'u' :
case 'g' :
case 'w' :
case 'x' :
case 'y' :
case 'z' :
case 'A' :
case 'B' :
case 'E' :
case 'G' :
case 'M' :
case 'N' :
case 'O' :
case 'P' :
case 'Q' :
case 'U' :
case 'W' :
case 'X' :
case 'Z' :
break;
case 'C':
CONTACT_SERVER = 1;
break;
case 'V' :
printf("\nThis is glimpse version %s, %s.\n\n", GLIMPSE_VERSION, GLIMPSE_DATE);
RETURNMAIN(0);
default :
if (isdigit(c)) quitwhile = ON;
else {
fprintf(stderr, "%s: illegal option: '-%c'\n", GProgname, c);
RETURNMAIN(usage());
}
break;
#endif /*ISSERVER*/
} /* switch(c) */
p ++;
}
}
#else
CONTACT_SERVER = 1;
argc=0;
#endif
#if !ISSERVER
/* Next arg must be the pattern: Check if the user wants to run the client as agrep, or doesn't want to contact the server */
if ((argc > 1) || (!CONTACT_SERVER)) goto doityourself;
#endif /*!ISSERVER*/
argv = oldargv;
argc = oldargc;
#endif /*CLIENTSERVER*/
#if ISSERVER && CLIENTSERVER
if (-1 == read_index(indexdir)) RETURNMAIN(ret);
/* Install signal handlers so that glimpseserver doesn't continue to run when sockets get broken, etc. */
for (i=0; i<32; i++)
if (ignore_signal[i]) signal(i, SIG_IGN);
signal(SIGHUP, cleanup);
signal(SIGINT, cleanup);
if (((void (*)())-1 == signal(SIGPIPE, quitrequest)) ||
((void (*)())-1 == signal(SIGUSR1, quitrequest)) ||
#ifndef SCO
((void (*)())-1 == signal(SIGURG, quitrequest)) ||
#endif
((void (*)())-1 == signal(SIGUSR2, reinitialize)) ||
((void (*)())-1 == signal(SIGHUP, reinitialize))) {
/* Check for return values here since they ensure reliability */
fprintf(stderr, "glimpseserver: Unable to install signal-handlers.\n");
RETURNMAIN(-1);
}
#if USE_UNIXDOMAIN
if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
fprintf(stderr, "server cannot open socket for communication.\n");
RETURNMAIN(-1);
}
unlink("/tmp/.glimpse_server");
memset((char *)&serv_addr, '\0', sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path, "/tmp/.glimpse_server"); /* < 108 ! */
len = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);
#else /*USE_UNIXDOMAIN*/
if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
perror("glimpseserver: Cannot create socket");
RETURNMAIN(-1);
}
memset((char *)&serv_addr, '\0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERV_PORT);
#if 0
/* use host-names not internet style d.d.d.d notation */
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
#else
/*
* We only want to accept connections from glimpse clients
* on the SERV_HOST, do not use INADDR_ANY!
*/
if ((hp = gethostbyname(SERV_HOST)) == NULL) {
perror("glimpseserver: Cannot resolve host");
RETURNMAIN(-1);
}
memcpy((caddr_t)&serv_addr.sin_addr, hp->h_addr, hp->h_length);
#endif /*0*/
len = sizeof(serv_addr);
#endif /*USE_UNIXDOMAIN*/
/* test code for glimpse server, get it to realse socket when it dies: contribution by Sheldon Smoker <sheldon@thunder.tig.com> */
if((setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,(char *)&one,sizeof(one))) == -1) {
fprintf(stderr,"glimpseserver: could not set socket option\n");
perror("setsockopt");
exit(1);
}
/* end test code */
if (bind(sockfd, (struct sockaddr *)&serv_addr, len) < 0) {
perror("glimpseserver: Cannot bind to socket");
RETURNMAIN(-1);
}
listen(sockfd, SERVER_QUEUE_SIZE);
printf("glimpseserver: On-line (pid = %d, port = %d) waiting for request...\n", getpid(), SERV_PORT);
fflush(stdout); /* must fflush to print on server stdout */
while (1) {
/*
* Spin until sockfd is ready to do a non-blocking accept(2).
* We only wait for 15 seconds, because SunOS may
* swap us out if we block for 20 seconds or more.
* -- Courtesy: Darren Hardy, hardy@cs.colorado.edu
*/
if ((ret = do_select(sockfd, 15)) == 0) {
if ((errno == EINTR) && glimpse_reinitialize) {
glimpse_reinitialize = 0;
CLEANUP;
close(sockfd);
sleep(IC_PORTRELEASE);
reinitialize_server(oldargc, oldargv);
}
continue;
}
else if (ret != 1) continue;
/* get parameters */
ret = 0;
clargc = 0;
clargv = NULL;
cli_len = sizeof(cli_addr);
if ((newsockfd = accept(sockfd, &cli_addr, &cli_len)) < 0) continue;
if (getreq(newsockfd, glimpse_reqbuf, &clstdin, &clstdout, &clstderr, &clargc, &clargv, &clpid) < 0) {
ret = -1;
#if DEBUG
printf("getreq errno: %d\n", errno);
#endif /*DEBUG*/
goto end_process;
}
#if DEBUG
printf("server processing request on %x\n", newsockfd);
#endif /*DEBUG*/
/*
* Server doesn't wait for response, no point using
svstdin = dup(0);
close(0);
dup(clstdin);
close(clstdin);
*/
/*
* This is wrong since clstderr == clstdout!
svstdout = dup(1);
close(1);
dup(clstdout);
close(clstdout);
svstderr = dup(2);
close(2);
dup(clstderr);
close(clstderr);
*/
svstdout = dup(1);
svstderr = dup(2);
close(1);
close(2);
dup(clstdout);
dup(clstderr);
close(clstdout);
close(clstderr);
/*
* IMPORTANT: Unbuffered I/O to the client!
* Done for Harvest since partial results might be
* needed and fflush will not flush partial results
* to the client if we type ^C and kill it: it puts
* them into /dev/null. This way, output is unbuffered
* and the client sees at least some results if killed.
*/
setbuf(stdout, NULL);
setbuf(stderr, NULL);
glimpse_call = 0;
glimpse_clientdied = 0;
ret = process_query(clargc, clargv, newsockfd);
/*
* Server doesn't wait for response, no point using
close(0);
dup(svstdin);
close(svstdin);
svstdin = 0;
*/
if (glimpse_clientdied) {
/*
* This code is *ONLY* used as a safety net now.
* The old problem was that users would see portions
* of previous (and usually) unrelated queries!
* glimpseserver now uses unbuffered I/O to the
* client so all previous fwrite's to now are
* gone. But since this is such a nasty problem
* we flush stdout to /dev/null just in case.
*/
clout = open("/dev/null", O_WRONLY);
close(1);
dup(clout);
close(clout);
fflush(stdout);
}
/* Restore svstdout and svstdout to stdout/stderr */
close(1);
dup(svstdout);
close(svstdout);
svstdout = 1;
close(2);
dup(svstderr);
close(svstderr);
svstderr = 2;
end_process:
#if USE_MSGHDR
/* send reply and cleanup */
array[0] = (ret & 0xff000000) >> 24;
array[1] = (ret & 0xff0000) >> 16;
array[2] = (ret & 0xff00) >> 8;
array[3] = (ret & 0xff);
writen(newsockfd, array, 4);
#endif /*USE_MSGHDR*/
#if DEBUG
write(1, "done\n", 5);
#endif /*DEBUG*/
for (i=0; i<clargc; i++)
if (clargv[i] != NULL) my_free(clargv[i], 0);
if (clargv != NULL) my_free(clargv, 0);
close(newsockfd); /* if !USE_MSGHDR, client directly reads from socket and writes onto stdout until EOF */
}
#else /*ISSERVER && CLIENTSERVER*/
#if CLIENTSERVER
trynewsocket:
#if USE_UNIXDOMAIN
if ((sockfd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
perror("socket");
goto doityourself;
}
memset((char *)&serv_addr, '\0', sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path, "/tmp/.glimpse_server"); /* < 108 ! */
len = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);
#else /*USE_UNIXDOMAIN*/
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
perror("socket");
goto doityourself;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(SERV_PORT);
#if 0
/* use host-names not internet style d.d.d.d notation */
serv_addr.sin__addr.s_addr = inet_addr(SERV_HOST);
#else /*0*/
if ((hp = gethostbyname(SERV_HOST)) == NULL) {
fprintf(stderr, "gethostbyname (%s) failed\n", SERV_HOST);
goto doityourself;
}
memcpy((caddr_t)&serv_addr.sin_addr, hp->h_addr, hp->h_length);
#endif /*0*/
len = sizeof(serv_addr);
#endif /*USE_UNIXDOMAIN*/
if (connect(sockfd, (struct sockaddr *)&serv_addr, len) < 0) {
char errbuf[4096];
sprintf(errbuf, "glimpse: Cannot contact glimpseserver: %s, port %d:", SERV_HOST, SERV_PORT);
perror(errbuf);
/* perror(SERV_HOST); */
#if DEBUG
printf("connect errno: %d\n", errno);
#endif /*DEBUG*/
close(sockfd);
if ((errno == ECONNREFUSED) && (tried < 4)) {
tried ++;
goto trynewsocket;
}
goto doityourself;
}
if (sendreq(sockfd, glimpse_reqbuf, fileno(stdin), fileno(stdout), fileno(stderr), argc, argv, getpid()) < 0) {
perror("sendreq");
#if DEBUG
printf("sendreq errno: %d\n", errno);
#endif /*DEBUG*/
close(sockfd);
goto doityourself;
}
#if USE_MSGHDR
if (readn(sockfd, array, 4) != 4) {
close(sockfd);
goto doityourself;
}
ret = (array[0] << 24) + (array[1] << 16) + (array[2] << 8) + array[3];
#else /*USE_MSGHDR*/
{
/*
* Dump everything the server writes into the socket onto
* stdout until EOF/error. Do this in a way so that *everything*
* the server sends is dumped to stdout by the client. The
* client might die suddenly via ^C or SIGTERM, but we still
* want the results.
*/
char tmpbuf[1024];
int n;
while ((n = read(sockfd, tmpbuf, 1024)) > 0) {
write(fileno(stdout), tmpbuf, n);
}
}
#endif /*USE_MSGHDR*/
close(sockfd);
RETURNMAIN(ret);
doityourself:
#if DEBUG
printf("doing it myself :-(\n");
#endif /*DEBUG*/
#endif /*CLIENTSERVER*/
setbuf(stdout, NULL); /* Unbuffered I/O to always get every result */
setbuf(stderr, NULL);
glimpse_call = 0;
glimpse_clientdied = 0;
ret = process_query(oldargc, oldargv, fileno(stdin));
RETURNMAIN(ret);
#endif /*ISSERVER && CLIENTSERVER*/
}
process_query(argc, argv, newsockfd)
int argc;
char *argv[];
int newsockfd;
{
int searchpercent;
int num_blocks;
int num_read;
int i, j;
int iii; /* Udi */
int jjj;
char c;
char *p;
int ret;
int jj;
int quitwhile;
char indexdir[MAX_LINE_LEN];
char temp_filenames_file[MAX_LINE_LEN];
char temp_bitfield_file[MAX_LINE_LEN];
char TEMP_FILE[MAX_LINE_LEN];
char temp_file[MAX_LINE_LEN];
int oldargc = argc;
char **oldargv = argv;
CHAR dummypat[MAX_PAT];
int dummylen=0;
int my_M_index, my_P_index, my_b_index, my_A_index, my_l_index = -1, my_B_index = -1;
char **outname;
int gnum_of_matched = 0;
int gprev_num_of_matched = 0;
int gfiles_matched = 0;
int foundpat = 0;
int wholefilescope=0;
int nobytelevelmustbeon=0;
long get_file_time();
if ((argc <= 0) || (argv == NULL)) {
errno = EINVAL;
return -1;
}
/*
* Macro to destroy EVERYTHING before return since we might want to make this a
* library function later on: convention is that after destroy, objects are made
* NULL throughout the source code, and are all set to NULL at initialization time.
* DO agrep_argv, index_argv and FileOpt my_malloc/my_free optimizations later.
* my_free calls have 2nd parameter = 0 if the size is not easily determinable.
*/
#define RETURN(val) \
{\
int q,k;\
\
first_search = 0;\
for (k=0; k<MAX_ARGS; k++) {\
if (agrep_argv[k] != NULL) my_free(agrep_argv[k], 0);\
if (index_argv[k] != NULL) my_free(index_argv[k], 0);\
agrep_argv[k] = index_argv[k] = NULL;\
}\
if (FileOpt != NULL) my_free(FileOpt, MAXFILEOPT);\
FileOpt = NULL;\
for (k=0; k<MAXNUM_PAT; k++) {\
if (pat_list[k] != NULL) my_free(pat_list[k], 0);\
pat_list[k] = NULL;\
}\
sprintf(tempfile, "%s/.glimpse_tmp.%d", TEMP_DIR, getpid());\
unlink(tempfile);\
sprintf(outname[0], "%s/.glimpse_apply.%d", TEMP_DIR, getpid());\
unlink(outname[0]);\
my_free(outname[0], 0);\
my_free(outname, 0);\
my_free(TEMP_DIR, MAX_LINE_LEN);\
my_free(filenames_file, MAX_LINE_LEN);\
my_free(bitfield_file, MAX_LINE_LEN);\
\
if (ByteLevelIndex) {\
if (src_offset_table != NULL) for (k=0; k<OneFilePerBlock; k++) {\
free_list(&src_offset_table[k]);\
}\
/* Don't make src_offset_table itself NULL: it will be bzero-d below if !NULL */\
for (q=0; q<MAXNUM_PAT; q++) {\
if (multi_dest_offset_table[q] != NULL) for (k=0; k<OneFilePerBlock; k++) {\
free_list(&multi_dest_offset_table[q][k]);\
}\
/* Don't make multi_dest_offset_table[q] itself NULL: it will be bzero-d below if !NULL */\
}\
}\
for (k=0; k<num_terminals;k++)\
free(terminals[k].data.leaf.value);\
if (ComplexBoolean) destroy_tree(&GParse);\
for (k=0; k<GNumfiles; k++) {\
my_free(GTextfiles[k], 0);\
GTextfiles[k] = NULL;\
}\
/* Don't free the GTextfiles buffer itself since it is allocated once in get_filename.c */\
return (val);\
}
/*
* Initialize
*/
strcpy(&GProgname[0], argv[0]);
if (argc <= 1) return(usage());
filenames_file = (char *)my_malloc(MAX_LINE_LEN);
bitfield_file = (char *)my_malloc(MAX_LINE_LEN);
TEMP_DIR = (char *)my_malloc(MAX_LINE_LEN);
strcpy(TEMP_DIR, "/tmp");
D_length = 0;
D = 0;
pattern_index = 0;
first_search = 1;
outname = (char **)my_malloc(sizeof(char *));
outname[0] = (char *)my_malloc(MAX_LINE_LEN);
NOBYTELEVEL = 0;
OPTIMIZEBYTELEVEL = 0;
GCONSTANT = 0;
GLIMITOUTPUT = 0;
GLIMITTOTALFILE = 0;
GBESTMATCH = 0;
GRECURSIVE = 0;
GNOPROMPT = 0;
GBYTECOUNT = 0;
GPRINTFILENUMBER = 0;
GPRINTFILETIME = 0;
GOUTTAIL = 2; /* stupid fix, but works */
GFILENAMEONLY = 0;
GNOFILENAME = 0;
GPRINTNONEXISTENTFILE = 0;
MATCHFILE = 0;
PRINTATTR = 0;
PRINTINDEXLINE = 0;
Pat_as_is=0;
Only_first = 0;
PRINTAPPXFILEMATCH = 0;
GCOUNT = 0;
HINTSFROMUSER = 0;
FILENAMESINFILE=0;
BITFIELDFILE=0;
BITFIELDOFFSET=0;
BITFIELDLENGTH=0;
BITFIELDENDIAN=0;
GNumDays = 0;
foundattr = 0;
foundnot = 0;
ComplexBoolean = 0;
bestmatcherrors = 0;
patbufpos = -1;
RegionLimit=DEFAULT_REGION_LIMIT;
strcpy(GD_pattern, "\n");
GD_length = strlen(GD_pattern);
indexdir[0] = '\0';
memset(index_argv, '\0', sizeof(char *) * MAX_ARGS);
index_argc = 0;
memset(agrep_argv, '\0', sizeof(char *) * MAX_ARGS);
agrep_argc = 0;
FileOpt = NULL;
fileopt_length = 0;
memset(pat_list, '\0', sizeof(char *) * MAXNUM_PAT);
memset(pat_attr, '\0', sizeof(int) * MAXNUM_PAT);
for (i=0; i<MAX_ARGS; i++)
index_argv[i] = (char *)my_malloc(MaxNameLength + 2);
memset(is_mgrep_pat, '\0', sizeof(int) * MAXNUM_PAT);
memset(mgrep_pat_index, '\0', sizeof(int) *MAXNUM_PAT);
num_mgrep_pat = 0;
memset(pat_buf, '\0', (MAXNUM_PAT + 2)*MAXPAT);
pat_ptr = 0;
sprintf(tempfile, "%s/.glimpse_tmp.%d", TEMP_DIR, getpid());
/* Set WHOLEFILESCOPE for per-request processing at server */
if (StructuredIndex) WHOLEFILESCOPE = 1;
else WHOLEFILESCOPE = 0;
last_Y_filenumber = 0;
if (argc > MAX_ARGS) {
#if ISSERVER
fprintf(stderr, "too many arguments %d obtained on server!\n", argc);
#endif /*ISSERVER*/
i = fileagrep(oldargc, oldargv, 0, stdout);
RETURN(i);
}
/*
* Process what options you can, then call fileagrep_init() to set
* options in agrep and get the pattern. Then, call fileagrep_search().
* Begin by copying options into agrep_argv assuming glimpse was not
* called as agrep (optimistic :-).
*/
agrep_argc = 0;
for (i=0; i<MAX_ARGS; i++) agrep_argv[i] = NULL;
agrep_argv[agrep_argc] = (char *)my_malloc(strlen(argv[0]) + 2);
strcpy(agrep_argv[agrep_argc], argv[0]); /* copy the name of the program anyway */
agrep_argc ++;
/* In glimpse, you should never output filenames with zero matches */
if (agrep_argc + 1 >= MAX_ARGS) {
fprintf(stderr, "%s: too many options!\n", GProgname);
RETURN(usage());
}
agrep_argv[agrep_argc] = (char *)my_malloc(sizeof(char *));
agrep_argv[agrep_argc][0] = '-';
agrep_argv[agrep_argc][1] = 'z';
agrep_argv[agrep_argc][2] = '\0';
agrep_argc ++;
/* In glimpse, you should always print pattern when using mgrep (user can't do -f or -m)! */
if (agrep_argc + 1 >= MAX_ARGS) {
fprintf(stderr, "%s: too many options!\n", GProgname);
RETURN(usage());
}
agrep_argv[agrep_argc] = (char *)my_malloc(sizeof(char *));
agrep_argv[agrep_argc][0] = '-';
agrep_argv[agrep_argc][1] = 'P';
agrep_argv[agrep_argc][2] = '\0';
my_P_index = agrep_argc;
agrep_argc ++;
/* In glimpse, you should always output multiple when doing mgrep */
if (agrep_argc + 1 >= MAX_ARGS) {
fprintf(stderr, "%s: too many options!\n", GProgname);
RETURN(usage());
}
agrep_argv[agrep_argc] = (char *)my_malloc(sizeof(char *));
agrep_argv[agrep_argc][0] = '-';
agrep_argv[agrep_argc][1] = 'M';
agrep_argv[agrep_argc][2] = '\0';
my_M_index = agrep_argc;
agrep_argc ++;
/* In glimpse, you should print the byte offset if there is a structured query */
if (agrep_argc + 1 >= MAX_ARGS) {
fprintf(stderr, "%s: too many options!\n", GProgname);
RETURN(usage());
}
agrep_argv[agrep_argc] = (char *)my_malloc(sizeof(char *));
agrep_argv[agrep_argc][0] = '-';
agrep_argv[agrep_argc][1] = 'b';
agrep_argv[agrep_argc][2] = '\0';
my_b_index = agrep_argc;
agrep_argc ++;
/* In glimpse, you should always have space for doing -m if required */
if (agrep_argc + 2 >= MAX_ARGS) {
fprintf(stderr, "%s: too many options!\n", GProgname);
RETURN(usage());
}
agrep_argv[agrep_argc] = (char *)my_malloc(sizeof(char *));
agrep_argv[agrep_argc][0] = '-';
agrep_argv[agrep_argc][1] = 'm';
agrep_argv[agrep_argc][2] = '\0';
agrep_argc ++;
agrep_argv[agrep_argc] = (char *)my_malloc(2); /* no op */
agrep_argv[agrep_argc][0] = '\0';
agrep_argc ++;
/* Add -A option to print filenames as default */
if (agrep_argc + 1 >= MAX_ARGS) {
fprintf(stderr, "%s: too many options!\n", GProgname);
RETURN(usage());
}
agrep_argv[agrep_argc] = (char *)my_malloc(sizeof(char *));
agrep_argv[agrep_argc][0] = '-';
agrep_argv[agrep_argc][1] = 'A';
agrep_argv[agrep_argc][2] = '\0';
my_A_index = agrep_argc;
agrep_argc ++;
while((agrep_argc < MAX_ARGS) && (--argc > 0) && (*++argv)[0] == '-' ) {
p = argv[0] + 1; /* ptr to first character after '-' */
c = *(argv[0]+1);
quitwhile = OFF;
while (!quitwhile && (*p != '\0')) {
c = *p;
switch(c) {
case 'F' :
MATCHFILE = ON;
FileOpt = (CHAR *)my_malloc(MAXFILEOPT);
if (*(p + 1) == '\0') {/* space after - option */
if(argc <= 1) {
fprintf(stderr, "%s: a file pattern must follow the -F option\n", GProgname);
RETURN(usage());
}
argv++;
if ((dummylen = strlen(argv[0])) > MAXFILEOPT) {
fprintf(stderr, "%s: -F option list too long\n", GProgname);
RETURN(usage());
}
strcpy(FileOpt, argv[0]);
argc--;
} else {
if ((dummylen = strlen(p+1)) > MAXFILEOPT) {
fprintf(stderr, "%s: -F option list too long\n", GProgname);
RETURN(usage());
}
strcpy(FileOpt, p+1);
} /* else */
quitwhile = ON;
break;
/* search the index only and output the number of blocks */
case 'N' :
Only_first = ON;
break ;
/* also keep track of the matches in each file */
case 'Q' :
PRINTAPPXFILEMATCH = ON;
break ;
case 'U' :
InfoAfterFilename = ON;
break;
case '!' :
HINTSFROMUSER = ON;
break;
/* go to home directory to find the index: even if server overwrites indexdir here, it won't overwrite INDEX_DIR until read_index() */
case 'H' :
if (*(p + 1) == '\0') {/* space after - option */
if (argc <= 1) {
fprintf(stderr, "%s: a directory name must follow the -H option\n", GProgname);
RETURN(usage());
}
argv ++;
#if !ISSERVER
strcpy(indexdir, argv[0]);
#endif /*!ISSERVER*/
argc --;
}
#if !ISSERVER
else {
strcpy(indexdir, p+1);
}
agrep_argv[agrep_argc] = (char *)my_malloc(4);
strcpy(agrep_argv[agrep_argc], "-H");
agrep_argc ++;
agrep_argv[agrep_argc] = (char *)my_malloc(strlen(indexdir) + 2);
strcpy(agrep_argv[agrep_argc], indexdir);
agrep_argc ++;
#endif /*!ISSERVER*/
quitwhile = ON;
break;
#if ISSERVER && SFS_COMPAT
/* INDEX_DIR will be already set since this is the server, so we can direclty xfer the .glimpse_* files */
case '.' :
strcpy(TEMP_FILE, INDEX_DIR);
strcpy(temp_file, ".");
strcat(TEMP_FILE, "/.");
if (*(p + 1) == '\0') {/* space after - option */
if (argc <= 1) {
fprintf(stderr, "%s: a file name must follow the -. option\n", GProgname);
RETURN(usage());
}
argv ++;
strcat(TEMP_FILE, argv[0]);
strcat(temp_file, argv[0]);
argc --;
}
else {
strcat(TEMP_FILE, p+1);
strcat(temp_file, p+1);
}
if (!strcmp(temp_file, INDEX_FILE) || !strcmp(temp_file, FILTER_FILE) ||
!strcmp(temp_file, ATTRIBUTE_FILE) || !strcmp(temp_file, MINI_FILE) ||
!strcmp(temp_file, P_TABLE) || !strcmp(temp_file, PROHIBIT_LIST) ||
!strcmp(temp_file, INCLUDE_LIST) || !strcmp(temp_file, NAME_LIST) ||
!strcmp(temp_file, NAME_LIST_INDEX) || !strcmp(temp_file, NAME_HASH) ||
!strcmp(temp_file, NAME_HASH_INDEX) || !strcmp(temp_file, DEF_STAT_FILE) ||
!strcmp(temp_file, DEF_MESSAGE_FILE) || !strcmp(temp_file, DEF_TIME_FILE)) {
if ((ret = open(TEMP_FILE, O_RDONLY, 0)) <= 0) RETURN(ret);
while ((num_read = read(ret, matched_region, MAX_REGION_LIMIT*2)) > 0) {
write(1 /* NOT TO newsockfd since that was got by a syscall!!! */, matched_region, num_read);
}
close(ret);
}
quitwhile = ON;
RETURN(0);
#endif /* ISSERVER */
/* go to temp directory to create temp files */
case 'T' :
if (*(p + 1) == '\0') {/* space after - option */
if (argc <= 1) {
fprintf(stderr, "%s: a directory name must follow the -T option\n", GProgname);
RETURN(usage());
}
argv ++;
strcpy(TEMP_DIR, argv[0]);
argc --;
}
else {
strcpy(TEMP_DIR, p+1);
}
sprintf(tempfile, "%s/.glimpse_tmp.%d", TEMP_DIR, getpid());
quitwhile = ON;
break;
/* To get files within some number of days before indexing was done */
case 'Y':
if (*(p + 1) == '\0') {/* space after - option */
if (argc <= 1) {
fprintf(stderr, "%s: the number of days must follow the -Y option\n", GProgname);
RETURN(usage());
}
argv ++;
GNumDays = atoi(argv[0]);
argc --;
}
else {
GNumDays = atoi(p+1);
}
if (GNumDays <= 0) {
fprintf(stderr, "%s: the number of days %d must be > 0\n", GProgname, GNumDays);
RETURN(usage());
}
quitwhile = ON;
break;
case 'R' :
if (*(p + 1) == '\0') {/* space after - option */
if (argc <= 1) {
fprintf(stderr, "%s: the record size must follow the -R option\n", GProgname);
RETURN(usage());
}
argv ++;
RegionLimit = atoi(argv[0]);
argc --;
}
else {
RegionLimit = atoi(p+1);
}
if ((RegionLimit <= 0) || (RegionLimit > MAX_REGION_LIMIT)) {
fprintf(stderr, "Bad record size %d: must be in [%d, %d]: using default %d\n",
RegionLimit, 1, MAX_REGION_LIMIT, DEFAULT_REGION_LIMIT);
RegionLimit = DEFAULT_REGION_LIMIT;
}
quitwhile = ON;
break;
/* doesn't matter if we overwrite the value in the client since the same value would have been picked up in main() anyway */
case 'J' :
if (*(p + 1) == '\0') {/* space after - option */
if (argc <= 1) {
fprintf(stderr, "%s: the server host name must follow the -J option\n", GProgname);
RETURN(usageS());
}
argv ++;
#if !ISSERVER
strcpy(SERV_HOST, argv[0]);
#endif /*!ISSERVER*/
argc --;
}
#if !ISSERVER
else {
strcpy(SERV_HOST, p+1);
}
#endif /*!ISSERVER*/
quitwhile = ON;
break;
/* doesn't matter if we overwrite the value in the client since the same value would have been picked up in main() anyway */
case 'K' :
if (*(p + 1) == '\0') {/* space after - option */
if (argc <= 1) {
fprintf(stderr, "%s: the server port must follow the -C option\n", GProgname);
RETURN(usage());
}
argv ++;
#if !ISSERVER
SERV_PORT = atoi(argv[0]);
#endif /*!ISSERVER*/
argc --;
}
#if !ISSERVER
else {
SERV_PORT = atoi(p+1);
}
if ((SERV_PORT < MIN_SERV_PORT) || (SERV_PORT > MAX_SERV_PORT)) {
fprintf(stderr, "Bad server port %d: must be in [%d, %d]: using default %d\n",
SERV_PORT, MIN_SERV_PORT, MAX_SERV_PORT, DEF_SERV_PORT);
SERV_PORT = DEF_SERV_PORT;
}
#endif /*!ISSERVER*/
quitwhile = ON;
break;
/* Based on contribution From ada@mail2.umu.se Fri Jul 12 01:56 MST 1996; Christer Holgersson, Sen. SysNet Mgr, Umea University/SUNET, Sweden */
/* the bit-mask corresponding to the set of filenames within which the pattern should be searched is explicitly provided in a filename (absolute path name) */
case 'p' :
if (*(p + 1) == '\0') {/* space after - option */
if (argc <= 1) {
fprintf(stderr, "%s: the bitfield file [and an offset/length/endian separated by :] must follow the -p option\n", GProgname);
RETURN(usage());
}
argv ++;
strcpy(bitfield_file, argv[0]);
argc --;
}
else {
strcpy(bitfield_file, p+1);
}
/* Find offset and length into bitfield file */
{
int iiii = 0;
BITFIELDOFFSET=0;
BITFIELDLENGTH=0;
BITFIELDENDIAN=0;
iiii = 0;
while (bitfield_file[iiii] != '\0') {
if (bitfield_file[iiii] == '\\') {
iiii ++;
if (bitfield_file[iiii] == '\0') break;
if (bitfield_file[iiii] == ':') {
strcpy(&bitfield_file[iiii-1], &bitfield_file[iiii]);
}
else iiii ++;
continue;
}
if (bitfield_file[iiii] == ':') {
bitfield_file[iiii] = '\0';
sscanf(&bitfield_file[iiii+1], "%d:%d:%d", &BITFIELDOFFSET, &BITFIELDLENGTH, &BITFIELDENDIAN);
if ((BITFIELDOFFSET < 0) || (BITFIELDLENGTH < 0) || (BITFIELDENDIAN < 0)) {
fprintf(stderr, "Wrong offset %d or length %d or endian %d of bitfield file\n", BITFIELDOFFSET, BITFIELDLENGTH, BITFIELDENDIAN);
RETURN(usage());
}
break;
}
iiii++;
}
#if BG_DEBUG
fprintf(debug, "BITFIELD %s : %d : %d : %d\n", BITFIELDFILE, BITFIELDOFFSET, BITFIELDLENGTH, BITFIELDENDIAN);
#endif
}
if (bitfield_file[0] != '/') {
getcwd(temp_bitfield_file, MAX_LINE_LEN-1);
strcat(temp_bitfield_file, "/");
strcat(temp_bitfield_file, bitfield_file);
strcpy(bitfield_file, temp_bitfield_file);
}
BITFIELDFILE = 1;
quitwhile = ON;
break;
/* the set of filenames within which the pattern should be searched is explicitly provided in a filename (absolute path name) */
case 'f' :
if (*(p + 1) == '\0') {/* space after - option */
if (argc <= 1) {
fprintf(stderr, "%s: the filenames file must follow the -f option\n", GProgname);
RETURN(usage());
}
argv ++;
strcpy(filenames_file, argv[0]);
argc --;
}
else {
strcpy(filenames_file, p+1);
}
if (filenames_file[0] != '/') {
getcwd(temp_filenames_file, MAX_LINE_LEN-1);
strcat(temp_filenames_file, "/");
strcat(temp_filenames_file, filenames_file);
strcpy(filenames_file, temp_filenames_file);
}
FILENAMESINFILE = 1;
quitwhile = ON;
break;
case 'C' :
CONTACT_SERVER = 1;
break;
case 'a' :
PRINTATTR = 1;
break;
case 'E':
PRINTINDEXLINE = 1;
break;
case 'W':
wholefilescope = 1;
break;
case 'z' :
UseFilters = 1;
break;
case 'r' :
GRECURSIVE = 1;
break;
case 'V' :
printf("\nThis is glimpse version %s, %s.\n\n", GLIMPSE_VERSION, GLIMPSE_DATE);
RETURN(0);
/* Must let 'm' fall thru to default once multipatterns are done in agrep */
case 'm' :
case 'v' :
fprintf(stderr, "%s: illegal option: '-%c'\n", GProgname, c);
RETURN(usage());
case 'I' :
case 'D' :
case 'S' :
/* There is no space after these options */
agrep_argv[agrep_argc] = (char *)my_malloc(strlen(argv[0]) + 2);
agrep_argv[agrep_argc][0] = '-';
strcpy(agrep_argv[agrep_argc] + 1, p);
agrep_argc ++;
quitwhile = ON;
break;
case 'l':
GFILENAMEONLY = 1;
my_l_index = agrep_argc;
agrep_argv[agrep_argc] = (char *)my_malloc(4);
agrep_argv[agrep_argc][0] = '-';
agrep_argv[agrep_argc][1] = c;
agrep_argv[agrep_argc][2] = '\0';
agrep_argc ++;
break;
/*
* Copy the set of options for agrep: put them in separate argvs
* even if they are together after one '-' (easier to process).
* These are agrep options which glimpse has to peek into.
*/
default:
agrep_argv[agrep_argc] = (char *)my_malloc(16);
agrep_argv[agrep_argc][0] = '-';
agrep_argv[agrep_argc][1] = c;
agrep_argv[agrep_argc][2] = '\0';
agrep_argc ++;
if (c == 'n') {
nobytelevelmustbeon=1;
}
else if (c == 'X') GPRINTNONEXISTENTFILE = 1;
else if (c == 'j') GPRINTFILETIME = 1;
else if (c == 'b') GBYTECOUNT = 1;
else if (c == 'g') GPRINTFILENUMBER = 1;
else if (c == 't') GOUTTAIL = 1;
else if (c == 'y') GNOPROMPT = 1;
else if (c == 'h') GNOFILENAME = 1;
else if (c == 'c') GCOUNT = 1;
else if (c == 'B') {
GBESTMATCH = 1;
my_B_index = agrep_argc - 1;
}
/* the following options are followed by a parameter */
else if ((c == 'e') || (c == 'd') || (c == 'L') || (c == 'k')) {
if (*(p + 1) == '\0') {/* space after - option */
if(argc <= 1) {
fprintf(stderr, "%s: the '-%c' option must have an argument\n", GProgname, c);
RETURN(usage());
}
argv++;
if ( (c == 'd') && ((D_length = strlen(argv[0])) > MAX_NAME_SIZE) ) {
fprintf(stderr, "%s: delimiter pattern too long (has > %d chars)\n", GProgname, MAX_NAME_SIZE);
RETURN(usage());
/* Should this be RegionLimit if ByteLevelIndex? */
}
else if (c == 'L') {
GLIMITOUTPUT = GLIMITTOTALFILE = GLIMITPERFILE = 0;
sscanf(argv[0], "%d:%d:%d", &GLIMITOUTPUT, &GLIMITTOTALFILE, &GLIMITPERFILE);
if ((GLIMITOUTPUT < 0) || (GLIMITTOTALFILE < 0) || (GLIMITPERFILE < 0)) {
fprintf(stderr, "%s: invalid output limit %s\n", GProgname, argv[0]);
RETURN(usage());
}
}
agrep_argv[agrep_argc] = (char *)my_malloc(strlen(argv[0]) + 2);
strcpy(agrep_argv[agrep_argc], argv[0]);
if (c == 'd') {
preprocess_delimiter(argv[0], D_length, GD_pattern, &GD_length);
if (GOUTTAIL == 2) GOUTTAIL = 0;
/* Should this be RegionLimit if ByteLevelIndex? */
}
if (c == 'k') GCONSTANT = 1;
argc--;
} else {
if ( (c == 'd') && ((D_length = strlen(p+1)) > MAX_NAME_SIZE) ) {
fprintf(stderr, "%s: delimiter pattern too long (has > %d chars)\n", GProgname, MAX_NAME_SIZE);
RETURN(usage());
/* Should this be RegionLimit if ByteLevelIndex? */
}
else if (c == 'L') {
GLIMITOUTPUT = GLIMITTOTALFILE = GLIMITPERFILE = 0;
sscanf(p+1, "%d:%d:%d", &GLIMITOUTPUT, &GLIMITTOTALFILE, &GLIMITPERFILE);
if ((GLIMITOUTPUT < 0) || (GLIMITTOTALFILE < 0) || (GLIMITPERFILE < 0)) {
fprintf(stderr, "%s: invalid output limit %s\n", GProgname, p+1);
RETURN(usage());
}
}
agrep_argv[agrep_argc] = (char *)my_malloc(strlen(p+1) + 2);
strcpy(agrep_argv[agrep_argc], p+1);
if (c == 'd') {
preprocess_delimiter(p+1, D_length-2, GD_pattern, &GD_length);
if (GOUTTAIL == 2) GOUTTAIL = 0;
/* Should this be RegionLimit if ByteLevelIndex? */
}
if (c == 'k') GCONSTANT = 1;
}
agrep_argc ++;
#if DEBUG
fprintf(stderr, "%d = %s\n", agrep_argc, agrep_argv[agrep_argc - 1]);
#endif /*DEBUG*/
quitwhile = ON;
if ((c == 'e') || (c == 'k')) foundpat = 1;
}
/* else it is something that glimpse doesn't know and agrep needs to look at */
break; /* from default: */
} /* switch(c) */
p ++;
}
} /* while (--argc > 0 && (*++argv)[0] == '-') */
/* exitloop: */
if ((GBESTMATCH == ON) && (MATCHFILE == ON) && (Only_first == ON))
fprintf(stderr, "%s: Warning: the number of matches may be incorrect when -B is used with -F.\n", HARVEST_PREFIX);
if (GOUTTAIL) GOUTTAIL = 1;
if (GNOFILENAME) {
agrep_argv[my_A_index][1] = 'Z'; /* ignore the -A option */
}
#if ISSERVER
if (RemoteFiles) { /* force -NQ so that won't start looking for files! */
Only_first = ON;
PRINTAPPXFILEMATCH = ON;
}
#endif
if (argc > 0) {
/* copy the rest of the options the pattern and the filenames if any verbatim */
for (i=0; i<argc; i++) {
if (agrep_argc >= MAX_ARGS) break;
agrep_argv[agrep_argc] = (char *)my_malloc(strlen(argv[0]) + 2);
strcpy(agrep_argv[agrep_argc], argv[0]);
agrep_argc ++;
argv ++; }
if (!foundpat) argc --;
}
#if 0
for (j=0; j<agrep_argc; j++) printf("agrep_argv[%d] = %s\n", j, agrep_argv[j]);
printf("argc = %d\n", argc);
#endif /*0*/
/*
* Now perform the search by first looking at the index
* and obtaining the files to search; and then search
* them and output the result. If argc > 0, glimpse
* runs as agrep: otherwise, it searches index, etc.
*/
if (argc <= 0) {
if (RecordLevelIndex) { /* based on work done for robint@zedcor.com Robin Thomas, Art Today, Tucson, AZ */
/*
if ((D_length > 0) && strcmp(GD_pattern, rdelim)) {
fprintf(stderr, "Index created for delimiter `%s': cannot search with delimiter `%s'\n", rdelim, GD_pattern);
RETURN(-1);
}
SHOULD I HAVE THIS CHECK? MAYBE GD_pattern is a SUBSTRING OF rdelim??? But this is safest thing to do... robint@zedcor.com
*/
RegionLimit = 0; /* region is EXACTLY the same record number, not a portion of a file within some offset+length */
}
glimpse_call = 1;
/* Initialize some data structures, read the index */
if (GRECURSIVE == 1) {
fprintf(stderr, "illegal option: '-r'\n");
RETURN(usage());
}
num_terminals = 0;
GParse = NULL;
memset(terminals, '\0', sizeof(ParseTree) * MAXNUM_PAT);
#if !ISSERVER
if (-1 == read_index(indexdir)) RETURN(-1);
#endif /*!ISSERVER*/
/*
This handles the -n option with ByteLevelIndex: disabled as of now, else should go into file search...
*/
if (nobytelevelmustbeon && (ByteLevelIndex && !RecordLevelIndex)) { /* with RecordLevelIndex, we'll do search, so don't set NOBYTELEVEL */
/* fprintf(stderr, "Warning: -n option used with byte-level index: must SEARCH the files\n"); */
NOBYTELEVEL=ON;
}
WHOLEFILESCOPE = (WHOLEFILESCOPE || wholefilescope);
if (ByteLevelIndex) {
/* Must zero them here in addition to index search so that RETURN macro runs correctly */
if ((src_offset_table == NULL) &&
((src_offset_table = (struct offsets **)my_malloc(sizeof(struct offsets *) * OneFilePerBlock)) == NULL)) exit(2);
memset(src_offset_table, '\0', sizeof(struct offsets *) * OneFilePerBlock);
for (i=0; i<MAXNUM_PAT; i++) {
if ((multi_dest_offset_table[i] == NULL) &&
((multi_dest_offset_table[i] = (struct offsets **)my_malloc(sizeof(struct offsets *) * OneFilePerBlock)) == NULL)) exit(2);
memset(multi_dest_offset_table[i], '\0', sizeof(struct offsets *) * OneFilePerBlock);
}
}
read_filters(INDEX_DIR, UseFilters);
if (glimpse_clientdied) RETURN(0);
/* Now initialize agrep, set the options and get the actual pattern into GPattern */
if ((GM = fileagrep_init(agrep_argc, agrep_argv, MAXPAT, GPattern)) <= 0) {
/* this printf need not be there: agrep prints messages if error */
fprintf(stderr, "%s: error in options or arguments to `agrep'\n", HARVEST_PREFIX);
RETURN(usage());
}
patindex = pattern_index;
for (j=0; j<GM; j++) {
if (GPattern[j] == '\\') j++;
else if (test_indexable_char[GPattern[j]]) break;
}
if (j >= GM) {
fprintf(stderr, "%s: pattern '%s' has no characters that were indexed: glimpse cannot search for it\n", HARVEST_PREFIX, GPattern);
for (j=0; j<GM; j++) {
if (GPattern[j] == '\\') j++;
else if (isdigit(GPattern[j])) break;
}
if (j < GM) {
fprintf(stderr, "\t(to search for numbers, make the index using 'glimpseindex -n ...')\n");
}
RETURN(-1);
}
/* Split GPattern into individual boolean terms */
if (GCONSTANT) {
strcpy(APattern, GPattern);
GParse = NULL;
ComplexBoolean = 0;
terminals[0].op = 0;
terminals[0].type = LEAF;
terminals[0].terminalindex = 0;
terminals[0].data.leaf.attribute = 0;
terminals[0].data.leaf.value = (CHAR *)malloc(GM + 1);
strcpy(terminals[0].data.leaf.value, GPattern);
num_terminals = 1;
}
else if (split_pattern(GPattern, GM, APattern, terminals, &num_terminals, &GParse, StructuredIndex) <= 0) RETURN(-1);
#if BG_DEBUG
fprintf(debug, "GPattern = %s, APattern = %s, num_terminals = %d\n", GPattern, APattern, num_terminals);
#endif /*BG_DEBUG*/
/* Set scope for booleans */
if (foundnot && !wholefilescope) {
fprintf(stderr, "To use the NOT (~) operation, you must use whole file scope (-W) for booleans.\n");
RETURN(usage())
}
if (foundattr) WHOLEFILESCOPE = 1; /* makes no sense to search attribute=value expressions without WHOLEFILESCOPE */
else if (!ComplexBoolean && !PRINTATTR && !((long)GParse & AND_EXP)) WHOLEFILESCOPE = 0; /* ORs can be done without WHOLEFILESCOPE */
if (WHOLEFILESCOPE <= 0) agrep_argv[my_b_index][1] = 'Z';
/*
if (!ComplexBoolean && ((long)GParse & AND_EXP) && (my_l_index != -1)) agrep_argv[my_l_index][1] = 'Z';
*/
if ((ComplexBoolean || ((long)GParse & AND_EXP)) && (my_l_index != -1)) agrep_argv[my_l_index][1] = 'Z';
/* Now re-initialize agrep_argv with APattern instead of GPattern */
my_free(agrep_argv[patindex], 0);
AM=strlen(APattern);
agrep_argv[patindex] = (char *)my_malloc(AM + 2);
strcpy(agrep_argv[patindex], APattern);
if (HINTSFROMUSER) {
int num=0, x, y, i, j;
char temp[MAX_NAME_SIZE+2];
struct offsets *o, *tailo, *heado;
while(1) {
if ((num = readline(newsockfd, dest_index_buf, REAL_INDEX_BUF)) <= 0) {
fprintf(stderr, "Input format error with -U option\n");
RETURN(-1);
}
dest_index_buf[num+1] = '\n';
if (!strncmp(dest_index_buf, "BEGIN", strlen("BEGIN"))) break;
}
sscanf(&dest_index_buf[strlen("BEGIN")], "%d%d%d", &bestmatcherrors, &NOBYTELEVEL, &OPTIMIZEBYTELEVEL);
/* printf("BEGIN %d %d %d\n", bestmatcherrors, NOBYTELEVEL, OPTIMIZEBYTELEVEL); */
num = readline(newsockfd, dest_index_buf, REAL_INDEX_BUF);
while (num > 0) {
dest_index_buf[num+1] = '\n';
if (!strncmp(dest_index_buf, "END", strlen("END"))) break;
i = j = 0;
while ((j<MAX_NAME_SIZE) && (dest_index_buf[i] != ' ') && (dest_index_buf[i] != '[') && (dest_index_buf[i] != '\n'))
temp[j++] = dest_index_buf[i++];
temp[j] = '\0';
x = atoi(temp);
GFileIndex[GNumfiles] = x;
if (x == file_num - 1) {
bigbuffer[bigbuffer_size] = '\0';
GTextfiles[GNumfiles++] = (CHAR *)strdup(GTextfilenames[x]);
bigbuffer[bigbuffer_size] = '\n';
}
else {
*(GTextfilenames[x+1] - 1) = '\0';
GTextfiles[GNumfiles++] = (CHAR *)strdup(GTextfilenames[x]);
*(GTextfilenames[x+1] - 1) = '\n';
}
/* printf("%d %s [", x, GTextfiles[GNumfiles-1]); */
src_index_set[block2index(x)] |= block2mask(x);
if (ByteLevelIndex && !NOBYTELEVEL) { /* NOBYTELEVEL is 0 here with RecordLevelIndex */
heado = tailo = NULL;
onemorey:
j = 0;
while ((j<MAX_NAME_SIZE) && ((dest_index_buf[i] == ' ') || (dest_index_buf[i] == '['))) i++;
while ((j<MAX_NAME_SIZE) && (dest_index_buf[i] != ' ') && (dest_index_buf[i] != '\n') && (dest_index_buf[i] != ']'))
temp[j++] = dest_index_buf[i++];
temp[j] = '\0';
y = atoi(temp);
/* printf(" %d", y); */
o = (struct offsets *)my_malloc(sizeof(struct offsets));
o->offset = y;
o->next = NULL;
o->sign = o->done = 0;
if (heado == NULL) {
heado = o;
tailo = o;
}
else {
tailo->next = o;
tailo = o;
}
if (dest_index_buf[i] == ' ') goto onemorey;
src_offset_table[x] = heado;
}
/* printf("]\n"); */
num = readline(newsockfd, dest_index_buf, REAL_INDEX_BUF);
}
goto search_files;
}
/*
* Copy the agrep-options that are relevant to index search into
* index_argv (see man-pages for which options are relevant).
* Also, adjust patindex whenever options are skipped over.
* NOTE: agrep_argv does NOT contain two options after one '-'.
*/
index_argc = 0;
for (j=0; j<agrep_argc; j++) {
if (agrep_argv[j][0] == '-') {
if ((agrep_argv[j][1] == 'c') || (agrep_argv[j][1] == 'h') || (agrep_argv[j][1] == 'l') || (agrep_argv[j][1] == 'n') ||
(agrep_argv[j][1] == 's') || (agrep_argv[j][1] == 't') || (agrep_argv[j][1] == 'G') || (agrep_argv[j][1] == 'O') ||
(agrep_argv[j][1] == 'b') || (agrep_argv[j][1] == 'i') || (agrep_argv[j][1] == 'u') || (agrep_argv[j][1] == 'g') ||
(agrep_argv[j][1] == 'E') || (agrep_argv[j][1] == 'Z') || (agrep_argv[j][1] == 'j') || (agrep_argv[j][1] == 'X')) {
patindex --;
continue;
}
if ((agrep_argv[j][1] == 'd') || (agrep_argv[j][1] == 'L')) { /* skip over the argument too */
j++;
patindex -= 2;
continue;
}
if ((agrep_argv[j][1] == 'e') || (agrep_argv[j][1] == 'm')) {
strcpy(index_argv[index_argc], agrep_argv[j]);
index_argc ++; j++;
strcpy(index_argv[index_argc], agrep_argv[j]);
if (agrep_argv[j-1][1] == 'm') patbufpos = index_argc; /* where to put the patbuf if fast-boolean by mgrep() */
index_argc ++;
}
else { /* No arguments: just copy THAT option: maybe, change some options */
strcpy(index_argv[index_argc], agrep_argv[j]);
if (agrep_argv[j][1] == 'A') index_argv[index_argc][1] = 'h';
else if (agrep_argv[j][1] == 'x') index_argv[index_argc][1] = 'w';
index_argc++;
}
}
else { /* This is either the pattern itself or a filename */
strcpy(index_argv[index_argc], agrep_argv[j]);
index_argc++;
}
}
sprintf(index_argv[index_argc], "%s", INDEX_FILE);
index_argc ++;
#if 0
for (j=0; j<index_argc; j++) printf("index_argv[%d] = %s\n", j, index_argv[j]);
printf("patindex = %d\n", patindex);
#endif /*0*/
/* process -Y option BEFORE search_index() so that get_set() doesn't even have to collect data for very old files */
ret = process_Y_option(file_num, GNumDays, timesindexfp);
/* Search the index and process index-search-only options; Worry about file-pattern */
ret = search_index(GParse);
if ((ret <= 0) && (!OneFilePerBlock || (src_index_set[REAL_PARTITION - 1] != 1))) RETURN(-1); /* previously was: if ret < 0 then return... */
num_blocks=0;
if (OneFilePerBlock) {
if (ByteLevelIndex || RecordLevelIndex) {
for(iii=0; iii<OneFilePerBlock; iii++) {
if (src_offset_table[iii] != NULL) num_blocks ++;
}
/* printf("num_blocks = %d\n", num_blocks); */
}
if (src_index_set[REAL_PARTITION - 1] == 1) {
num_blocks = OneFilePerBlock;
for(iii=0; iii<round(OneFilePerBlock, 8*sizeof(int)) - 1; iii++) {
src_index_set[iii] = 0xffffffff;
}
src_index_set[i] = 0;
for (jjj=0; jjj<8*sizeof(int); jjj++) {
if (iii*8*sizeof(int) + jjj >= OneFilePerBlock) break;
src_index_set[iii] |= mask_int[jjj];
}
}
else for(iii=0; iii<round(OneFilePerBlock, 8*sizeof(int)); iii++) {
if (src_index_set[iii] == 0) continue;
for (jjj=0; jjj < 8*sizeof(int); jjj++)
if (src_index_set[iii] & mask_int[jjj])
if (!ByteLevelIndex || NOBYTELEVEL) {
num_blocks ++;
}
else {
if (src_offset_table[iii*8*sizeof(int) + jjj] != NULL)
num_blocks ++;
else src_index_set[iii] &= ~mask_int[jjj];
}
}
if (num_blocks > OneFilePerBlock) num_blocks = OneFilePerBlock; /* roundoff */
}
else {
for (iii=0; iii<MAX_PARTITION; iii++)
if (src_index_set[iii]) num_blocks++;
}
if (num_blocks <= 0) RETURN (0);
if ((src_index_set[REAL_PARTITION - 1] == 1) && !Only_first && !OPTIMIZEBYTELEVEL) {
fprintf(stderr, "Warning: pattern has words present in the stop-list: must SEARCH the files\n");
}
if (RecordLevelIndex && GFILENAMEONLY && veryfast && (src_index_set[REAL_PARTITION - 1] != 1)) Only_first = 1;
/* if just the NOBYTELEVEL flag is set, then it is an optimization which glimpse does and user need not be warned */
#if DEBUG
fprintf(stderr, "--> search=%d optimize=%d times=%d all=%d blocks=%d len=%d pat=%s scope=%d\n",
NOBYTELEVEL, OPTIMIZEBYTELEVEL, src_index_set[REAL_PARTITION - 2], src_index_set[REAL_PARTITION - 1], num_blocks, strlen(APattern), APattern, WHOLEFILESCOPE);
#endif /*DEBUG*/
/* Based on contribution From ada@mail2.umu.se Fri Jul 12 01:56 MST 1996; Christer Holgersson, Sen. SysNet Mgr, Umea University/SUNET, Sweden */
if (BITFIELDFILE) {
int i, len = -1, nextchar;
FILE *fp;
fp = fopen(bitfield_file, "r");
if (fp != NULL) {
if (BITFIELDENDIAN >= 2) { /* is a BIG-ENDIAN 4B integer list of indexes of files in .glimpse_filenames (sparse set) */
if (BITFIELDLENGTH == 0) BITFIELDLENGTH = file_num;
if (BITFIELDOFFSET > 0) fseek(fp, BITFIELDOFFSET, (long)0);
if (OneFilePerBlock) {
for (i=0; i<round(file_num, 8*sizeof(int)); i++)
multi_dest_index_set[0][i] = 0;
}
else {
for (i=0; i<MAX_PARTITION; i++)
multi_dest_index_set[0][i] = 0;
}
i = -1;
while ((nextchar = getc(fp)) != EOF) {
nextchar = nextchar & 0xff;
i = nextchar << 24;
if ((nextchar = getc(fp)) == EOF) break;
nextchar = nextchar & 0xff;
i |= nextchar << 16;
if ((nextchar = getc(fp)) == EOF) break;
nextchar = nextchar & 0xff;
i |= nextchar << 8;
if ((nextchar = getc(fp)) == EOF) break;
nextchar = nextchar & 0xff;
i |= nextchar;
if (OneFilePerBlock) {
if (i < file_num) multi_dest_index_set[0][block2index(i)] |= mask_int[i%(8*sizeof(int))];
}
else {
if (i < MAX_PARTITION) multi_dest_index_set[0][i] = 1;
}
}
fclose(fp);
if (i == -1) {
fprintf(stderr, "Error in reading %d bytes from offset %d in bitfield file %s ... ignoring it\n", BITFIELDOFFSET, BITFIELDLENGTH, BITFIELDFILE);
/* ignore index_file */
}
else { /* intersect files in index_file with those that were obtained after pattern search */
if (OneFilePerBlock) {
for (i=0; i<round(file_num, 8*sizeof(int)); i++)
src_index_set[i] &= multi_dest_index_set[0][i];
}
else {
for (i=0; i<MAX_PARTITION; i++)
src_index_set[i] &= multi_dest_index_set[0][i];
}
}
}
else {
if (BITFIELDLENGTH == 0) BITFIELDLENGTH = sizeof(int)*REAL_PARTITION /* sizeof(int)*FILEMASK_SIZE */;
if (BITFIELDOFFSET > 0) fseek(fp, BITFIELDOFFSET, (long)0);
if (OneFilePerBlock) {
for (i=0; i<round(file_num, 8*sizeof(int)); i++)
multi_dest_index_set[0][i] = 0;
}
else {
for (i=0; i<MAX_PARTITION; i++)
multi_dest_index_set[0][i] = 0;
}
i = 0;
while ((i < BITFIELDLENGTH) && (nextchar = getc(fp)) != EOF) {
nextchar = nextchar & 0xff;
if (OneFilePerBlock) {
if (BITFIELDENDIAN == 1) { /* little-endian: little end of integer was dumped first in bitfield_file */
multi_dest_index_set[0][i/4] |= (nextchar << (8*(i%4)));
}
else if (BITFIELDENDIAN == 0) { /* big-endian: big end of integer is first was dumped first in bitfield_file */
multi_dest_index_set[0][i/4] |= (nextchar << (8*(4-1-(i%4))));
}
}
else {
if (i < MAX_PARTITION) { /* interpretation of "bit" changes without OneFilePerBlock */
multi_dest_index_set[0][i] = (nextchar != 0) ? 1 : 0;
}
else break; /* BITFIELDLENGTH, by above definition, is always > MAX_PARTITION: see io.c */
}
i++;
}
fclose(fp);
if (i <= 0) {
fprintf(stderr, "Error in reading %d bytes from offset %d in bitfield file %s ... ignoring it\n", BITFIELDOFFSET, BITFIELDLENGTH, BITFIELDFILE);
/* ignore bitfield_file */
}
else { /* intersect files in bitfield_file with those that were obtained after pattern search */
if (OneFilePerBlock) {
for (i=0; i<round(file_num, 8*sizeof(int)); i++)
src_index_set[i] &= multi_dest_index_set[0][i];
}
else {
for (i=0; i<MAX_PARTITION; i++)
src_index_set[i] &= multi_dest_index_set[0][i];
}
}
}
}
}
if (FILENAMESINFILE) mask_filenames(src_index_set, filenames_file, file_num, num_blocks); /* keep only those files that are in filenames_file */
if (BITFIELDFILE || FILENAMESINFILE) {
num_blocks=0;
if (OneFilePerBlock) {
for(iii=0; iii<round(OneFilePerBlock, 8*sizeof(int)); iii++) {
if (src_index_set[iii] == 0) continue;
for (jjj=0; jjj < 8*sizeof(int); jjj++)
if (src_index_set[iii] & mask_int[jjj])
if (!ByteLevelIndex || NOBYTELEVEL) {
num_blocks ++;
}
else {
if (src_offset_table[iii*8*sizeof(int) + jjj] != NULL)
num_blocks ++;
else src_index_set[iii] &= ~mask_int[jjj];
}
}
if (num_blocks > OneFilePerBlock) num_blocks = OneFilePerBlock; /* roundoff */
}
else {
for (iii=0; iii<MAX_PARTITION; iii++)
if (src_index_set[iii]) num_blocks++;
}
if (num_blocks <= 0) RETURN (0);
}
dummypat[0] = '\0';
if (!MATCHFILE) { /* the argc,argv don't matter */
get_filenames(src_index_set, 0, NULL, dummylen, dummypat, file_num);
if (Only_first) { /* search the index only */
fprintf(stderr, "There are matches to %d out of %d %s\n", num_blocks, (OneFilePerBlock > 0) ? OneFilePerBlock : GNumpartitions, (OneFilePerBlock > 0) ? "files" : "blocks");
if (num_blocks > 0) {
char cc[8];
cc[0] = 'y';
#if !ISSERVER
if (!GNOPROMPT) {
fprintf(stderr, "Do you want to see the file names? (y/n)");
fgets(cc, 4, stdin);
}
#endif /*!ISSERVER*/
if (!SILENT && (cc[0] == 'y')) {
if (PRINTAPPXFILEMATCH && Only_first && GPRINTFILENUMBER) {
printf("BEGIN %d %d %d\n", bestmatcherrors, NOBYTELEVEL, OPTIMIZEBYTELEVEL);
}
for (jjj=0; jjj<GNumfiles; jjj++) {
if ((GLIMITOUTPUT > 0) && (jjj >= GLIMITOUTPUT)) break;
if (ByteLevelIndex && !NOBYTELEVEL && (src_index_set[REAL_PARTITION - 1] != 1) && (src_offset_table[GFileIndex[jjj]] == NULL)) continue;
if (GPRINTFILENUMBER) printf("%d", GFileIndex[jjj]);
else printf("%s", GTextfiles[jjj]);
if (PRINTAPPXFILEMATCH) {
if (GCOUNT) {
int n = 0;
printf(": ");
if (ByteLevelIndex && (src_offset_table != NULL)) {
struct offsets *p1 = src_offset_table[GFileIndex[jjj]];
while (p1 != NULL) {
n ++;
p1 = p1->next;
}
}
else n = 1; /* there is atleast 1 match */
printf("%d", n);
}
else {
printf(" [");
if (ByteLevelIndex && (src_offset_table != NULL)) {
struct offsets *p1 = src_offset_table[GFileIndex[jjj]];
while (p1 != NULL) {
printf(" %d", p1->offset);
p1 = p1->next;
}
}
printf("]");
}
}
printf("\n");
}
if (PRINTAPPXFILEMATCH && Only_first && GPRINTFILENUMBER) {
printf("END\n");
}
}
}
RETURN(0);
} /* end of Only_first */
if (!OneFilePerBlock) searchpercent = num_blocks*100/GNumpartitions;
else searchpercent = num_blocks * 100 / OneFilePerBlock;
#if BG_DEBUG
fprintf(debug, "searchpercent = %d, num_blocks = %d\n", searchpercent, num_blocks);
#endif /*BG_DEBUG*/
#if !ISSERVER
if (!GNOPROMPT && (searchpercent > MAX_SEARCH_PERCENT)) {
char cc[8];
cc[0] = 'y';
fprintf(stderr, "Your query may search about %d%% of the total space! Continue? (y/n)", searchpercent);
fgets(cc, 4, stdin);
if (cc[0] != 'y') RETURN(0);
}
if (ByteLevelIndex && !RecordLevelIndex && (searchpercent > DEF_MAX_INDEX_PERCENT)) NOBYTELEVEL = 1; /* with RecordLevelIndex, I don't just want to stop collecting offsets just because searchpercent > .... */
#endif /*!ISSERVER*/
} /* end of !MATCHFILE */
else { /* set up the right options for -F in index_argv/index_argc itself since they will no longer be used */
index_argc=0;
strcpy(index_argv[0], GProgname);
/* adding the -h option, which is safer for -F */
index_argc ++;
index_argv[index_argc][0] = '-';
index_argv[index_argc][1] = 'h';
index_argv[index_argc][2] = '\0';
index_argc ++;
/* new code: bgopal, Feb/8/94: deleted udi's code here */
j = 0;
while (FileOpt[j] == '-') {
j++;
while ((FileOpt[j] != ' ') && (FileOpt[j] != '\0') && (FileOpt[j] != '\n')) {
if (j >= MAX_ARGS - 1) {
fprintf(stderr, "%s: too many options after -F: %s\n", GProgname, FileOpt);
RETURN(usage());
}
index_argv[index_argc][0] = '-';
index_argv[index_argc][1] = FileOpt[j];
index_argv[index_argc][2] = '\0';
index_argc ++;
j++;
}
if ((FileOpt[j] == '\0') || (FileOpt[j] == '\n')) break;
if ((FileOpt[j] == ' ') && (FileOpt[j-1] == '-')) {
fprintf(stderr, "%s: illegal option: '-' after -F\n", GProgname);
RETURN(usage());
}
else if (FileOpt[j] == ' ') while(FileOpt[j] == ' ') j++;
}
while(FileOpt[j] == ' ') j++;
fileopt_length = strlen(FileOpt);
strncpy(index_argv[index_argc],FileOpt+j,fileopt_length-j);
index_argv[index_argc][fileopt_length-j] = '\0';
index_argc++;
my_free(FileOpt, MAXFILEOPT);
FileOpt = NULL;
#if BG_DEBUG
fprintf(debug, "pattern to check with -F = %s\n",index_argv[index_argc-1]);
#endif /*BG_DEBUG*/
#if DEBUG
fprintf(stderr, "-F : ");
for (jj=0; jj < index_argc; jj++)
fprintf(stderr, " %s ",index_argv[jj]);
fprintf(stderr, "\n");
#endif /*DEBUG*/
fflush(stdout);
get_filenames(src_index_set, index_argc, index_argv, dummylen, dummypat, file_num);
/* Assume #files per partitions is appx constant */
if (OneFilePerBlock) num_blocks = GNumfiles;
else num_blocks = GNumfiles * GNumpartitions / p_table[GNumpartitions - 1];
if (Only_first) { /* search the index only */
fprintf(stderr, "There are matches to %d out of %d %s\n", num_blocks, (OneFilePerBlock > 0) ? OneFilePerBlock : GNumpartitions, (OneFilePerBlock > 0) ? "files" : "blocks");
if (num_blocks > 0) {
char cc[8];
cc[0] = 'y';
#if !ISSERVER
if (!GNOPROMPT) {
fprintf(stderr, "Do you want to see the file names? (y/n)");
fgets(cc, 4, stdin);
}
#endif /*!ISSERVER*/
if (!SILENT && (cc[0] == 'y')) {
if (PRINTAPPXFILEMATCH && Only_first && GPRINTFILENUMBER) {
printf("BEGIN %d %d %d\n", bestmatcherrors, NOBYTELEVEL, OPTIMIZEBYTELEVEL);
}
for (jjj=0; jjj<GNumfiles; jjj++) {
if ((GLIMITOUTPUT > 0) && (jjj >= GLIMITOUTPUT)) break;
if (ByteLevelIndex && !NOBYTELEVEL && (src_index_set[REAL_PARTITION - 1] != 1) && (src_offset_table[GFileIndex[jjj]] == NULL)) continue;
if (GPRINTFILENUMBER) printf("%d", GFileIndex[jjj]);
else printf("%s", GTextfiles[jjj]);
if (PRINTAPPXFILEMATCH) {
if (GCOUNT) {
int n = 0;
printf(": ");
if (ByteLevelIndex && (src_offset_table != NULL)) {
struct offsets *p1 = src_offset_table[GFileIndex[jjj]];
while (p1 != NULL) {
n ++;
p1 = p1->next;
}
}
else n = 1; /* there is atleast 1 match */
printf("%d", n);
}
else {
printf("[");
if (ByteLevelIndex && (src_offset_table != NULL)) {
struct offsets *p1 = src_offset_table[GFileIndex[jjj]];
while (p1 != NULL) {
printf(" %d", p1->offset);
p1 = p1->next;
}
}
printf("]");
}
}
printf("\n");
}
if (PRINTAPPXFILEMATCH && Only_first && GPRINTFILENUMBER) {
printf("END\n");
}
}
}
RETURN(0);
} /* end of Only_first */
if (OneFilePerBlock) searchpercent = GNumfiles * 100 / OneFilePerBlock;
else searchpercent = GNumfiles * 100 / p_table[GNumpartitions - 1];
#if BG_DEBUG
fprintf(debug, "searchpercent = %d, num_files = %d\n", searchpercent, p_table[GNumpartitions - 1]);
#endif /*BG_DEBUG*/
#if !ISSERVER
if (!GNOPROMPT && (searchpercent > MAX_SEARCH_PERCENT)) {
char cc[8];
cc[0] = 'y';
fprintf(stderr, "Your query may search about %d%% of the total space! Continue? (y/n)", searchpercent);
fgets(cc, 4, stdin);
if (cc[0] != 'y') RETURN(0);
}
if (ByteLevelIndex && !RecordLevelIndex && (searchpercent > DEF_MAX_INDEX_PERCENT)) NOBYTELEVEL = 1; /* with RecordLevelIndex, I don't just want to stop collecting offsets just because searchpercent > .... */
#endif /*!ISSERVER*/
}
/* At this point, I have the set of files to search */
search_files:
/* Replace -B by the number of errors if best-match */
if (GBESTMATCH && (my_B_index >= 0)) {
sprintf(&agrep_argv[my_B_index][1], "%d", bestmatcherrors);
#if BG_DEBUG
fprintf(debug, "Changing -B to -%d\n", bestmatcherrors);
#endif /*BG_DEBUG*/
}
agrep_argv[my_M_index][1] = 'Z';
agrep_argv[my_P_index][1] = 'Z';
#if 0
for (iii=0; iii<agrep_argc; iii++) fprintf(stdout, "'%s' ", agrep_argv[iii]);
fprintf(stdout, "\n");
#endif
/*
if (!ComplexBoolean && ((long)GParse & AND_EXP) && (my_l_index != -1) && !WHOLEFILESCOPE) agrep_argv[my_l_index][1] = 'l';
*/
if ((ComplexBoolean || ((long)GParse & AND_EXP)) && (my_l_index != -1) && !WHOLEFILESCOPE) agrep_argv[my_l_index][1] = 'l';
if (GNumfiles <= 0) RETURN(0);
if (glimpse_clientdied) RETURN(0);
/* must reinitialize since the above agrep calls for index-search ruined the real options: it is required EVEN IF ByteLevelIndex */
AM = fileagrep_init(agrep_argc, agrep_argv, MAXPAT, APattern);
/* do actual search with postfiltering if structured query */
if (WHOLEFILESCOPE <= 0) {
if (!UseFilters) {
if (!ByteLevelIndex || RecordLevelIndex || NOBYTELEVEL) {
for (i=0; i<GNumfiles; i++) {
/* if (RecordLevelIndex && (src_offset_table[GFileIndex[i]] == NULL)) continue; */
gprev_num_of_matched = gnum_of_matched;
SetCurrentFileName = 1;
if (GPRINTFILENUMBER) sprintf(CurrentFileName, "%d", GFileIndex[i]);
else strcpy(CurrentFileName, GTextfiles[i]);
if (GPRINTFILETIME) {
SetCurrentFileTime = 1;
CurrentFileTime = get_file_time(timesfp, NULL, GTextfiles[i], GFileIndex[i]);
}
if ((ret = fileagrep_search(AM, APattern, 1, >extfiles[i], 0, stdout)) > 0) {
gnum_of_matched += ret;
gfiles_matched ++;
}
SetCurrentFileName = 0;
if (GPRINTFILETIME) SetCurrentFileTime = 0;
if (GLIMITOUTPUT > 0) {
if (GLIMITOUTPUT <= gnum_of_matched) break;
LIMITOUTPUT = GLIMITOUTPUT - gnum_of_matched;
}
if (GLIMITTOTALFILE > 0) {
if (GLIMITTOTALFILE <= gfiles_matched) break;
LIMITTOTALFILE = GLIMITTOTALFILE - gfiles_matched;
}
if ((ret < 0) && (errno == AGREP_ERROR)) break;
if (glimpse_clientdied) break;
fflush(stdout);
}
}
else {
for (i=0; i<GNumfiles; i++) {
gprev_num_of_matched = gnum_of_matched;
SetCurrentFileName = 1;
if (GPRINTFILENUMBER) sprintf(CurrentFileName, "%d", GFileIndex[i]);
else strcpy(CurrentFileName, GTextfiles[i]);
if (my_stat(GTextfiles[i], &file_stat_buf) == -1) {
if (GPRINTNONEXISTENTFILE) printf("%s\n", CurrentFileName);
continue;
}
if (GPRINTFILETIME) {
SetCurrentFileTime = 1;
CurrentFileTime = get_file_time(timesfp, &file_stat_buf, GTextfiles[i], GFileIndex[i]);
}
if (file_stat_buf.st_mtime > index_stat_buf.st_mtime) {
/* fprintf(stderr, "Warning: file modified after indexing: must SEARCH %s\n", CurrentFileName); */
free_list(&src_offset_table[GFileIndex[i]]);
first_search = 1;
if ((ret = fileagrep_search(AM, APattern, 1, >extfiles[i], 0, stdout)) > 0) {
gnum_of_matched += ret;
gfiles_matched ++;
}
}
else if ((ret = glimpse_search(AM, APattern, GD_length, GD_pattern, GTextfiles[i], GTextfiles[i], GFileIndex[i], src_offset_table, stdout)) > 0) {
gnum_of_matched += ret;
gfiles_matched ++;
}
SetCurrentFileName = 0;
if (GPRINTFILETIME) SetCurrentFileTime = 0;
if (GLIMITOUTPUT > 0) {
if (GLIMITOUTPUT <= gnum_of_matched) break;
LIMITOUTPUT = GLIMITOUTPUT - gnum_of_matched;
}
if (GLIMITTOTALFILE > 0) {
if (GLIMITTOTALFILE <= gfiles_matched) break;
LIMITTOTALFILE = GLIMITTOTALFILE - gfiles_matched;
}
if ((ret < 0) && (errno == AGREP_ERROR)) break;
if (glimpse_clientdied) break;
fflush(stdout);
}
}
} /* end of !UseFilters */
else {
sprintf(outname[0], "%s/.glimpse_apply.%d", TEMP_DIR, getpid());
for (i=0; i<GNumfiles; i++) {
/* if (RecordLevelIndex && (src_offset_table[GFileIndex[i]] == NULL)) continue; */
if (apply_filter(GTextfiles[i], outname[0]) == 1) {
gprev_num_of_matched = gnum_of_matched;
SetCurrentFileName = 1;
if (GPRINTFILENUMBER) sprintf(CurrentFileName, "%d", GFileIndex[i]);
else strcpy(CurrentFileName, GTextfiles[i]);
if (my_stat(GTextfiles[i], &file_stat_buf) == -1) {
if (GPRINTNONEXISTENTFILE) printf("%s\n", CurrentFileName);
continue;
}
if (GPRINTFILETIME) {
SetCurrentFileTime = 1;
CurrentFileTime = get_file_time(timesfp, &file_stat_buf, GTextfiles[i], GFileIndex[i]);
}
if (!ByteLevelIndex || RecordLevelIndex || NOBYTELEVEL || (file_stat_buf.st_mtime > index_stat_buf.st_mtime)) {
first_search = 1;
if ((ret = fileagrep_search(AM, APattern, 1, outname, 0, stdout)) > 0) {
gnum_of_matched += ret;
gfiles_matched ++;
}
}
else {
if (file_stat_buf.st_mtime > index_stat_buf.st_mtime) {
/* fprintf(stderr, "Warning: file modified after indexing: must SEARCH %s\n", CurrentFileName); */
free_list(&src_offset_table[GFileIndex[i]]);
first_search = 1;
if ((ret = fileagrep_search(AM, APattern, 1, outname, 0, stdout)) > 0) {
gnum_of_matched += ret;
gfiles_matched ++;
}
}
else if ((ret = glimpse_search(AM, APattern, GD_length, GD_pattern, GTextfiles[i], outname[0], GFileIndex[i], src_offset_table, stdout)) > 0) {
gfiles_matched ++;
gnum_of_matched += ret;
}
}
unlink(outname[0]);
SetCurrentFileName = 0;
if (GPRINTFILETIME) SetCurrentFileTime = 0;
}
else {
if (!ByteLevelIndex || RecordLevelIndex || NOBYTELEVEL) {
first_search = 1;
if ((ret = fileagrep_search(AM, APattern, 1, >extfiles[i], 0, stdout)) > 0) {
gnum_of_matched += ret;
gfiles_matched ++;
}
}
else {
SetCurrentFileName = 1;
if (GPRINTFILENUMBER) sprintf(CurrentFileName, "%d", GFileIndex[i]);
else strcpy(CurrentFileName, GTextfiles[i]);
if (my_stat(GTextfiles[i], &file_stat_buf) == -1) {
if (GPRINTNONEXISTENTFILE) printf("%s\n", CurrentFileName);
continue;
}
if (GPRINTFILETIME) {
SetCurrentFileTime = 1;
CurrentFileTime = get_file_time(timesfp, &file_stat_buf, GTextfiles[i], GFileIndex[i]);
}
if (file_stat_buf.st_mtime > index_stat_buf.st_mtime) {
/* fprintf(stderr, "Warning: file modified after indexing: must SEARCH %s\n", CurrentFileName); */
free_list(&src_offset_table[GFileIndex[i]]);
first_search = 1;
if ((ret = fileagrep_search(AM, APattern, 1, >extfiles[i], 0, stdout)) > 0) {
gnum_of_matched += ret;
gfiles_matched ++;
}
}
else if ((ret = glimpse_search(AM, APattern, GD_length, GD_pattern, GTextfiles[i], GTextfiles[i], GFileIndex[i], src_offset_table, stdout)) > 0) {
gnum_of_matched += ret;
gfiles_matched ++;
}
SetCurrentFileName = 0;
if (GPRINTFILETIME) SetCurrentFileTime = 0;
}
}
if (GLIMITOUTPUT > 0) {
if (GLIMITOUTPUT <= gnum_of_matched) break;
LIMITOUTPUT = GLIMITOUTPUT - gnum_of_matched;
}
if (GLIMITTOTALFILE > 0) {
if (GLIMITTOTALFILE <= gfiles_matched) break;
LIMITTOTALFILE = GLIMITTOTALFILE - gfiles_matched;
}
if ((ret < 0) && (errno == AGREP_ERROR)) break;
if (glimpse_clientdied) break;
fflush(stdout);
}
}
} /* end of WHOLEFILESCOPE <= 0 */
else {
FILE *tmpfp = NULL; /* to store structured query-search output */
int OLDFILENAMEONLY;/* don't use FILENAMEONLY for agrepping the stuff: handle it in filtering */
int OLDLIMITOUTPUT; /* don't use LIMITs for search: only for filtering=identify_region(): agrep NEVER changes these 3 */
int OLDLIMITPERFILE;
int OLDLIMITTOTALFILE;
int OLDPRINTRECORD; /* don't use PRINTRECORD for search: only after filter_output() recognizes boolean in wholefilescope */
int OLDCOUNT; /* don't use OLDCOUNT for search: only after filter_output() recognizes boolean in wholefilescope */
if (!UseFilters) {
for (i=0; i<GNumfiles; i++) {
/* if (RecordLevelIndex && (src_offset_table[GFileIndex[i]] == NULL)) continue; */
OLDFILENAMEONLY = FILENAMEONLY;
FILENAMEONLY = 0;
OLDLIMITOUTPUT = LIMITOUTPUT;
LIMITOUTPUT = 0;
OLDLIMITPERFILE = LIMITPERFILE;
LIMITPERFILE = 0;
OLDLIMITTOTALFILE = LIMITTOTALFILE;
LIMITTOTALFILE = 0;
OLDPRINTRECORD = PRINTRECORD;
PRINTRECORD = 1;
OLDCOUNT = COUNT;
COUNT = 0;
gprev_num_of_matched = gnum_of_matched;
if ((tmpfp = fopen(tempfile, "w")) == NULL) {
fprintf(stderr, "%s: cannot open for writing: %s, errno=%d\n", GProgname, tempfile, errno);
RETURN(usage());
}
SetCurrentFileName = 1;
if (GPRINTFILENUMBER) sprintf(CurrentFileName, "%d", GFileIndex[i]);
else strcpy(CurrentFileName, GTextfiles[i]);
if (!ByteLevelIndex || RecordLevelIndex || NOBYTELEVEL) {
if (GPRINTFILETIME) {
SetCurrentFileTime = 1;
CurrentFileTime = get_file_time(timesfp, NULL, GTextfiles[i], GFileIndex[i]);
}
first_search = 1;
ret = fileagrep_search(AM, APattern, 1, >extfiles[i], 0, tmpfp);
}
else {
if (my_stat(GTextfiles[i], &file_stat_buf) == -1) {
if (GPRINTNONEXISTENTFILE) printf("%s\n", CurrentFileName);
fclose(tmpfp);
continue;
}
if (GPRINTFILETIME) {
SetCurrentFileTime = 1;
CurrentFileTime = get_file_time(timesfp, &file_stat_buf, GTextfiles[i], GFileIndex[i]);
}
if (file_stat_buf.st_mtime > index_stat_buf.st_mtime) {
/* fprintf(stderr, "Warning: file modified after indexing: must SEARCH %s\n", CurrentFileName); */
free_list(&src_offset_table[GFileIndex[i]]);
first_search = 1;
ret = fileagrep_search(AM, APattern, 1, >extfiles[i], 0, tmpfp);
}
else ret = glimpse_search(AM, APattern, GD_length, GD_pattern, GTextfiles[i], GTextfiles[i], GFileIndex[i], src_offset_table, tmpfp);
}
SetCurrentFileName = 0;
if (GPRINTFILETIME) SetCurrentFileTime = 0;
fflush(tmpfp);
fclose(tmpfp);
tmpfp = NULL;
if ((ret < 0) && (errno == AGREP_ERROR)) break;
#if DEBUG
printf("done search\n");
fflush(stdout);
#endif /*DEBUG*/
FILENAMEONLY = OLDFILENAMEONLY;
LIMITOUTPUT = OLDLIMITOUTPUT;
LIMITPERFILE = OLDLIMITPERFILE;
LIMITTOTALFILE = OLDLIMITTOTALFILE;
PRINTRECORD = OLDPRINTRECORD;
COUNT = OLDCOUNT;
ret = filter_output(GTextfiles[i], tempfile, GParse, GD_pattern, GD_length, GOUTTAIL, nullfp, StructuredIndex);
gnum_of_matched += (ret > 0) ? ret : 0;
gfiles_matched += (ret > 0) ? 1 : 0;
if (GLIMITOUTPUT > 0) {
if (GLIMITOUTPUT <= gnum_of_matched) break;
LIMITOUTPUT = GLIMITOUTPUT - gnum_of_matched;
}
if (GLIMITTOTALFILE > 0) {
if (GLIMITTOTALFILE <= gfiles_matched) break;
LIMITTOTALFILE = GLIMITTOTALFILE - gfiles_matched;
}
if (glimpse_clientdied) break;
fflush(stdout);
}
}
else { /* we should try to apply the filter (we come here with -W -z, say) */
sprintf(outname[0], "%s/.glimpse_apply.%d", TEMP_DIR, getpid());
for (i=0; i<GNumfiles; i++) {
/* if (RecordLevelIndex && (src_offset_table[GFileIndex[i]] == NULL)) continue; */
OLDFILENAMEONLY = FILENAMEONLY;
FILENAMEONLY = 0;
OLDLIMITOUTPUT = LIMITOUTPUT;
LIMITOUTPUT = 0;
OLDLIMITPERFILE = LIMITPERFILE;
LIMITPERFILE = 0;
OLDLIMITTOTALFILE = LIMITTOTALFILE;
LIMITTOTALFILE = 0;
OLDPRINTRECORD = PRINTRECORD;
PRINTRECORD = 1;
OLDCOUNT = COUNT;
COUNT = 0;
gprev_num_of_matched = gnum_of_matched;
if ((tmpfp = fopen(tempfile, "w")) == NULL) {
fprintf(stderr, "%s: cannot open for writing: %s, errno=%d\n", GProgname, tempfile, errno);
RETURN(usage());
}
SetCurrentFileName = 1;
if (GPRINTFILENUMBER) sprintf(CurrentFileName, "%d", GFileIndex[i]);
else strcpy(CurrentFileName, GTextfiles[i]);
if (apply_filter(GTextfiles[i], outname[0]) == 1) {
if (my_stat(GTextfiles[i], &file_stat_buf) == -1) {
if (GPRINTNONEXISTENTFILE) printf("%s\n", CurrentFileName);
fclose(tmpfp);
continue;
}
if (GPRINTFILETIME) {
SetCurrentFileTime = 1;
CurrentFileTime = get_file_time(timesfp, &file_stat_buf, GTextfiles[i], GFileIndex[i]);
}
if (!ByteLevelIndex || RecordLevelIndex || NOBYTELEVEL || (file_stat_buf.st_mtime > index_stat_buf.st_mtime)) {
first_search = 1;
ret = fileagrep_search(AM, APattern, 1, outname, 0, tmpfp);
}
else {
if (file_stat_buf.st_mtime > index_stat_buf.st_mtime) {
/* fprintf(stderr, "Warning: file modified after indexing: must SEARCH %s\n", CurrentFileName); */
free_list(&src_offset_table[GFileIndex[i]]);
first_search = 1;
ret = fileagrep_search(AM, APattern, 1, outname, 0, tmpfp);
}
else ret = glimpse_search(AM, APattern, GD_length, GD_pattern, GTextfiles[i], outname[0], GFileIndex[i], src_offset_table, tmpfp);
}
unlink(outname[0]);
}
else {
if (!ByteLevelIndex || RecordLevelIndex || NOBYTELEVEL) {
if (GPRINTFILETIME) {
SetCurrentFileTime = 1;
CurrentFileTime = get_file_time(timesfp, NULL, GTextfiles[i], GFileIndex[i]);
}
first_search = 1;
ret = fileagrep_search(AM, APattern, 1, >extfiles[i], 0, tmpfp);
}
else {
if (my_stat(GTextfiles[i], &file_stat_buf) == -1) {
if (GPRINTNONEXISTENTFILE) printf("%s\n", CurrentFileName);
fclose(tmpfp);
continue;
}
if (GPRINTFILETIME) {
SetCurrentFileTime = 1;
CurrentFileTime = get_file_time(timesfp, &file_stat_buf, GTextfiles[i], GFileIndex[i]);
}
if (file_stat_buf.st_mtime > index_stat_buf.st_mtime) {
/* fprintf(stderr, "Warning: file modified after indexing: must SEARCH %s\n", CurrentFileName); */
free_list(&src_offset_table[GFileIndex[i]]);
first_search = 1;
ret = fileagrep_search(AM, APattern, 1, >extfiles[i], 0, tmpfp);
}
else ret = glimpse_search(AM, APattern, GD_length, GD_pattern, GTextfiles[i], GTextfiles[i], GFileIndex[i], src_offset_table, tmpfp);
}
}
SetCurrentFileName = 0;
if (GPRINTFILETIME) SetCurrentFileTime = 0;
fflush(tmpfp);
fclose(tmpfp);
tmpfp = NULL;
if ((ret < 0) && (errno == AGREP_ERROR)) break;
#if DEBUG
printf("done search\n");
fflush(stdout);
#endif /*DEBUG*/
FILENAMEONLY = OLDFILENAMEONLY;
LIMITOUTPUT = OLDLIMITOUTPUT;
LIMITPERFILE = OLDLIMITPERFILE;
LIMITTOTALFILE = OLDLIMITTOTALFILE;
PRINTRECORD = OLDPRINTRECORD;
COUNT = OLDCOUNT;
ret = filter_output(GTextfiles[i], tempfile, GParse, GD_pattern, GD_length, GOUTTAIL, nullfp, StructuredIndex);
gnum_of_matched += (ret > 0) ? ret : 0;
gfiles_matched += (ret > 0) ? 1 : 0;
if (GLIMITOUTPUT > 0) {
if (GLIMITOUTPUT <= gnum_of_matched) break;
LIMITOUTPUT = GLIMITOUTPUT - gnum_of_matched;
}
if (GLIMITTOTALFILE > 0) {
if (GLIMITTOTALFILE <= gfiles_matched) break;
LIMITTOTALFILE = GLIMITTOTALFILE - gfiles_matched;
}
if (glimpse_clientdied) break;
fflush(stdout);
}
}
}
if (errno == AGREP_ERROR) {
fprintf(stderr, "%s: error in options or arguments to `agrep'\n", HARVEST_PREFIX);
}
RETURN(0);
}
else { /* argc > 0: simply call agrep */
#if DEBUG
for (i=0; i<agrep_argc; i++)
printf("agrep_argv[%d] = %s\n", i, agrep_argv[i]);
#endif /*DEBUG*/
i = fileagrep(oldargc, oldargv, 0, stdout);
RETURN(i);
}
}
/* end of process_query() */
/*
* Simple function to remove the non-existent files from the set of
* files passed onto agrep for search. These are the files which got
* DELETED after the index was built (but a fresh index was NOT built).
* Redundant since agrep opens them anyway and stat is as bad as open.
*/
int
purge_filenames(filenames, num)
CHAR **filenames;
int num;
{
struct stat buf;
int i, j;
int newnum = num;
int ret;
for (i=0; i<newnum; i++) {
if (-1 == (ret = my_stat(filenames[i], &buf))) {
#if BG_DEBUG
fprintf(debug, "stat on %s = %d\n", filenames[i], ret);
#endif /*BG_DEBUG*/
my_free(filenames[i], 0);
for (j=i; j<newnum-1; j++)
filenames[j] = filenames[j+1];
filenames[j] = NULL;
newnum --;
i--; /* to counter the ++ on top */
}
}
#if BG_DEBUG
fprintf(debug, "Old numfiles=%d\tNew numfiles=%d\n", num, newnum);
for (i=0; i<newnum; i++)
fprintf(debug, "file %d = %s\n", i, filenames[i]);
#endif /*BG_DEBUG*/
return newnum;
}
CHAR filter_buf[BLOCKSIZE + MAXPAT*2];
/* returns #of bytes stripped off */
int getbyteoff(buf, pbyteoff)
CHAR *buf;
int *pbyteoff;
{
CHAR temp[32];
int i = 0;
while (isdigit(*buf) && (i<32)) temp[i++] = *buf++;
if ((*buf != '=') || (*(buf + 1) != ' ')) return -1;
temp[i] = '\0';
*pbyteoff = atoi(temp);
return i+2;
}
/*
* Filter the output in infile:
*
* -- get the matched line/record-s using GD_pattern, GD_length and GOUTAIL
* -- call identify regions using matched line/record's byte offset
* -- collect patterns corr. to that attribute into a new pattern (in split_pat itself)
* -- see if one of them matches that line/record using memagrep
* -- if so, output that line/record onto stdout
*/
/* May have to change name to reflect the fact that it does booleans too */
int
filter_output(infile, outfile, GParse, GD_pattern, GD_length, GOUTTAIL, nullfp, num_attr)
char *infile;
char *outfile;
ParseTree *GParse;
CHAR GD_pattern[];
int GD_length;
int GOUTTAIL;
FILE *nullfp;
int num_attr;
{
FILE *outfp;
FILE *displayfp = NULL;
FILE *storefp = NULL;
int num_read, total_read = 0;
int residue = 0;
int byteoff;
int attribute;
int i, ii; /* i is forloop index, ii is booleaneval index */
CHAR *final_end;
CHAR *current_end;
CHAR *current_begin;
CHAR *previous_begin;
int skiplen;
char s[MAX_LINE_LEN];
CHAR c1, c2;
int printed, numprinted = 0; /* returns number of printed records if successful in matching the pattern in the object infile */
char *attrname;
int success = 0; /* do we print the stored output or not */
int count = 0;
int OLDLIMITOUTPUT = 0, OLDLIMITPERFILE = 0, OLDLIMITTOTALFILE = 0;
#if BG_DEBUG
printf("INFILE=%s\n", infile);
printf("OUTFILE\n");
sprintf(s, "exec cat %s\n", outfile);
system(s);
printf("OUTFILEDONE\n");
#endif /*BG_DEBUG*/
if ((outfp = fopen(outfile, "r")) == NULL) return 0;
if (StructuredIndex && (-1 == region_create(infile))) {
fclose(outfp);
return 0;
}
if (ComplexBoolean || ((long)GParse & AND_EXP)) {
sprintf(s, "%s/.glimpse_storeoutput.%d", TEMP_DIR, getpid());
if ((displayfp = storefp = fopen(s, "w")) == NULL) {
if (StructuredIndex) region_destroy();
fclose(outfp);
return 0;
}
}
else {
displayfp = stdout;
/* cannot come to filter_output in this case unless -a! */
}
memset(matched_terminals, '\0', num_terminals);
while ( ( (num_read = fread(filter_buf + residue, 1, BLOCKSIZE - residue, outfp)) > 0) || (residue > 0)) {
total_read += num_read;
if (num_read <= 0) {
final_end = filter_buf + residue;
num_read = residue;
residue = 0;
}
else {
num_read += residue;
final_end = (CHAR *)backward_delimiter(filter_buf + num_read, filter_buf, GD_pattern, GD_length, GOUTTAIL);
residue = filter_buf + num_read - final_end;
}
#if DEBUG
fprintf(stderr, "filter_buf=%x final_end=%x residue=%x last_chars=%c%c%c num_read=%x\n",
filter_buf, final_end, residue, *(final_end-2), *(final_end-1), *(final_end), num_read);
#endif /*DEBUG*/
current_begin = previous_begin = filter_buf;
#if 1
current_end = (CHAR *)forward_delimiter(filter_buf, filter_buf + num_read, GD_pattern, GD_length, GOUTTAIL); /* skip over prefixes like filename */
if (!GOUTTAIL) current_end = (CHAR *)forward_delimiter((long)current_end + GD_length, final_end, GD_pattern, GD_length, GOUTTAIL);
#else /*1*/
current_end = (CHAR *)forward_delimiter(filter_buf+1, final_end, GD_pattern, GD_length, GOUTTAIL);
#endif /*1*/
#if DEBUG
fprintf(stderr, "current_begin=%x current_end=%x\n", current_begin, current_end);
#endif /*DEBUG*/
while (current_end <= final_end) {
previous_begin = current_begin;
/* look for %d= */
byteoff = -1;
while (current_begin < current_end) {
if (isdigit(*current_begin)) {
skiplen = getbyteoff(current_begin, &byteoff);
#if BG_DEBUG
fprintf(debug, "byteoff=%d skiplen=%d\n", byteoff, skiplen);
#endif /*BG_DEBUG*/
if ((skiplen < 0) || (byteoff < 0)) {
current_begin ++;
continue;
}
else break;
}
else current_begin ++;
}
#if DEBUG
printf("current_begin=%x current_end=%x final_end=%x residue=%x num_read=%x\n", current_begin, current_end, final_end, residue, num_read);
#endif /*DEBUG*/
#if DEBUG
printf("byteoff=%d skiplen=%d\n", byteoff, skiplen);
#endif /*DEBUG*/
if ((skiplen < 0) || (byteoff < 0)) { /* output the whole line as it is: there is nothing to strip (e.g., -l) */
#if 0
/* This is an error: -l is now handled completely inside filter_output --> agrep won't processes it when -W */
if (!SILENT) fwrite(previous_begin, 1, current_end-previous_begin, displayfp);
numprinted ++;
#endif
}
else if ( (num_attr <= 0) || (((attribute = region_identify(byteoff, 0)) < num_attr) && (attribute >= 0)) ) {
/* prefix is from previous_begin to current_begin. Skip skiplen from current_begin. Rest until current_end is valid output */
if (num_attr <= 0) attribute = 0;
#if BG_DEBUG
fprintf(debug, "region@%d=%d\n", byteoff, attribute);
#endif /*BG_DEBUG*/
c1 = *(current_begin + skiplen - 1);
c2 = *(current_end + 1);
printed = 0;
for (i=0; i<num_terminals; i++) {
#if 0
printf("--> already_matched[%d] = %d, going to look at '%s'\n", i, matched_terminals[i], terminals[i].data.leaf.value);
#endif
if (matched_terminals[i] && (GFILENAMEONLY || FILEOUT || printed || ((LIMITOUTPUT > 0) && (numprinted >= LIMITOUTPUT)) || ((LIMITPERFILE > 0) && (numprinted >= LIMITPERFILE)))) continue;
if ((terminals[i].data.leaf.attribute == 0) || ((int)(terminals[i].data.leaf.attribute) == attribute)) {
*(current_begin + skiplen - 1) = '\n';
*(current_end + 1) = '\n';
OLDLIMITOUTPUT = LIMITOUTPUT;
LIMITOUTPUT = 0;
OLDLIMITPERFILE = LIMITPERFILE;
LIMITPERFILE = 0;
OLDLIMITTOTALFILE = LIMITTOTALFILE;
LIMITTOTALFILE = 0;
if (memagrep_search( strlen(terminals[i].data.leaf.value), terminals[i].data.leaf.value,
current_end - current_begin - skiplen + 1, current_begin + skiplen - 1,
0, nullfp) > 0) {
LIMITOUTPUT = OLDLIMITOUTPUT;
LIMITPERFILE = OLDLIMITPERFILE;
LIMITTOTALFILE = OLDLIMITTOTALFILE;
#if 0
*(current_end + 1) = '\0';
printf("--> search succeeded for %s in %s\n", terminals[i].data.leaf.value, previous_begin);
#endif /*0*/
*(current_begin + skiplen - 1) = c1;
*(current_end + 1) = c2;
matched_terminals[i] = 1; /* must reevaluate/set since don't know if it should be printed */
if (!(((LIMITOUTPUT > 0) && (numprinted >= LIMITOUTPUT)) ||
((LIMITPERFILE > 0) && (numprinted >= LIMITPERFILE))) && !printed) { /* see if it was useful later */
if (!COUNT && !FILEOUT && !SILENT) {
fwrite(previous_begin, 1, current_begin - previous_begin, displayfp);
if (PRINTATTR) fprintf(displayfp, "%s# ",
(attrname = attr_id_to_name(attribute)) == NULL ? "(null)" : attrname);
if (GBYTECOUNT) fprintf(displayfp, "%d= ", byteoff);
if (PRINTRECORD) {
fwrite(current_begin + skiplen, 1, current_end - current_begin - skiplen, displayfp);
}
else {
if (*(current_begin + skiplen) == '@') {
int iii = 0;
while (current_begin[skiplen + iii] != '}')
fputc(current_begin[skiplen + iii++], displayfp);
fputc('}', displayfp);
}
fputc('\n', displayfp);
}
}
printed = 1;
numprinted ++;
}
}
else {
LIMITOUTPUT = OLDLIMITOUTPUT;
LIMITPERFILE = OLDLIMITPERFILE;
LIMITTOTALFILE = OLDLIMITTOTALFILE;
#if 0
*(current_end + 1) = '\0';
printf("--> search failed for %s in %s\n", terminals[i].data.leaf.value, previous_begin);
#endif /*0*/
*(current_begin + skiplen - 1) = c1;
*(current_end + 1) = c2;
}
}
}
if (!success) {
if (ComplexBoolean) {
success = eval_tree(GParse, matched_terminals);
}
else {
if ((long)GParse & AND_EXP) {
success = 0;
for (ii=0; ii<num_terminals; ii++) {
if (!matched_terminals[ii]) break;
}
if (ii >= num_terminals) success = 1;
}
else {
success = 0;
/* cannot come to filter_output in this case unless -a! */
}
}
}
/* optimize options that do not need all the matched lines */
if (success) {
if (GFILENAMEONLY) {
if (GPRINTFILETIME) { /* from bug fix message by Dr Jaime Prilusky lsprilus@weizmann.weizmann.ac.il jaimep@terminator.pdb.bnl.gov */
if (!SILENT) fprintf(stdout, "%s%s\n", CurrentFileName, aprint_file_time(CurrentFileTime));
}
else {
if (!SILENT) fprintf(stdout, "%s\n", CurrentFileName);
}
if (storefp != NULL) fclose(storefp); /* don't bother to flush! */
storefp = NULL;
goto unlink_and_quit;
}
else if (FILEOUT) {
/* file_out(infile); */
if (storefp != NULL) fclose(storefp); /* don't bother to flush! */
storefp = NULL;
goto unlink_and_quit;
}
}
}
if (success && (((LIMITOUTPUT > 0) && (numprinted >= LIMITOUTPUT)) || ((LIMITPERFILE > 0) && (numprinted >= LIMITPERFILE)))) goto double_break;
if (glimpse_clientdied) goto double_break;
if (current_end >= final_end) break;
current_begin = current_end;
if (!GOUTTAIL) current_end = (CHAR *)forward_delimiter((long)current_end + GD_length, final_end, GD_pattern, GD_length, GOUTTAIL);
else current_end = (CHAR *)forward_delimiter(current_end, final_end, GD_pattern, GD_length, GOUTTAIL);
#if DEBUG
fprintf(stderr, "current_begin=%x current_end=%x\n", current_begin, current_end);
#endif /*DEBUG*/
}
if (residue > 0) {
memcpy(filter_buf, final_end, residue);
memcpy(filter_buf+residue, GD_pattern, GD_length);
}
}
double_break:
/* Come here on normal exit or when the current agrep-output is no longer of any use */
if (!success && (total_read > 0)) {
if (ComplexBoolean) {
success = eval_tree(GParse, matched_terminals);
}
else {
if ((long)GParse & AND_EXP) {
success = 0;
for (ii=0; ii<num_terminals; ii++) {
if (!matched_terminals[ii]) break;
}
if (ii >= num_terminals) success = 1;
}
else {
success = 0;
/* cannot come to filter_output in this case unless -a! */
}
}
}
/* Print the temporary output onto stdout if search was successful; unlink the temprorary file */
if (success) {
if (GFILENAMEONLY) { /* all other output options are useless since they all deal with the MATCHED line */
if (GPRINTFILETIME) { /* from bug fix message by Dr Jaime Prilusky lsprilus@weizmann.weizmann.ac.il jaimep@terminator.pdb.bnl.gov */
if (!SILENT) fprintf(stdout, "%s%s\n", CurrentFileName, aprint_file_time(CurrentFileTime));
}
else {
if (!SILENT) fprintf(stdout, "%s\n", CurrentFileName);
}
if (!SILENT) fprintf(stdout, "%s\n", CurrentFileName);
if (storefp != NULL) fclose(storefp); /* don't bother to flush! */
storefp = NULL;
}
else if (COUNT && !FILEOUT) {
if (!SILENT) {
if(!NOFILENAME) fprintf(stdout, "%s: %d\n", CurrentFileName, numprinted);
else fprintf(stdout, "%d\n", numprinted);
}
if (storefp != NULL) fclose(storefp); /* don't bother to flush! */
storefp = NULL;
}
else if (FILEOUT) {
/* file_out(infile); */
if (storefp != NULL) fclose(storefp); /* don't bother to flush! */
storefp = NULL;
}
else if (storefp != NULL) {
fflush(storefp);
fclose(storefp);
#if DEBUG
printf("STOREOUTPUT\n");
sprintf(s, "exec cat %s/.glimpse_storeoutput.%d\n", TEMP_DIR, getpid());
system(s);
#endif /*DEBUG*/
sprintf(s, "%s/.glimpse_storeoutput.%d", TEMP_DIR, getpid());
if ((storefp = fopen(s, "r")) != NULL) {
if (!SILENT) while (fgets(s, MAX_LINE_LEN, storefp) != NULL) fputs(s, stdout);
fclose(storefp);
}
storefp = NULL;
}
}
else {
if (storefp != NULL) fclose(storefp); /* else don't bother to flush */
}
unlink_and_quit:
sprintf(s, "%s/.glimpse_storeoutput.%d", TEMP_DIR, getpid());
unlink(s);
if (StructuredIndex) region_destroy();
fclose(outfp);
if (GFILENAMEONLY) {
if (numprinted > 0) return 1;
else return 0;
}
else if (ComplexBoolean || ((long)GParse & AND_EXP)) {
if (success) return numprinted;
else return 0;
} else { /* must be -a */
return numprinted;
}
}
usage()
{
fprintf(stderr, "\nThis is glimpse version %s, %s.\n\n", GLIMPSE_VERSION, GLIMPSE_DATE);
fprintf(stderr, "usage: %s [-#abceghijklnprstwxyBCDEGIMNPQSVWZ] [-d DEL] [-f FILE] [-F PAT] [-H DIR] [-J HOST] [-K PORT] [-L X[:Y:Z]] [-R X] [-T DIR] [-Y D] pattern [files]", GProgname);
fprintf(stderr, "\n");
fprintf(stderr, "List of options (see %s for more details):\n", GLIMPSE_URL);
fprintf(stderr, "\n");
fprintf(stderr, "-#: find matches with at most # errors\n");
fprintf(stderr, "-a: print attribute names (useful only for Harvest SOIF format)\n");
fprintf(stderr, "-b: print the byte offset of the record from the beginning of the file\n");
fprintf(stderr, "-B: best match mode: find the closest matches to the pattern\n");
fprintf(stderr, "-c: output the number of matched records\n");
fprintf(stderr, "-C: send queries to glimpseserver\n");
fprintf(stderr, "-d DEL: define record delimiter DEL\n");
fprintf(stderr, "-D x: adjust the cost of deletions to x\n");
fprintf(stderr, "-e: for patterns starting with -\n");
fprintf(stderr, "-E: print matching lines as they appear in the index (useful in -EQNg)\n");
fprintf(stderr, "-f FILE: restrict the search to files whose names appear in FILE\n");
fprintf(stderr, "-F PAT: restrict the search to files matching PAT\n");
fprintf(stderr, "-g: print the file number (in the index)\n");
fprintf(stderr, "-G: output the (whole) files that contain a match\n");
fprintf(stderr, "-h: do not output file names before matched record\n");
fprintf(stderr, "-H DIR: the glimpse index is located in directory DIR\n");
fprintf(stderr, "-i: case-insensitive search, e.g., 'a' = 'A'\n");
fprintf(stderr, "-I x: adjust the cost of insertions to x\n");
fprintf(stderr, "-j: output file modification dates (if -t was used for the indexing)\n");
fprintf(stderr, "-J HOST: send queries to glimpseserver at HOST\n");
fprintf(stderr, "-k: use the pattern as is (no meta characters)\n");
fprintf(stderr, "-K PORT: send queries to glimpseserver at TCP port number PORT\n");
fprintf(stderr, "-l: output only the names of files that contain a match\n");
fprintf(stderr, "-L X[:Y:Z]: limit the output to X records [Y files, Z matches per file]\n");
fprintf(stderr, "-n: output record prefixed by record number\n");
fprintf(stderr, "-N: search only the index (may not be precise for some queries) \n");
fprintf(stderr, "-o: delimiter is output at the beginning of the matched record\n");
fprintf(stderr, "-O: file names are printed only once per file\n");
fprintf(stderr, "-p FILE:off:len:endian: restrict the search to the files whose names\n\tare specified as a bit-field OR sparse-set in FILE\n");
fprintf(stderr, "-P: print the pattern that matched before the matched record\n");
fprintf(stderr, "-q: print the offsets of the beginning and end of each matched record\n");
fprintf(stderr, "-Q: (with -N) print offsets of matches from (only the large) index\n");
fprintf(stderr, "-r: (used only for agrep) - recursive search\n");
fprintf(stderr, "-R X: set the maximum size of a record to X\n");
fprintf(stderr, "-s: display nothing except error messages\n");
fprintf(stderr, "-S x: adjust the cost of substitutions to x\n");
fprintf(stderr, "-t DEL: like -d, except that the delimiter DEL appears at the end \n");
fprintf(stderr, "-T DIR: temporary files are put in directory DIR (instead of /tmp)\n");
fprintf(stderr, "-u: do not output matched records (useful in -qbug)\n");
fprintf(stderr, "-U: interpret .glimpse_filenames when -U / -X option is used in glimpseindex\n");
fprintf(stderr, "-v: (works ONLY for agrep) - output all records that do not contain a match\n");
fprintf(stderr, "-V: print the current version of glimpse\n");
fprintf(stderr, "-w: pattern has to match as a word, e.g., 'win' will not match 'wind'\n");
fprintf(stderr, "-W: the scope of Booleans is the whole file (except for structured queries)\n");
fprintf(stderr, "-x: the pattern must match the whole line\n");
fprintf(stderr, "-X: if an indexed file that matches 'pattern' doesn't exist, print its name\n");
fprintf(stderr, "-y: no prompt\n");
fprintf(stderr, "-Y D: output only matches in files that were updated in the last D days\n");
fprintf(stderr, "-z: customizable filtering using the .glimpse_filters file\n");
fprintf(stderr, "-Z: no op\n");
fprintf(stderr, "\n");
fprintf(stderr, "For questions about glimpse, please contact: `%s'\n", GLIMPSE_EMAIL);
return -1; /* useful if we make glimpse into a library */
/*
* Undocumented Option Combinations for SFS (like RPC calls)
* print file number of match instead of file name: -g
* print enclosing offsets of matched record: -q
* NOT print matched record: -u
* E.G. USAGE: -qbug (b prints offset of pattern: can also use -lg or -Ng)
* look only at index: -E
* look at matched offsets in files as seen in index (w/o searching): -QN
* E.G. USAGE: -EQNgy
* read the -EQNg or just -QNg output from stdin and perform actual search w/o
* searching the index (take hints from user): -U
* NOTE: can't use U unless QNg are all used together (e.g., BEGIN/END won't be printed)
*/
}
usageS()
{
fprintf(stderr, "\nThis is glimpse server version %s, %s.\n\n", GLIMPSE_VERSION, GLIMPSE_DATE);
fprintf(stderr, "usage: %s [-H DIR] [-J HOST] [-K PORT]", GProgname);
fprintf(stderr, "\n");
fprintf(stderr, "-H DIR: the glimpse index is located in directory DIR\n");
fprintf(stderr, "-J HOST: the host name (string) clients must use / server runs on\n");
fprintf(stderr, "-K PORT: the port (short integer) clients must use / server runs on\n");
fprintf(stderr, "\n");
fprintf(stderr, "For questions about glimpse, please contact `%s'\n", GLIMPSE_EMAIL);
return -1; /* useful if we make glimpse into a library */
}
#if CLIENTSERVER
/*
* do_select() - based on select_loop() from the Harvest Broker.
* -- Courtesy: Darren Hardy, hardy@cs.colorado.edu
*/
int do_select(sock, sec)
int sock; /* the socket to wait for */
int sec; /* the number of seconds to wait */
{
struct timeval to;
fd_set qready;
int err;
if (sock < 0 || sec < 0)
return 0;
FD_ZERO(&qready);
FD_SET(sock, &qready);
to.tv_sec = sec;
to.tv_usec = 0;
if ((err = select(sock + 1, &qready, NULL, NULL, &to)) < 0) {
if (errno == EINTR)
return 0;
perror("select");
return -1;
}
if (err == 0)
return 0;
/* If there's someone waiting to get it, let them through */
return (FD_ISSET(sock, &qready) ? 1 : 0);
}
#endif /* CLIENTSERVER */
|