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
|
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal. All Rights Reserved. */
/* multipattern matcher */
#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#ifdef ultrix
#include <sys/types.h>
#endif
#include <sys/stat.h>
#include "agrep.h"
#include <sys/time.h>
#define ddebug
#define uchar unsigned char
#undef MAXPAT
#define MAXPAT 256
#undef MAXLINE
#define MAXLINE 1024
#undef MAXSYM
#define MAXSYM 256
#define MAXMEMBER1 32768
/* #define MAXMEMBER1 262144 */ /*2^18 */
#define MAXPATFILE 600000
#define BLOCKSIZE 16384
#define MAXHASH 32768
/* #define MAXHASH 262144 */
#define mask5 32767
#define max_num MAX_DASHF_FILES
#if ISO_CHAR_SET
#define W_DELIM 256
#else
#define W_DELIM 128
#endif
#define L_DELIM 10
#define Hbits 5 /* how much to shift to perform the hash */
extern char aduplicates[MAXNUM_PAT][MAXNUM_PAT]; /* tells what other patterns are exactly equal to the i-th one */
extern char tc_aduplicates[MAXNUM_PAT][MAXNUM_PAT]; /* tells what other patterns are exactly equal to the i-th one */
extern ParseTree aterminals[MAXNUM_PAT];
extern int AComplexBoolean;
extern int LIMITOUTPUT, LIMITPERFILE;
extern int BYTECOUNT, PRINTOFFSET, PRINTRECORD, CurrentByteOffset;
extern int MULTI_OUTPUT; /* used by glimpse only if OR, never for AND */
extern int DELIMITER;
extern CHAR D_pattern[MaxDelimit*2];
extern int D_length;
extern CHAR tc_D_pattern[MaxDelimit*2];
extern int tc_D_length;
extern COUNT, FNAME, SILENT, FILENAMEONLY, prev_num_of_matched, num_of_matched, PRINTFILETIME;
extern INVERSE, OUTTAIL;
extern WORDBOUND, WHOLELINE, NOUPPER;
extern ParseTree *AParse;
extern int AComplexPattern;
extern unsigned char CurrentFileName[], Progname[];
extern long CurrentFileTime;
extern total_line;
extern agrep_initialfd;
extern int EXITONERROR;
extern int PRINTPATTERN;
extern int agrep_inlen;
extern CHAR *agrep_inbuffer;
extern FILE *agrep_finalfp;
extern int agrep_outpointer;
extern int agrep_outlen;
extern CHAR * agrep_outbuffer;
extern int errno;
extern int NEW_FILE, POST_FILTER;
extern int tuncompressible();
extern int quick_tcompress();
extern int quick_tuncompress();
extern int TCOMPRESSED;
extern int EASYSEARCH;
extern char FREQ_FILE[MAX_LINE_LEN], HASH_FILE[MAX_LINE_LEN], STRING_FILE[MAX_LINE_LEN];
extern char PAT_FILE_NAME[MAX_LINE_LEN];
uchar SHIFT1[MAXMEMBER1];
int LONG = 0;
int SHORT = 0;
int p_size= 0;
uchar tr[MAXSYM];
uchar tr1[MAXSYM];
int HASH[MAXHASH];
int Hash2[max_num];
uchar *PatPtr[max_num];
uchar *pat_spool = NULL; /* [MAXPATFILE+2*max_num+MAXPAT]; */
uchar *patt[max_num];
int pat_len[max_num];
int pat_indices[max_num]; /* pat_indices[p] gives the actual index in matched_teriminals: used only with AParse != 0 */
int num_pat;
extern char amatched_terminals[MAXNUM_PAT]; /* which patterns have been matched in the current line? Used only with AParse != 0, so max_num is not needed */
extern int anum_terminals;
extern int AComplexBoolean;
static void countline();
#if DOTCOMPRESSED
/* Equivalent variables for compression search */
uchar tc_SHIFT1[MAXMEMBER1];
int tc_LONG = 0;
int tc_SHORT = 0;
int tc_p_size= 0;
uchar tc_tr[MAXSYM];
uchar tc_tr1[MAXSYM];
int tc_HASH[MAXHASH];
int tc_Hash2[max_num];
uchar *tc_PatPtr[max_num];
uchar *tc_pat_spool = NULL; /* [MAXPATFILE+2*max_num+MAXPAT]; */
uchar *tc_patt[max_num];
int tc_pat_len[max_num];
int tc_pat_indices[max_num]; /* pat_indices[p] gives the actual index in matched_teriminals: used only with AParse != 0 */
int tc_num_pat; /* must be the same as num_pat */
#endif /*DOTCOMPRESSED*/
static void f_prep();
static void f_prep1();
static void accumulate();
#if DOTCOMPRESSED
static void tc_f_prep();
static void tc_f_prep1();
static void tc_accumulate();
#endif
#ifdef perf_check
int cshift=0, cshift0=0, chash=0;
#endif
/*
* General idea behind output processing with delimiters, inverse, compression, etc.
* CAUTION: In compressed files, we can search ONLY for simple patterns or their ;,.
* Attempts to search for complex patterns / with errors might lead to spurious matches.
* 1. Once we find the match, go back and forward to get the delimiters that surround
* the matched region.
* 2. If it is a compressed file, verify that the match is "real" (compressed files
* can have pseudo matches hence this filtering step is required).
* 3. Increment num_of_matched.
* 4. Process some output options which print stuff before the matched region is
* printed.
* 5. If there is compression, decomress and output the matched region. Otherwise
* just output it as is. Remember, from step (1) we know the matched region.
* 6. If inverse is set, then we must keep track of the end of the last matched region
* in the variable lastout. When there is a match, we must print everything from
* lastout to the beginning of the current matched region (curtextbegin) and then
* update lastout to point to the end of the current matched region (curtextend).
* ALSO: if we exit from the main loops, we must output everything from the end
* of the last matched region to the end of the input buffer.
* 7. Delimiter handling in complex patterns is different: there the search is done
* for a boolean and of the delimiter pattern and the actual pattern.
* 8. For convenience and speed, the multipattern matching routines to handle
* compressed files have been separated from their (normal) counterparts.
* 9. One special note on handling complicated boolean patterns: the parse
* tree will be the same for both compressed and uncomrpessed patterns and the
* same amatched_terminals array will be used in both. BUT, the pat_spool and
* pat_index, etc., will be different as they refer to the individual terminals.
*/
int
prepf(mfp, mbuf, mlen)
int mfp, mlen;
unsigned char *mbuf;
{
int length=0, i, p=1;
uchar *pat_ptr;
unsigned Mask = 31;
int num_read;
unsigned char *buf;
struct stat stbuf;
int j, k; /* to implement \\ */
if ((mfp == -1) && ((mbuf == NULL) || (mlen <= 0))) return -1;
if (mfp != -1) {
if (fstat(mfp, &stbuf) == -1) {
fprintf(stderr, "%s: cannot stat file: %s\n", Progname, PAT_FILE_NAME);
return -1;
}
if (!S_ISREG(stbuf.st_mode)) {
fprintf(stderr, "%s: pattern file not regular file: %s\n", Progname, PAT_FILE_NAME);
return -1;
}
if (stbuf.st_size*2 > MAXPATFILE + 2*max_num) {
fprintf(stderr, "%s: pattern file too large (> %d B): %s\n", Progname, (MAXPATFILE+2*max_num)/2, PAT_FILE_NAME);
return -1;
}
if (pat_spool != NULL) free(pat_spool);
pat_ptr = pat_spool = (unsigned char *)malloc(stbuf.st_size*2 + MAXPAT);
alloc_buf(mfp, &buf, MAXPATFILE+2*BlockSize);
while((num_read = fill_buf(mfp, buf+length, 2*BlockSize)) > 0) {
length = length + num_read;
if(length > MAXPATFILE) {
fprintf(stderr, "%s: maximum pattern file size is %d\n", Progname, MAXPATFILE);
if (!EXITONERROR) {
errno = AGREP_ERROR;
free_buf(mfp, buf);
return -1;
}
else exit(2);
}
}
}
else {
buf = mbuf;
length = mlen;
if (mlen*2 > MAXPATFILE + 2*max_num) {
fprintf(stderr, "%s: pattern buffer too large (> %d B)\n", Progname, (MAXPATFILE+2*max_num)/2);
return -1;
}
if (pat_spool != NULL) free(pat_spool);
pat_ptr = pat_spool = (unsigned char *)malloc(mlen*2 + MAXPAT);
}
/* Now all the patterns are in buf */
buf[length] = '\n';
i=0; p=1;
/* removed by Udi 11/8/94 - we now do WORDBOUND "by hand"
if(WORDBOUND) {
while(i<length) {
patt[p] = pat_ptr;
*pat_ptr++ = W_DELIM;
while((i<length) && ((*pat_ptr = buf[i++]) != '\n')) pat_ptr++;
*pat_ptr++ = W_DELIM;
*pat_ptr++ = 0;
p++;
}
}
else
*/
if(WHOLELINE) {
while(i<length) {
patt[p] = pat_ptr;
*pat_ptr++ = L_DELIM;
while((i<length) && ((*pat_ptr = buf[i++]) != '\n')) pat_ptr++;
*pat_ptr++ = L_DELIM;
*pat_ptr++ = 0;
p++;
}
}
else {
while(i < length) {
patt[p] = pat_ptr;
while((i<length) && ((*pat_ptr = buf[i++]) != '\n')) pat_ptr++;
*pat_ptr++ = 0;
p++;
}
}
/* Now, the patterns have been copied into patt[] */
if(p>max_num) {
fprintf(stderr, "%s: maximum number of patterns is %d\n", Progname, max_num);
if (!EXITONERROR) {
errno = AGREP_ERROR;
free_buf(mfp, buf);
return -1;
}
else exit(2);
}
for(i=1; i<20; i++) *pat_ptr = i; /* boundary safety zone */
/* I might have to keep changing tr s.t. mgrep won't get confused with W_DELIM */
for(i=0; i< MAXSYM; i++) tr[i] = i;
if(NOUPPER) {
for (i=0; i<MAXSYM; i++)
if (isupper(i)) tr[i] = tr[tolower(i)];
/* for(i='A'; i<= 'Z'; i++) tr[i] = i + 'a' - 'A'; */
}
/*
if(WORDBOUND) {
for(i=1; i<MAXSYM; i++) if(!isalnum(i)) tr[i] = W_DELIM;
}
removed by Udi 11/8/94 - the trick of using W-delim was too buggy.
we now do it "by hand" after we find a match
*/
for(i=0; i< MAXSYM; i++) tr1[i] = tr[i]&Mask;
num_pat = p-1;
p_size = MAXPAT;
for(i=1; i<=num_pat; i++) {
p = strlen(patt[i]);
if ((patt[i][0] == '^') || (patt[i][0] == '$')) patt[i][0] = '\n';
if ((p > 1) && ((patt[i][p-1] == '^') || (patt[i][p-1] == '$')) && (patt[i][p-2] != '\\')) patt[i][p-1] = '\n';
/* Added by bg, Dec 2nd 1994 */
for (k=0; k<p; k++) {
if (patt[i][k] == '\\') {
for (j=k; j<p; j++)
patt[i][j] = patt[i][j+1]; /* including '\0' */
p--;
}
}
pat_len[i] = p;
/*
pat_len[i] = (WORDBOUND?(p-2>0?p-2:1):p); changed by Udi 11/8/94
*/
#ifdef debug
printf("prepf(): patt[%d]=%s, pat_len[%d]=%d\n", i, patt[i], i, pat_len[i]);
#endif
if(p!=0 && p < p_size) p_size = p; /* MIN */
}
if(p_size == 0) {
fprintf(stderr, "%s: the pattern file is empty\n", Progname);
if (!EXITONERROR) {
errno = AGREP_ERROR;
free_buf(mfp, buf);
return -1;
}
else exit(2);
}
if(length > 400 && p_size > 2) LONG = 1;
if(p_size == 1) SHORT = 1;
for(i=0; i<MAXMEMBER1; i++) SHIFT1[i] = p_size - 1 - LONG;
for(i=0; i<MAXHASH; i++) {
HASH[i] = 0;
}
for(i=1; i<=num_pat; i++) f_prep(i, patt[i]);
accumulate();
memset(pat_indices, '\0', sizeof(int) * (num_pat + 1));
for(i=1; i<=num_pat; i++) f_prep1(i, patt[i]);
#if DOTCOMPRESSED
/* prepf for compression */
if (-1 == tc_prepf(buf, length)) {
free_buf(mfp, buf);
return -1;
}
#endif /*DOTCOMPRESSED*/
free_buf(mfp, buf);
acompute_duplicates(aduplicates, aterminals, anum_terminals, tr);
return 0;
}
#if DOTCOMPRESSED
/*
* Compression equivalent of prepf: called right after prepf.
* 1. Read patt and SHIFT1
* 2. Call tcompress on the patterns in patt and put in tc_patt.
* 3. Use these patterns to compute tc_SHIFT (ignore WDELIM, LDELIM, case sensitivity, etc.)
* 4. Process other variables/functions (pat_spool, tr, tr1, pat_len, accumulate, SHIFT1, f_prep, f_prep1, pat_indices) appropriately.
*/
int
tc_prepf(buf, length)
unsigned char *buf;
int length;
{
int i, p=1;
uchar *pat_ptr;
unsigned Mask = 31;
int tc_length;
unsigned char tc_buf[MAXPAT * 2]; /* maximum length of the compressed pattern */
static struct timeval initt, finalt;
if (length*2 > MAXPATFILE + 2*max_num) {
fprintf(stderr, "%s: pattern buffer too large (> %d B)\n", Progname, (MAXPATFILE+2*max_num)/2);
return -1;
}
if (tc_pat_spool != NULL) free(tc_pat_spool);
pat_ptr = tc_pat_spool = (unsigned char *)malloc(length*2 + MAXPAT);
#if MEASURE_TIMES
gettimeofday(&initt, NULL);
#endif /*MEASURE_TIMES*/
i=0; p=1;
while(i < length) {
tc_patt[p] = pat_ptr;
while((*pat_ptr = buf[i++]) != '\n') pat_ptr++;
*pat_ptr++ = 0;
if ((tc_length = quick_tcompress(FREQ_FILE, HASH_FILE, tc_patt[p], strlen(tc_patt[p]), tc_buf, MAXPAT * 2 - 8, TC_EASYSEARCH)) > 0) {
memcpy(tc_patt[p], tc_buf, tc_length);
tc_patt[p][tc_length] = '\0';
pat_ptr = tc_patt[p] + tc_length + 1; /* character after '\0' */
}
p++;
}
for(i=1; i<20; i++) *pat_ptr = i; /* boundary safety zone */
/* Ignore all other options: it is automatically W_DELIM */
for(i=0; i< MAXSYM; i++) tc_tr[i] = i;
for(i=0; i< MAXSYM; i++) tc_tr1[i] = tc_tr[i]&Mask;
tc_num_pat = p-1;
tc_p_size = MAXPAT;
for(i=1; i<=num_pat; i++) {
p = strlen(tc_patt[i]);
tc_pat_len[i] = p;
#ifdef debug
printf("prepf(): tc_patt[%d]=%s, tc_pat_len[%d]=%d\n", i, tc_patt[i], i, tc_pat_len[i]);
#endif
if(p!=0 && p < tc_p_size) tc_p_size = p; /* MIN */
}
if(tc_p_size == 0) { /* cannot happen NOW */
fprintf(stderr, "%s: the pattern file is empty\n", Progname);
if (!EXITONERROR) {
errno = AGREP_ERROR;
return -1;
}
else exit(2);
}
if(length > 400 && tc_p_size > 2) tc_LONG = 1;
if(tc_p_size == 1) tc_SHORT = 1;
for(i=0; i<MAXMEMBER1; i++) tc_SHIFT1[i] = tc_p_size - 1 - LONG;
for(i=0; i<MAXHASH; i++) {
tc_HASH[i] = 0;
}
for(i=1; i<=tc_num_pat; i++) tc_f_prep(i, tc_patt[i]);
tc_accumulate();
memset(tc_pat_indices, '\0', sizeof(int) * (tc_num_pat + 1));
for(i=1; i<=tc_num_pat; i++) tc_f_prep1(i, tc_patt[i]);
acompute_duplicates(tc_aduplicates, aterminals, anum_terminals, tc_tr);
#if MEASURE_TIMES
gettimeofday(&finalt, NULL);
INFILTER_ms += (finalt.tv_sec*1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
#endif /*MEASURE_TIMES*/
return 0;
}
#endif /*DOTCOMPRESSED*/
int
mgrep(fd)
int fd;
{
register char r_newline = '\n';
unsigned char *text;
register int buf_end, num_read, start, end, residue = 0;
int oldCurrentByteOffset;
int first_time = 1;
#if AGREP_POINTER
if (fd != -1) {
#endif /*AGREP_POINTER*/
alloc_buf(fd, &text, 2*BlockSize+Max_record);
text[Max_record-1] = '\n'; /* initial case */
start = Max_record;
while( (num_read = fill_buf(fd, text+Max_record, 2*BlockSize)) > 0)
{
buf_end = end = Max_record + num_read -1 ;
oldCurrentByteOffset = CurrentByteOffset;
if (first_time) {
if ((TCOMPRESSED == ON) && tuncompressible(text+Max_record, num_read)) {
EASYSEARCH = text[Max_record+SIGNATURE_LEN-1];
start += SIGNATURE_LEN;
CurrentByteOffset += SIGNATURE_LEN;
if (!EASYSEARCH) {
fprintf(stderr, "not compressed for easy-search: can miss some matches in: %s\n", CurrentFileName);
}
}
else TCOMPRESSED = OFF;
first_time = 0;
}
if (!DELIMITER) {
while(text[end] != r_newline && end > Max_record) end--;
text[start-1] = r_newline;
}
else {
unsigned char *newbuf = text + end + 1;
newbuf = backward_delimiter(newbuf, text+Max_record, D_pattern, D_length, OUTTAIL); /* see agrep.c/'d' */
if (newbuf < text+Max_record+D_length) newbuf = text + end + 1;
end = newbuf - text - 1;
memcpy(text+start-D_length, D_pattern, D_length);
}
residue = buf_end - end + 1 ;
if(INVERSE && COUNT) countline(text+Max_record, num_read);
/* MGREP_PROCESS */
if (TCOMPRESSED) { /* separate functions since separate globals => too many if-statements within a single function makes it slow */
#if DOTCOMPRESSED
if(tc_SHORT) { if (-1 == tc_m_short(text, start, end)) {free_buf(fd, text); return -1;}}
else { if (-1 == tc_monkey1(text, start, end)) {free_buf(fd, text); return -1;}}
#endif /*DOTCOMPRESSED*/
}
else {
if(SHORT) { if (-1 == m_short(text, start, end)) {free_buf(fd, text); return -1;}}
else { if (-1 == monkey1(text, start, end)) {free_buf(fd, text); return -1;}}
}
if(FILENAMEONLY && (num_of_matched - prev_num_of_matched) && (NEW_FILE || !POST_FILTER)) {
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s", CurrentFileName);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(fd, text);
return -1;
}
agrep_outpointer += outindex;
}
if (PRINTFILETIME) {
char *s = aprint_file_time(CurrentFileTime);
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s", s);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(s[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
}
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(fd, text);
return -1;
}
agrep_outpointer += outindex;
}
}
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "\n");
else {
if (agrep_outpointer+1>=agrep_outlen) {
OUTPUT_OVERFLOW;
free_buf(fd, text);
return -1;
}
else agrep_outbuffer[agrep_outpointer++] = '\n';
}
free_buf(fd, text);
NEW_FILE = OFF;
return 0;
}
CurrentByteOffset = oldCurrentByteOffset + end - start + 1;
start = Max_record - residue;
if(start < 0) {
start = 1;
}
strncpy(text+start, text+end, residue);
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
free_buf(fd, text);
return 0; /* done */
}
} /* end of while(num_read = ... */
if (!DELIMITER) {
text[start-1] = '\n';
text[start+residue] = '\n';
}
else {
if (start > D_length) memcpy(text+start-D_length, D_pattern, D_length);
memcpy(text+start+residue, D_pattern, D_length);
}
end = start + residue;
if(residue > 1) {
if (TCOMPRESSED) {
#if DOTCOMPRESSED
if(tc_SHORT) tc_m_short(text, start, end);
else tc_monkey1(text, start, end);
#endif /*DOTCOMPRESSED*/
}
else {
if(SHORT) m_short(text, start, end);
else monkey1(text, start, end);
}
if(FILENAMEONLY && (num_of_matched - prev_num_of_matched) && (NEW_FILE || !POST_FILTER)) {
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s", CurrentFileName);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(fd, text);
return -1;
}
agrep_outpointer += outindex;
}
if (PRINTFILETIME) {
char *s = aprint_file_time(CurrentFileTime);
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s", s);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(s[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
}
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(fd, text);
return -1;
}
agrep_outpointer += outindex;
}
}
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "\n");
else {
if (agrep_outpointer+1>=agrep_outlen) {
OUTPUT_OVERFLOW;
free_buf(fd, text);
return -1;
}
else agrep_outbuffer[agrep_outpointer++] = '\n';
}
free_buf(fd, text);
NEW_FILE = OFF;
return 0;
}
}
free_buf(fd, text);
return (0);
#if AGREP_POINTER
}
else {
text = (unsigned char *)agrep_inbuffer;
num_read = agrep_inlen;
start = 0;
buf_end = end = num_read - 1;
oldCurrentByteOffset = CurrentByteOffset;
if (first_time) {
if ((TCOMPRESSED == ON) && tuncompressible(text+Max_record, num_read)) {
EASYSEARCH = text[Max_record+SIGNATURE_LEN-1];
start += SIGNATURE_LEN;
CurrentByteOffset += SIGNATURE_LEN;
if (!EASYSEARCH) {
fprintf(stderr, "not compressed for easy-search: can miss some matches in: %s\n", CurrentFileName);
}
}
else TCOMPRESSED = OFF;
first_time = 0;
}
if (!DELIMITER)
while(text[end] != r_newline && end > 1) end--;
else {
unsigned char *newbuf = text + end + 1;
newbuf = backward_delimiter(newbuf, text, D_pattern, D_length, OUTTAIL); /* see agrep.c/'d' */
if (newbuf < text+D_length) newbuf = text + end + 1;
end = newbuf - text - 1;
}
/* text[0] = text[end] = r_newline; : the user must ensure that the delimiter is there at text[0] and occurs somewhere before text[end] */
if (INVERSE && COUNT) countline(text, num_read);
/* An exact copy of the above MGREP_PROCESS */
if (TCOMPRESSED) { /* separate functions since separate globals => too many if-statements within a single function makes it slow */
#if DOTCOMPRESSED
if(tc_SHORT) { if (-1 == tc_m_short(text, start, end)) {free_buf(fd, text); return -1;}}
else { if (-1 == tc_monkey1(text, start, end)) {free_buf(fd, text); return -1;}}
#endif /*DOTCOMPRESSED*/
}
else {
if(SHORT) { if (-1 == m_short(text, start, end)) {free_buf(fd, text); return -1;}}
else { if (-1 == monkey1(text, start, end)) {free_buf(fd, text); return -1;}}
}
if(FILENAMEONLY && (num_of_matched - prev_num_of_matched) && (NEW_FILE || !POST_FILTER)) {
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s", CurrentFileName);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(fd, text);
return -1;
}
agrep_outpointer += outindex;
}
if (PRINTFILETIME) {
char *s = aprint_file_time(CurrentFileTime);
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s", s);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(s[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
}
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(fd, text);
return -1;
}
agrep_outpointer += outindex;
}
}
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "\n");
else {
if (agrep_outpointer+1>=agrep_outlen) {
OUTPUT_OVERFLOW;
free_buf(fd, text);
return -1;
}
else agrep_outbuffer[agrep_outpointer++] = '\n';
}
free_buf(fd, text);
NEW_FILE = OFF;
return 0;
}
return 0;
}
#endif /*AGREP_POINTER*/
#ifdef perf_check
fprintf(stderr,"Shifted %d times; shift=0 %d times; hash was = %d times\n",cshift, cshift0, chash);
return 0;
#endif
} /* end mgrep */
static void
countline(text, len)
unsigned char *text; int len;
{
int i;
for (i=0; i<len; i++) if(text[i] == '\n') total_line++;
}
/* Stuff that always needs to be printed whenever there is a match in all functions in this file */
int
print_options(pat_index, text, curtextbegin, curtextend)
int pat_index;
unsigned char *text, *curtextbegin, *curtextend;
{
int PRINTED = 0;
if (SILENT) return 0;
if(FNAME && (NEW_FILE || !POST_FILTER)) {
char nextchar = (POST_FILTER == ON)?'\n':' ';
char *prevstring = (POST_FILTER == ON)?"\n":"";
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s%s", prevstring, CurrentFileName);
else {
int outindex;
if (prevstring[0] != '\0') {
if(agrep_outpointer + 1 >= agrep_outlen) {
OUTPUT_OVERFLOW;
return -1;
}
else agrep_outbuffer[agrep_outpointer ++] = prevstring[0];
}
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
OUTPUT_OVERFLOW;
return -1;
}
agrep_outpointer += outindex;
}
if (PRINTFILETIME) {
char *s = aprint_file_time(CurrentFileTime);
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s", s);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(s[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
}
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
OUTPUT_OVERFLOW;
return -1;
}
agrep_outpointer += outindex;
}
}
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, ":%c", nextchar);
else {
if (agrep_outpointer+2>= agrep_outlen) {
OUTPUT_OVERFLOW;
return -1;
}
else {
agrep_outbuffer[agrep_outpointer++] = ':';
agrep_outbuffer[agrep_outpointer++] = nextchar;
}
}
NEW_FILE = OFF;
PRINTED = 1;
}
if (PRINTPATTERN) {
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%d- ", pat_index);
else {
char s[32];
int outindex;
sprintf(s, "%d- ", pat_index);
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(s[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
}
if (s[outindex] != '\0') {
OUTPUT_OVERFLOW;
return -1;
}
agrep_outpointer += outindex;
}
PRINTED = 1;
}
if (BYTECOUNT) {
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%d= ", CurrentByteOffset);
else {
char s[32];
int outindex;
sprintf(s, "%d= ", CurrentByteOffset);
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(s[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
}
if (s[outindex] != '\0') {
OUTPUT_OVERFLOW;
return -1;
}
agrep_outpointer += outindex;
}
PRINTED = 1;
}
if (PRINTOFFSET) {
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "@%d{%d} ", CurrentByteOffset - (text -curtextbegin), curtextend-curtextbegin);
else {
char s[32];
int outindex;
sprintf(s, "@%d{%d} ", CurrentByteOffset - (text -curtextbegin), curtextend-curtextbegin);
for (outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(s[outindex] != '\0'); outindex ++) {
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
}
if (s[outindex] != '\0') {
OUTPUT_OVERFLOW;
return -1;
}
agrep_outpointer += outindex;
}
PRINTED = 1;
}
return PRINTED;
}
int
monkey1( text, start, end )
int start, end; register unsigned char *text;
{
int PRINTED = 0;
int num=0;
unsigned char *oldtext;
int pat_index;
register uchar *textend;
unsigned char *textbegin;
unsigned char *curtextend;
unsigned char *curtextbegin;
register unsigned hash;
register uchar shift;
register int m1, Long=LONG;
int MATCHED=0;
register uchar *qx;
register uchar *px;
register int p, p_end;
uchar *lastout;
/* int OUT=0; */
int hash2;
int j;
int DOWITHMASK;
DOWITHMASK = 0;
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
textbegin = text + start;
textend = text + end;
m1 = p_size-1;
lastout = text+start;
text = text + start + m1 -1 ;
/* -1 to allow match to the first \n in case the pattern has ^ in front of it */
/*
if (WORDBOUND || WHOLELINE) text = text-1;
if (WHOLELINE) text = text-1;
*/
/* to accomodate the extra 2 W_delim */
while (text <= textend) {
hash=tr1[*text];
hash=(hash<<Hbits)+(tr1[*(text-1)]);
if(Long) hash=(hash<<Hbits)+(tr1[*(text-2)]);
shift = SHIFT1[hash];
#ifdef perf_check
cshift++;
#endif
if(shift == 0) {
hash=hash&mask5;
hash2 = (tr[*(text-m1)]<<8) + tr[*(text-m1+1)];
p = HASH[hash];
#ifdef perf_check
cshift0++;
#endif
p_end = HASH[hash+1];
#ifdef debug
printf("hash=%d, p=%d, p_end=%d\n", hash, p, p_end);
#endif
while(p++ < p_end) {
if(hash2 != Hash2[p]) continue;
#ifdef perf_check
chash++;
#endif
if (((pat_index = pat_indices[p]) <= 0) || (pat_len[pat_index] <= 0)) continue;
px = PatPtr[p];
qx = text-m1;
while((*px!=0)&&(tr[*px] == tr[*qx])) {
px++;
qx++;
}
if (*px == 0) {
if(text > textend) return 0;
if (WORDBOUND) {
if (isalnum(*(unsigned char *)qx)) goto skip_output;
if (isalnum(*(unsigned char *)(text-m1-1))) goto skip_output;
}
if (!DOWITHMASK) {
/* Don't update CurrentByteOffset here: only before outputting properly */
if (!DELIMITER) {
curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
if (*curtextbegin == '\n') curtextbegin ++;
curtextend = curtextbegin /*text-m1*/; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
if (*curtextend == '\n') curtextend ++;
}
else {
curtextbegin = backward_delimiter(text, textbegin, D_pattern, D_length, OUTTAIL);
curtextend = forward_delimiter(curtextbegin /*text-m1*/, textend, D_pattern, D_length, OUTTAIL);
}
if (!OUTTAIL || INVERSE) textbegin = curtextend;
else if (DELIMITER) textbegin = curtextend - D_length;
else textbegin = curtextend - 1;
}
DOWITHMASK = 1;
if (pat_index <= anum_terminals) {
int iii;
amatched_terminals[pat_index - 1] = 1;
for (iii=0; iii<anum_terminals; iii++)
if (aduplicates[pat_index - 1][iii])
amatched_terminals[iii] = 1;
}
if (AComplexBoolean) {
/* Can output only after all the matches in the current record have been identified: just like filter_output */
oldtext = text;
CurrentByteOffset += (oldtext + pat_len[pat_index] - 1 - text);
text = oldtext + pat_len[pat_index] - 1;
MATCHED = 0;
goto skip_output;
}
else if ((long)AParse & AND_EXP) {
for (j=0; j<anum_terminals; j++) if (!amatched_terminals[j]) break;
if (j<anum_terminals) goto skip_output;
}
MATCHED=1;
oldtext = text; /* only for MULTI_OUTPUT */
#undef DO_OUTPUT
#define DO_OUTPUT(change_text)\
num_of_matched++;\
if(FILENAMEONLY || SILENT) return 0;\
if (!COUNT) {\
num ++;\
if ((PRINTED = print_options(pat_index, text, curtextbegin, curtextend)) == -1) return -1;\
if(!INVERSE) {\
if (PRINTRECORD) {\
if (agrep_finalfp != NULL) {\
fwrite(curtextbegin, 1, curtextend - curtextbegin, agrep_finalfp);\
}\
else {\
if (agrep_outpointer + curtextend - curtextbegin>= agrep_outlen) {\
OUTPUT_OVERFLOW;\
return -1;\
}\
else {\
memcpy(agrep_outbuffer + agrep_outpointer, curtextbegin, curtextend-curtextbegin);\
agrep_outpointer += curtextend - curtextbegin;\
}\
}\
}\
else if (PRINTED) {\
if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);\
else agrep_outbuffer[agrep_outpointer ++] = '\n';\
PRINTED = 0;\
}\
if ((change_text) && MULTI_OUTPUT) { /* next match starting from end of current */\
CurrentByteOffset += (oldtext + pat_len[pat_index] - 1 - text);\
text = oldtext + pat_len[pat_index] - 1;\
MATCHED = 0;\
}\
else if (change_text) {\
CurrentByteOffset += textbegin - text;\
text = textbegin;\
}\
}\
else { /* INVERSE */\
/* if(lastout < curtextbegin) OUT=1; */\
if (!SILENT) {\
if (agrep_finalfp != NULL)\
fwrite(lastout, 1, curtextbegin-lastout, agrep_finalfp);\
else {\
if (curtextbegin - lastout + agrep_outpointer >= agrep_outlen) {\
OUTPUT_OVERFLOW;\
return -1;\
}\
memcpy(agrep_outbuffer+agrep_outpointer, lastout, curtextbegin-lastout);\
agrep_outpointer += (curtextbegin-lastout);\
}\
}\
lastout=textbegin;\
if (change_text) {\
CurrentByteOffset += textbegin - text;\
text = textbegin;\
}\
}\
}\
else if (change_text) { /* COUNT */\
CurrentByteOffset += textbegin - text;\
text = textbegin;\
}\
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||\
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) return 0; /* done */\
DO_OUTPUT(1)
}
skip_output:
if (MATCHED && !MULTI_OUTPUT && !AComplexBoolean) break; /* else look for more possible matches since we never know how many will match */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
}
/* If I found some match and I am about to cross over a delimiter, then set DOWITHMASK to 0 and zero out the amatched_terminals */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
if(!MATCHED) shift = 1; /* || MULTI_OUTPUT is implicit */
else {
MATCHED = 0;
shift = m1 - 1 > 0 ? m1 - 1 : 1;
}
}
/* If I found some match and I am about to cross over a delimiter, then set DOWITHMASK to 0 and zero out the amatched_terminals */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
text += shift;
CurrentByteOffset += shift;
}
/* Do residual stuff: check if there was a match at the end of the line | check if rest of the buffer needs to be output due to inverse */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
if(INVERSE && !COUNT && (lastout <= textend)) {
if (!SILENT) {
if (agrep_finalfp != NULL) {
while(lastout <= textend) fputc(*lastout++, agrep_finalfp);
}
else {
if (textend - lastout + 1 + agrep_outpointer >= agrep_outlen) {
OUTPUT_OVERFLOW;
return -1;
}
memcpy(agrep_outbuffer+agrep_outpointer, lastout, textend-lastout+1);
agrep_outpointer += (textend-lastout+1);
lastout = textend;
}
}
}
return 0;
}
#if DOTCOMPRESSED
int
tc_monkey1( text, start, end )
int start, end;
register unsigned char *text;
{
int PRINTED = 0;
unsigned char *oldtext;
int pat_index;
register uchar *textend;
unsigned char *textbegin;
unsigned char *curtextend;
unsigned char *curtextbegin;
register unsigned hash;
register uchar shift;
register int m1, Long=LONG;
int MATCHED=0;
register uchar *qx;
register uchar *px;
register int p, p_end;
uchar *lastout;
/* int OUT=0; */
int hash2;
int j;
int DOWITHMASK;
struct timeval initt, finalt;
int newlen;
DOWITHMASK = 0;
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
textbegin = text + start;
textend = text + end;
m1 = tc_p_size-1;
lastout = text+start;
text = text + start + m1 -1;
/* -1 to allow match to the first \n in case the pattern has ^ in front of it */
/* WORDBOUND adjustment not required */
while (text <= textend) {
hash=tc_tr1[*text];
hash=(hash<<Hbits)+(tc_tr1[*(text-1)]);
if(Long) hash=(hash<<Hbits)+(tc_tr1[*(text-2)]);
shift = tc_SHIFT1[hash];
#ifdef perf_check
cshift++;
#endif
if(shift == 0) {
hash=hash&mask5;
hash2 = (tc_tr[*(text-m1)]<<8) + tc_tr[*(text-m1+1)];
p = tc_HASH[hash];
#ifdef perf_check
cshift0++;
#endif
p_end = tc_HASH[hash+1];
#ifdef debug
printf("hash=%d, p=%d, p_end=%d\n", hash, p, p_end);
#endif
while(p++ < p_end) {
if(hash2 != tc_Hash2[p]) continue;
#ifdef perf_check
chash++;
#endif
if (((pat_index = tc_pat_indices[p]) <= 0) || (tc_pat_len[pat_index] <= 0)) continue;
px = tc_PatPtr[p];
qx = text-m1;
while((*px!=0)&&(tc_tr[*px] == tc_tr[*qx])) {
px++;
qx++;
}
if (*px == 0) {
if(text > textend) return 0;
if (!DOWITHMASK) {
/* Don't update CurrentByteOffset here: only before outputting properly */
if (!DELIMITER) {
curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
if (*curtextbegin == '\n') curtextbegin ++;
curtextend = curtextbegin /*text-m1*/; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
if (*curtextend == '\n') curtextend ++;
}
else {
curtextbegin = backward_delimiter(text, textbegin, tc_D_pattern, tc_D_length, OUTTAIL);
curtextend = forward_delimiter(curtextbegin /*text-m1*/, textend, tc_D_pattern, tc_D_length, OUTTAIL);
}
}
/* else prev curtextbegin is OK: if full AND isn't found, DOWITHMASK is 0-ed so that we search at most 1 line below */
#if MEASURE_TIMES
gettimeofday(&initt, NULL);
#endif /*MEASURE_TIMES*/
/* Was it really a match in the compressed line from prev line in text to text + strlen(tc_pat_len[pat_index]? */
if (-1==exists_tcompressed_word(tc_PatPtr[p], tc_pat_len[pat_index], curtextbegin, text - curtextbegin + tc_pat_len[pat_index], EASYSEARCH))
goto skip_output;
#if MEASURE_TIMES
gettimeofday(&finalt, NULL);
FILTERALGO_ms += (finalt.tv_sec *1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
#endif /*MEASURE_TIMES*/
if (!DOWITHMASK) {
if (!OUTTAIL || INVERSE) textbegin = curtextend;
else if (DELIMITER) textbegin = curtextend - D_length;
else textbegin = curtextend - 1;
}
DOWITHMASK = 1;
if (pat_index <= anum_terminals) {
int iii;
amatched_terminals[pat_index - 1] = 1;
for (iii=0; iii<anum_terminals; iii++)
if (aduplicates[pat_index - 1][iii])
amatched_terminals[iii] = 1;
}
if (AComplexBoolean) {
/* Can output only after all the matches in the current record have been identified: just like filter_output */
oldtext = text;
CurrentByteOffset += (oldtext + pat_len[pat_index] - 1 - text);
text = oldtext + pat_len[pat_index] - 1;
MATCHED = 0;
goto skip_output;
}
else if ((long)AParse & AND_EXP) {
for (j=0; j<anum_terminals; j++) if (!amatched_terminals[j]) break;
if (j<anum_terminals) goto skip_output;
}
MATCHED=1;
oldtext = text; /* only for MULTI_OUTPUT */
#undef DO_OUTPUT
#define DO_OUTPUT(change_text)\
num_of_matched++;\
if(FILENAMEONLY || SILENT) return 0;\
if (!COUNT) {\
if ((PRINTED = print_options(pat_index, text, curtextbegin, curtextend)) == -1) return -1;\
if(!INVERSE) {\
if (PRINTRECORD) {\
/* #if MEASURE_TIMES\
gettimeofday(&initt, NULL);\
*/ /*#endif MEASURE_TIMES */\
if (agrep_finalfp != NULL)\
newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_finalfp, -1, EASYSEARCH);\
else {\
if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {\
if (newlen + agrep_outpointer >= agrep_outlen) {\
OUTPUT_OVERFLOW;\
return -1;\
}\
agrep_outpointer += newlen;\
}\
}\
/* #if MEASURE_TIMES\
gettimeofday(&finalt, NULL);\
OUTFILTER_ms += (finalt.tv_sec* 1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);\
*/ /*#endif MEASURE_TIMES */\
}\
else if (PRINTED) {\
if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);\
else agrep_outbuffer[agrep_outpointer ++] = '\n';\
PRINTED = 0;\
}\
if ((change_text) && MULTI_OUTPUT) { /* next match starting from end of current */\
CurrentByteOffset += (oldtext + tc_pat_len[pat_index] - 1 - text);\
text = oldtext + tc_pat_len[pat_index] - 1;\
MATCHED = 0;\
}\
else if (change_text) {\
CurrentByteOffset += textbegin - text;\
text = textbegin;\
}\
}\
else { /* INVERSE: Don't care about filtering time */\
/* if(lastout < curtextbegin) OUT=1; */\
if (!SILENT) {\
if (agrep_finalfp != NULL)\
newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_finalfp, -1, EASYSEARCH);\
else {\
if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {\
if (newlen + agrep_outpointer >= agrep_outlen) {\
OUTPUT_OVERFLOW;\
return -1;\
}\
agrep_outpointer += newlen;\
}\
}\
}\
lastout=textbegin;\
if (change_text) {\
CurrentByteOffset += textbegin - text;\
text = textbegin;\
}\
}\
}\
else if (change_text) {\
CurrentByteOffset += textbegin - text;\
text = textbegin;\
}\
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||\
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) return 0; /* done */\
DO_OUTPUT(1)
}
skip_output:
if (MATCHED && !MULTI_OUTPUT && !AComplexBoolean) break; /* else look for more possible matches since we never know how many will match */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
}
/* If I found some match and I am about to cross over a delimiter, then set DOWITHMASK to 0 and zero out the amatched_terminals */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
if(!MATCHED) shift = 1; /* || MULTI_OUTPUT is implicit */
else {
MATCHED = 0;
shift = m1 - 1 > 0 ? m1 - 1 : 1;
}
}
/* If I found some match and I am about to cross over a delimiter, then set DOWITHMASK to 0 and zero out the amatched_terminals */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
text += shift;
CurrentByteOffset += shift;
}
/* Do residual stuff: check if there was a match at the end of the line | check if rest of the buffer needs to be output due to inverse */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
if (INVERSE && !COUNT && (lastout <= textend)) {
if (!SILENT) {
if (agrep_finalfp != NULL)
newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_finalfp, -1, EASYSEARCH);
else {
if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
if (newlen + agrep_outpointer >= agrep_outlen) {
OUTPUT_OVERFLOW;
return -1;
}
agrep_outpointer += newlen;
}
}
}
}
return 0;
}
#endif /*DOTCOMPRESSED*/
/* shift is always 1: slight change in MATCHED semantics: it is set to 1 even if COUNT is set: previously, it wasn't set. Will it effect m_short? */
int
m_short(text, start, end)
int start, end; register uchar *text;
{
int m1=1;
int PRINTED = 0;
int pat_index;
unsigned char *oldtext;
register uchar *textend;
unsigned char *textbegin;
unsigned char *curtextend;
unsigned char *curtextbegin;
register int p, p_end;
int MATCHED=0;
/* int OUT=0; */
uchar *lastout;
uchar *qx;
uchar *px;
int j;
int DOWITHMASK;
DOWITHMASK = 0;
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
textend = text + end;
lastout = text + start;
textbegin = text + start;
text = text + start - 1 ;
/*
if (WORDBOUND || WHOLELINE) text = text-1;
*/
if (WHOLELINE) text = text-1;
/* to accomodate the extra 2 W_delim */
while (++text <= textend) {
CurrentByteOffset ++;
p = HASH[tr[*text]];
p_end = HASH[tr[*text]+1];
while(p++ < p_end) {
if (((pat_index = pat_indices[p]) <= 0) || (pat_len[pat_index] <= 0)) continue;
#ifdef debug
printf("m_short(): p=%d pat_index=%d off=%d\n", p, pat_index, textend - text);
#endif
px = PatPtr[p];
qx = text;
while((*px!=0)&&(tr[*px] == tr[*qx])) {
px++;
qx++;
}
if (*px == 0) {
if(text >= textend) return 0;
if (WORDBOUND) {
if (isalnum(*(unsigned char *)qx)) goto skip_output;
if (isalnum(*(unsigned char *)(text-1))) goto skip_output;
}
if (!DOWITHMASK) {
/* Don't update CurrentByteOffset here: only before outputting properly */
if (!DELIMITER) {
curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
if (*curtextbegin == '\n') curtextbegin ++;
curtextend = curtextbegin /*text-m1*/; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
if (*curtextend == '\n') curtextend ++;
}
else {
curtextbegin = backward_delimiter(text, textbegin, D_pattern, D_length, OUTTAIL);
curtextend = forward_delimiter(curtextbegin /*text-m1*/, textend, D_pattern, D_length, OUTTAIL);
}
if (!OUTTAIL || INVERSE) textbegin = curtextend;
else if (DELIMITER) textbegin = curtextend - D_length;
else textbegin = curtextend - 1;
}
/* else prev curtextbegin is OK: if full AND isn't found, DOWITHMASK is 0-ed so that we search at most 1 line below */
DOWITHMASK = 1;
if (pat_index <= anum_terminals) {
int iii;
amatched_terminals[pat_index - 1] = 1;
for (iii=0; iii<anum_terminals; iii++)
if (aduplicates[pat_index - 1][iii])
amatched_terminals[iii] = 1;
}
if (AComplexBoolean) {
/* Can output only after all the matches in the current record have been identified: just like filter_output */
oldtext = text;
CurrentByteOffset += (oldtext + pat_len[pat_index] - 1 - text);
text = oldtext + pat_len[pat_index] - 1;
MATCHED = 0;
goto skip_output;
}
else if ((long)AParse & AND_EXP) {
for (j=0; j<anum_terminals; j++) if (!amatched_terminals[j]) break;
if (j<anum_terminals) goto skip_output;
}
MATCHED = 1;
oldtext = text; /* used only if MULTI_OUTPUT */
#undef DO_OUTPUT
#define DO_OUTPUT(change_text)\
num_of_matched++;\
if(FILENAMEONLY || SILENT) return 0;\
if (!COUNT) {\
if ((PRINTED = print_options(pat_index, text, curtextbegin, curtextend)) == -1) return -1;\
if(!INVERSE) {\
if (PRINTRECORD) {\
if (agrep_finalfp != NULL) {\
fwrite(curtextbegin, 1, curtextend - curtextbegin, agrep_finalfp);\
}\
else {\
if (agrep_outpointer + curtextend - curtextbegin >= agrep_outlen) {\
OUTPUT_OVERFLOW;\
return -1;\
}\
else {\
memcpy(agrep_outbuffer + agrep_outpointer, curtextbegin, curtextend-curtextbegin);\
agrep_outpointer += curtextend - curtextbegin;\
}\
}\
}\
else if (PRINTED) {\
if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);\
else agrep_outbuffer[agrep_outpointer ++] = '\n';\
PRINTED = 0;\
}\
if ((change_text) && MULTI_OUTPUT) { /* next match starting from end of current */\
CurrentByteOffset += (oldtext + pat_len[pat_index] - 1 - text);\
text = oldtext + pat_len[pat_index] - 1;\
MATCHED = 0;\
}\
else if (change_text) {\
CurrentByteOffset += textbegin - text;\
text = textbegin;\
}\
}\
else {\
/* if(lastout < curtextbegin) OUT=1; */\
if (!SILENT) {\
if (agrep_finalfp != NULL)\
fwrite(lastout, 1, curtextbegin-lastout, agrep_finalfp);\
else {\
if (curtextbegin - lastout + agrep_outpointer >= agrep_outlen) {\
OUTPUT_OVERFLOW;\
return -1;\
}\
memcpy(agrep_outbuffer+agrep_outpointer, lastout, curtextbegin-lastout);\
agrep_outpointer += (curtextbegin-lastout);\
}\
}\
lastout=textbegin;\
if (change_text) {\
CurrentByteOffset += textbegin - text;\
text = textbegin;\
}\
}\
}\
else if (change_text) {\
CurrentByteOffset += textbegin - text;\
text = textbegin;\
}\
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||\
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) return 0; /* done */\
DO_OUTPUT(1)
}
skip_output:
if(MATCHED && !MULTI_OUTPUT && !AComplexBoolean) break; /* else look for more possible matches */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
}
/* If I found some match and I am about to cross over a delimiter, then set DOWITHMASK to 0 and zero out the amatched_terminals */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
if (MATCHED) text --;
MATCHED = 0;
} /* while */
CurrentByteOffset ++;
/* Do residual stuff: check if there was a match at the end of the line | check if rest of the buffer needs to be output due to inverse */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
if(INVERSE && !COUNT && (lastout <= textend)) {
if (!SILENT) {
if (agrep_finalfp != NULL) {
while(lastout <= textend) fputc(*lastout++, agrep_finalfp);
}
else {
if (textend - lastout + 1 + agrep_outpointer >= agrep_outlen) {
OUTPUT_OVERFLOW;
return -1;
}
memcpy(agrep_outbuffer+agrep_outpointer, lastout, text-lastout+1);
agrep_outpointer += (text-lastout+1);
lastout = textend;
}
}
}
return 0;
}
#if DOTCOMPRESSED
/* shift is always 1: slight change in MATCHED semantics: it is set to 1 even if COUNT is set: previously, it wasn't set. Will it effect m_short? */
int
tc_m_short(text, start, end)
int start, end; register uchar *text;
{
int m1=1;
int PRINTED = 0;
int pat_index;
unsigned char *oldtext;
register uchar *textend;
unsigned char *textbegin;
unsigned char *curtextend;
unsigned char *curtextbegin;
register int p, p_end;
int MATCHED=0;
/* int OUT=0; */
uchar *lastout;
uchar *qx;
uchar *px;
int j;
int DOWITHMASK;
struct timeval initt, finalt;
int newlen;
DOWITHMASK = 0;
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
textend = text + end;
lastout = text + start;
text = text + start - 1 ;
textbegin = text + start;
/* WORDBOUND adjustment not required */
while (++text <= textend) {
CurrentByteOffset ++;
p = tc_HASH[tc_tr[*text]];
p_end = tc_HASH[tc_tr[*text]+1];
while(p++ < p_end) {
if (((pat_index = tc_pat_indices[p]) <= 0) || (tc_pat_len[pat_index] <= 0)) continue;
#ifdef debug
printf("m_short(): p=%d pat_index=%d off=%d\n", p, pat_index, textend - text);
#endif
px = tc_PatPtr[p];
qx = text;
while((*px!=0)&&(tc_tr[*px] == tc_tr[*qx])) {
px++;
qx++;
}
if (*px == 0) {
if(text >= textend) return 0;
if (!DOWITHMASK) {
/* Don't update CurrentByteOffset here: only before outputting properly */
if (!DELIMITER) {
curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
if (*curtextbegin == '\n') curtextbegin ++;
curtextend = curtextbegin /*text-m1*/; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
if (*curtextend == '\n') curtextend ++;
}
else {
curtextbegin = backward_delimiter(text, textbegin, tc_D_pattern, tc_D_length, OUTTAIL);
curtextend = forward_delimiter(curtextbegin /*text-m1*/, textend, tc_D_pattern, tc_D_length, OUTTAIL);
}
}
/* else prev curtextbegin is OK: if full AND isn't found, DOWITHMASK is 0-ed so that we search at most 1 line below */
#if MEASURE_TIMES
gettimeofday(&initt, NULL);
#endif /*MEASURE_TIMES*/
/* Was it really a match in the compressed line from prev line in text to text + strlen(tc_pat_len[pat_index]? */
if (-1 == exists_tcompressed_word(tc_PatPtr[p], tc_pat_len[pat_index], curtextbegin, text - curtextbegin + tc_pat_len[pat_index], EASYSEARCH))
goto skip_output;
#if MEASURE_TIMES
gettimeofday(&finalt, NULL);
FILTERALGO_ms += (finalt.tv_sec *1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
#endif /*MEASURE_TIMES*/
if (!DOWITHMASK) {
if (!OUTTAIL || INVERSE) textbegin = curtextend;
else if (DELIMITER) textbegin = curtextend - D_length;
else textbegin = curtextend - 1;
}
DOWITHMASK = 1;
if (pat_index <= anum_terminals) {
int iii;
amatched_terminals[pat_index - 1] = 1;
for (iii=0; iii<anum_terminals; iii++)
if (aduplicates[pat_index - 1][iii])
amatched_terminals[iii] = 1;
}
if (AComplexBoolean) {
/* Can output only after all the matches in the current record have been identified: just like filter_output */
oldtext = text;
CurrentByteOffset += (oldtext + pat_len[pat_index] - 1 - text);
text = oldtext + pat_len[pat_index] - 1;
MATCHED = 0;
goto skip_output;
}
else if ((long)AParse & AND_EXP) {
for (j=0; j<anum_terminals; j++) if (!amatched_terminals[j]) break;
if (j<anum_terminals) goto skip_output;
}
MATCHED = 1;
oldtext = text; /* used only if MULTI_OUTPUT */
#undef DO_OUTPUT
#define DO_OUTPUT(change_text)\
num_of_matched++;\
if(FILENAMEONLY || SILENT) return 0;\
if (!COUNT) {\
if ((PRINTED = print_options(pat_index, text, curtextbegin, curtextend)) == -1) return -1;\
if(!INVERSE) {\
if (PRINTRECORD) {\
/* #if MEASURE_TIMES\
gettimeofday(&initt, NULL);\
*/ /*#endif MEASURE_TIMES*/\
if (agrep_finalfp != NULL)\
newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_finalfp, -1, EASYSEARCH);\
else {\
if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {\
if (newlen + agrep_outpointer >= agrep_outlen) {\
OUTPUT_OVERFLOW;\
return -1;\
}\
agrep_outpointer += newlen;\
}\
}\
/*#if MEASURE_TIMES\
gettimeofday(&finalt, NULL);\
OUTFILTER_ms += (finalt.tv_sec* 1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);\
*/ /*#endif MEASURE_TIMES*/\
}\
else if (PRINTED) {\
if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);\
else agrep_outbuffer[agrep_outpointer ++] = '\n';\
PRINTED = 0;\
}\
if ((change_text) && MULTI_OUTPUT) { /* next match starting from end of current */\
CurrentByteOffset += (oldtext + tc_pat_len[pat_index] - 1 - text);\
text = oldtext + tc_pat_len[pat_index] - 1;\
MATCHED = 0;\
}\
else if (change_text) {\
CurrentByteOffset += textbegin - text;\
text = textbegin;\
}\
}\
else { /* INVERSE: Don't care about filtering time */\
/* if(lastout < curtextbegin) OUT=1; */\
if (!SILENT) {\
if (agrep_finalfp != NULL)\
newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_finalfp, -1, EASYSEARCH);\
else {\
if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {\
if (newlen + agrep_outpointer >= agrep_outlen) {\
OUTPUT_OVERFLOW;\
return -1;\
}\
agrep_outpointer += newlen;\
}\
}\
}\
lastout=textbegin;\
if (change_text) {\
CurrentByteOffset += textbegin - text;\
text = textbegin;\
}\
}\
}\
else if (change_text) {\
CurrentByteOffset += textbegin - text;\
text = textbegin;\
}\
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||\
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) return 0; /* done */\
DO_OUTPUT(1)
}
skip_output:
if(MATCHED && !MULTI_OUTPUT && !AComplexBoolean) break; /* else look for more possible matches */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
}
/* If I found some match and I am about to cross over a delimiter, then set DOWITHMASK to 0 and zero out the amatched_terminals */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
if (MATCHED) text--;
MATCHED = 0;
} /* while */
CurrentByteOffset ++;
/* Do residual stuff: check if there was a match at the end of the line | check if rest of the buffer needs to be output due to inverse */
if (DOWITHMASK && (text >= curtextend - 1)) {
DOWITHMASK = 0;
if (AComplexBoolean && dd(curtextbegin, curtextend) && eval_tree(AParse, amatched_terminals)) {
DO_OUTPUT(0)
}
if (AParse != 0) memset(amatched_terminals, '\0', anum_terminals);
}
if (INVERSE && !COUNT && (lastout <= textend)) {
if (!SILENT) {
if (agrep_finalfp != NULL)
newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_finalfp, -1, EASYSEARCH);
else {
if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
if (newlen + agrep_outpointer >= agrep_outlen) {
OUTPUT_OVERFLOW;
return -1;
}
agrep_outpointer += newlen;
}
}
}
}
return 0;
}
#endif /*DOTCOMPRESSED*/
static void
f_prep(pat_index, Pattern)
uchar *Pattern; int pat_index;
{
int i, m;
register unsigned hash=0;
#ifdef debug
puts(Pattern);
#endif
m = p_size;
for (i=m-1; i>=(1+LONG); i--) {
hash = (tr1[Pattern[i]]);
hash = (hash << Hbits) + (tr1[Pattern[i-1]]);
if(LONG) hash = (hash << Hbits) + (tr1[Pattern[i-2]] );
if(SHIFT1[hash] >= m-1-i) SHIFT1[hash] = m-1-i;
}
i=m-1;
hash = (tr1[Pattern[i]]);
hash = (hash << Hbits) + (tr1[Pattern[i-1]]);
if(LONG) hash = (hash << Hbits) + (tr1[Pattern[i-2]] );
if(SHORT) hash=tr[Pattern[0]];
#ifdef debug
printf("hash = %d\n", hash);
#endif
HASH[hash]++;
return;
}
#if DOTCOMPRESSED
static void
tc_f_prep(pat_index, Pattern)
uchar *Pattern; int pat_index;
{
int i, m;
register unsigned hash=0;
#ifdef debug
puts(Pattern);
#endif
m = tc_p_size;
for (i=m-1; i>=(1+tc_LONG); i--) {
hash = (tc_tr1[Pattern[i]]);
hash = (hash << Hbits) + (tc_tr1[Pattern[i-1]]);
if(tc_LONG) hash = (hash << Hbits) + (tc_tr1[Pattern[i-2]] );
if(tc_SHIFT1[hash] >= m-1-i) tc_SHIFT1[hash] = m-1-i;
}
i=m-1;
hash = (tc_tr1[Pattern[i]]);
hash = (hash << Hbits) + (tc_tr1[Pattern[i-1]]);
if(tc_LONG) hash = (hash << Hbits) + (tc_tr1[Pattern[i-2]] );
if(tc_SHORT) hash=tc_tr[Pattern[0]];
#ifdef debug
printf("hash = %d\n", hash);
#endif
tc_HASH[hash]++;
return;
}
#endif /*DOTCOMPRESSED*/
static void
f_prep1(pat_index, Pattern)
uchar *Pattern; int pat_index;
{
int i, m;
int hash2;
register unsigned hash;
m = p_size;
#ifdef debug
puts(Pattern);
#endif
for (i=m-1; i>=(1+LONG); i--) {
hash = (tr1[Pattern[i]]);
hash = (hash << Hbits) + (tr1[Pattern[i-1]]);
if(LONG) hash = (hash << Hbits) + (tr1[Pattern[i-2]] );
if(SHIFT1[hash] >= m-1-i) SHIFT1[hash] = m-1-i;
}
i=m-1;
hash = (tr1[Pattern[i]]);
hash = (hash << Hbits) + (tr1[Pattern[i-1]]);
if(LONG) hash = (hash << Hbits) + (tr1[Pattern[i-2]] );
if(SHORT) hash=tr[Pattern[0]];
hash2 = (tr[Pattern[0]] << 8) + tr[Pattern[1]];
#ifdef debug
printf("hash = %d, HASH[hash] = %d\n", hash, HASH[hash]);
#endif
PatPtr[HASH[hash]] = Pattern;
pat_indices[HASH[hash]] = pat_index;
Hash2[HASH[hash]] = hash2;
HASH[hash]--;
return;
}
#if DOTCOMPRESSED
static void
tc_f_prep1(pat_index, Pattern)
uchar *Pattern; int pat_index;
{
int i, m;
int hash2;
register unsigned hash;
m = tc_p_size;
#ifdef debug
puts(Pattern);
#endif
for (i=m-1; i>=(1+tc_LONG); i--) {
hash = (tc_tr1[Pattern[i]]);
hash = (hash << Hbits) + (tc_tr1[Pattern[i-1]]);
if(tc_LONG) hash = (hash << Hbits) + (tc_tr1[Pattern[i-2]] );
if(tc_SHIFT1[hash] >= m-1-i) tc_SHIFT1[hash] = m-1-i;
}
i=m-1;
hash = (tc_tr1[Pattern[i]]);
hash = (hash << Hbits) + (tc_tr1[Pattern[i-1]]);
if(tc_LONG) hash = (hash << Hbits) + (tc_tr1[Pattern[i-2]] );
if(tc_SHORT) hash=tc_tr[Pattern[0]];
hash2 = (tc_tr[Pattern[0]] << 8) + tc_tr[Pattern[1]];
#ifdef debug
printf("hash = %d, tc_HASH[hash] = %d\n", hash, tc_HASH[hash]);
#endif
tc_PatPtr[tc_HASH[hash]] = Pattern;
tc_pat_indices[tc_HASH[hash]] = pat_index;
tc_Hash2[tc_HASH[hash]] = hash2;
tc_HASH[hash]--;
return;
}
#endif /*DOTCOMPRESSED*/
static void
accumulate()
{
int i;
for(i=1; i<MAXHASH; i++) {
/*
printf("%d, ", HASH[i]);
*/
HASH[i] = HASH[i-1] + HASH[i];
}
HASH[0] = 0;
return;
}
#if DOTCOMPRESSED
static void
tc_accumulate()
{
int i;
for(i=1; i<MAXHASH; i++) {
/*
printf("%d, ", HASH[i]);
*/
tc_HASH[i] = tc_HASH[i-1] + tc_HASH[i];
}
tc_HASH[0] = 0;
return;
}
#endif /*DOTCOMPRESSED*/
/* Compute duplicate strings using tr's info, not strcmp! */
int
acompute_duplicates(aduplicates, aterminals, anum_terminals, tr)
char aduplicates[MAXNUM_PAT][MAXNUM_PAT];
ParseTree aterminals[];
int anum_terminals;
char tr[256];
{
int i, j, k, leni, lenj, initk;
for (i=0; i<MAXNUM_PAT; i++) memset(aduplicates[i], '\0', MAXNUM_PAT);
for (i=0; i<anum_terminals; i++) {
leni = strlen(aterminals[i].data.leaf.value);
for (j=i; j<anum_terminals; j++) {
if (i==j) {
aduplicates[i][j] = 1;
continue;
}
lenj = strlen(aterminals[j].data.leaf.value);
if (lenj != leni) continue;
for (k=0; k<lenj; k++) {
if (tr[aterminals[i].data.leaf.value[k]] != tr[aterminals[j].data.leaf.value[k]]) break;
}
if (k < lenj) continue;
aduplicates[i][j] = 1;
aduplicates[j][i] = 1;
}
}
}
|