~ubuntu-branches/ubuntu/dapper/agrep/dapper

« back to all changes in this revision

Viewing changes to sgrep.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2005-12-27 17:01:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20051227170100-nk2hnq0bnlkbk3q3
Tags: 4.17-2
Added patch to fix FTBS on amd64 (Closes: #344909).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 1991 Sun Wu and Udi Manber.  All Rights Reserved. */
 
1
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal.  All Rights Reserved. */
2
2
#include <stdio.h>
3
3
#include <ctype.h>
 
4
#include "agrep.h"
 
5
#include <errno.h>
 
6
#undef  MAXSYM
4
7
#define MAXSYM  256
5
8
#define MAXMEMBER 8192
6
9
#define CHARTYPE        unsigned char
 
10
#undef  MaxError /* don't use agrep.h definition */
7
11
#define MaxError 20
8
12
#define MAXPATT 256
 
13
#undef  MAXLINE
9
14
#define MAXLINE 1024
 
15
#undef  MAXNAME
 
16
#define MAXNAME 256
 
17
#undef  MaxCan  /* don't use agrep.h definition */
10
18
#define MaxCan  2048
11
 
#define BLOCKSIZE    8192
 
19
#define BLOCKSIZE    16384
12
20
#define MAX_SHIFT_2  4096
 
21
#undef  ON
13
22
#define ON      1
 
23
#undef  OFF
 
24
#define OFF     0
14
25
#define LOG_ASCII 8
15
26
#define LOG_DNA  3
16
27
#define MAXMEMBER_1 65536
17
28
#define LONG_EXAC  20
18
29
#define LONG_APPX  24
 
30
#if     ISO_CHAR_SET
 
31
#define W_DELIM    256
 
32
#else
19
33
#define W_DELIM    128
20
 
 
21
 
extern COUNT, FNAME, SILENT, FILENAMEONLY, num_of_matched;
22
 
extern DNA ;  /* DNA flag is set in checksg when pattern is DNA pattern and
 
34
#endif
 
35
#include <sys/time.h>
 
36
 
 
37
extern int tuncompressible();
 
38
extern int quick_tcompress();
 
39
extern int quick_tuncompress();
 
40
 
 
41
extern int DELIMITER, OUTTAIL;
 
42
extern int D_length, tc_D_length;
 
43
extern unsigned char D_pattern[MaxDelimit *2], tc_D_pattern[MaxDelimit *2];
 
44
extern int LIMITOUTPUT, LIMITPERFILE, INVERSE;
 
45
extern int CurrentByteOffset;
 
46
extern int BYTECOUNT;
 
47
extern int PRINTOFFSET;
 
48
extern int PRINTRECORD;
 
49
extern int CONSTANT, COUNT, FNAME, SILENT, FILENAMEONLY, prev_num_of_matched, num_of_matched, PRINTFILETIME;
 
50
extern int DNA ;  /* DNA flag is set in checksg when pattern is DNA pattern and
23
51
                 p_size > 16  */
24
52
extern WORDBOUND, WHOLELINE, NOUPPER;
25
53
extern unsigned char CurrentFileName[],  Progname[]; 
 
54
extern long CurrentFileTime;
26
55
extern unsigned Mask[];
27
56
extern unsigned endposition;
 
57
extern int agrep_inlen;
 
58
extern CHARTYPE *agrep_inbuffer;
 
59
extern int agrep_initialfd;
 
60
extern FILE *agrep_finalfp;
 
61
extern int agrep_outpointer;
 
62
extern int agrep_outlen;
 
63
extern CHARTYPE * agrep_outbuffer;
 
64
 
 
65
extern int NEW_FILE, POST_FILTER;
 
66
 
 
67
extern int EXITONERROR;
 
68
extern int errno;
 
69
extern int TCOMPRESSED;
 
70
extern int EASYSEARCH;
 
71
extern char FREQ_FILE[MAX_LINE_LEN], HASH_FILE[MAX_LINE_LEN], STRING_FILE[MAX_LINE_LEN];
 
72
 
 
73
#if     MEASURE_TIMES
 
74
/* timing variables */
 
75
extern int OUTFILTER_ms;
 
76
extern int FILTERALGO_ms;
 
77
extern int INFILTER_ms;
 
78
#endif  /*MEASURE_TIMES*/
28
79
 
29
80
unsigned char BSize;                /* log_c m   */
30
81
unsigned char char_map[MAXSYM];
31
 
        
32
82
 
33
83
/* data area */
34
84
int shift_1;
39
89
char MEMBER_1[MAXMEMBER_1];
40
90
CHARTYPE TR[MAXSYM];
41
91
 
 
92
static void initmask();
 
93
static void am_preprocess();
 
94
static void m_preprocess();
 
95
static void prep();
 
96
static void prep4();
 
97
static void prep_bm();
 
98
 
 
99
/*
 
100
 * General idea behind output processing with delimiters, inverse, compression, etc.
 
101
 * CAUTION: In compressed files, we can search ONLY for simple patterns or their ;,.
 
102
 * Attempts to search for complex patterns / with errors might lead to spurious matches.
 
103
 * 1. Once we find the match, go back and forward to get the delimiters that surround
 
104
 *    the matched region.
 
105
 * 2. If it is a compressed file, verify that the match is "real" (compressed files
 
106
 *    can have pseudo matches hence this filtering step is required).
 
107
 * 3. Increment num_of_matched.
 
108
 * 4. Process some output options which print stuff before the matched region is
 
109
 *    printed.
 
110
 * 5. If there is compression, decomress and output the matched region. Otherwise
 
111
 *    just output it as is. Remember, from step (1) we know the matched region.
 
112
 * 6. If inverse is set, then we must keep track of the end of the last matched region
 
113
 *    in the variable lastout. When there is a match, we must print everything from
 
114
 *    lastout to the beginning of the current matched region (curtextbegin) and then
 
115
 *    update lastout to point to the end of the current matched region (curtextend).
 
116
 *    ALSO: if we exit from the main loops, we must output everything from the end
 
117
 *    of the last matched region to the end of the input buffer.
 
118
 * 7. Delimiter handling in complex patterns is different: there the search is done
 
119
 *    for a boolean and of the delimiter pattern and the actual pattern.
 
120
 */
 
121
 
 
122
/* skips over escaped characters */
 
123
unsigned char *
 
124
mystrchr(s, c)
 
125
unsigned char *s;
 
126
int c;
 
127
{
 
128
        unsigned char   *t = s;
 
129
 
 
130
        while (*t) {
 
131
                if (*t == '\\') t++;
 
132
                else if (c == *t) return t;
 
133
                t ++;
 
134
        }
 
135
        return NULL;
 
136
}
 
137
 
 
138
void
42
139
char_tr(pat, m)
43
 
        unsigned char *pat;
44
 
        int *m;
 
140
unsigned char *pat;
 
141
int *m;
45
142
{
46
 
int i;
47
 
unsigned char temp[MAXPATT];
 
143
        int i;
 
144
        unsigned char temp[MAXPATT];
48
145
        for(i=0; i<MAXSYM; i++) TR[i] = i;
49
146
        if(NOUPPER) {
50
 
                for(i='A'; i<= 'Z'; i++) TR[i] = i + 'a' - 'A';
 
147
                for(i=0; i<MAXSYM; i++)
 
148
                        if (isupper(i)) TR[i] = TR[tolower(i)];
 
149
                /* for(i='A'; i<= 'Z'; i++) TR[i] = i + 'a' - 'A'; */
51
150
        }
52
 
        if(WORDBOUND) { /* SUN: To be added to be more complete */
53
 
                for(i=0; i<128; i++) {
54
 
                        if(!isalnum(i)) TR[i] = W_DELIM;
 
151
        /*
 
152
        if(WORDBOUND) {
 
153
                for(i=0; i<MAXSYM; i++) {
 
154
                        if(!isalnum(i)) TR[i] = W_DELIM;removed by Udi.
 
155
                        we don't use the trick of making the boundary W_delim anymore.
 
156
                        It's too buggy otherwise and it's not necessary.
55
157
                }
56
158
        }
 
159
        removed by bg 11/8/94
 
160
        */
57
161
        if(WHOLELINE) {
58
162
                memcpy(temp, pat, *m);
59
163
                pat[0] = '\n';
64
168
        }
65
169
}
66
170
 
67
 
sgrep(pat, m, fd, D)
68
 
CHARTYPE *pat;  int fd, m, D;
69
 
70
 
    CHARTYPE text[BLOCKSIZE+2*MAXLINE+MAXPATT]; /* input text stream */
71
 
    int offset = 2*MAXLINE;
72
 
    int buf_end, num_read, i, start, end, residue = 0;
73
 
    if(pat[0] == '^' || pat[0] == '$') pat[0] = '\n';
74
 
    if(pat[m-1] == '^' || pat[m-1] == '$') pat[m-1] = '\n';
75
 
    char_tr(pat, &m);   /* will change pat, and m if WHOLELINE is ON */
76
 
    text[offset-1] = '\n';  /* initial case */
77
 
    for(i=0; i < MAXLINE; i++) text[i] = 0;   /* security zone */
78
 
    start = offset;   
79
 
    if(WHOLELINE) start--;
80
 
    if(m >= MAXPATT) {
81
 
         fprintf(stderr, "%s: pattern too long\n", Progname);
82
 
         exit(2);
83
 
    }
84
 
    if(D == 0) {
85
 
        if(m > LONG_EXAC) m_preprocess(pat);
86
 
        else prep_bm(pat, m);
87
 
    }
88
 
    else if (DNA) prep4(pat, m);
89
 
         else   if(m >= LONG_APPX) am_preprocess(pat);
90
 
                else {
91
 
                        prep(pat, m, D);
92
 
                        initmask(pat, Mask, m, 0, &endposition); 
93
 
                }
94
 
    for(i=1; i<=m; i++) text[BLOCKSIZE+offset+i] = pat[m-1];
95
 
                /* to make sure the skip loop in bm() won't go out of bound */
96
 
    while( (num_read = read(fd, text+offset, BLOCKSIZE)) > 0) 
97
 
    {
98
 
       buf_end = end = offset + num_read -1 ;
99
 
       while(text[end]  != '\n' && end > offset) end--;
100
 
       residue = buf_end - end + 1 ;
101
 
       text[start-1] = '\n';
102
 
       if(D==0)  {
103
 
                if(m > LONG_EXAC) monkey(pat, m, text+start, text+end);
104
 
                else bm(pat, m, text+start, text+end);
105
 
       }
106
 
       else {
107
 
                if(DNA) monkey4( pat, m, text+start, text+end, D  );
108
 
                else {
109
 
                  if(m >= LONG_APPX) a_monkey(pat, m, text+start, text+end, D);
110
 
                  else       agrep(pat, m, text+start, text+end, D);
111
 
                }
112
 
       }
113
 
       if(FILENAMEONLY && num_of_matched) {
114
 
            printf("%s\n", CurrentFileName);
115
 
            return; }
116
 
       start = offset - residue ;
117
 
       if(start < MAXLINE) {
118
 
            start = MAXLINE; 
119
 
       }
120
 
       strncpy(text+start, text+end, residue);
121
 
       start++;
122
 
    } /* end of while(num_read = ... */
123
 
    return;
 
171
int
 
172
sgrep(in_pat, in_m, fd, D, samepattern)
 
173
CHARTYPE *in_pat;  
 
174
int fd, in_m, D;
 
175
{
 
176
        CHARTYPE patbuf[MAXLINE];
 
177
        CHARTYPE *pat = patbuf;
 
178
        int m = in_m;
 
179
        CHARTYPE *text; /* input text stream */
 
180
        int offset = 2*Max_record;
 
181
        int buf_end, num_read, i, start, end, residue = 0;
 
182
        int first_time = 1;
 
183
        CHARTYPE *oldpat = pat;
 
184
        int k, j, oldm = m;
 
185
        static CHARTYPE newpat[MAXLINE];        /* holds compressed version */
 
186
        static int newm;
 
187
#if     MEASURE_TIMES
 
188
        static struct timeval initt, finalt;
 
189
#endif
 
190
        CHARTYPE *tempbuf;
 
191
        int     oldCurrentByteOffset;
 
192
 
 
193
        strncpy(pat, in_pat, MAXLINE);
 
194
        pat[MAXLINE-1] = '\0';
 
195
 
 
196
#define PROCESS_PATTERN \
 
197
        if (!CONSTANT) {\
 
198
                if( (pat[0] == '^') || (pat[0] == '$') ) pat[0] = '\n';\
 
199
                if ((m>1) && (pat[m-2] != '\\') && ((pat[m-1] == '^') || (pat[m-1] == '$'))) pat[m-1] = '\n';\
 
200
        }\
 
201
        /* whether constant or not, interpret the escape character */\
 
202
        for (k=0; k<m; k++) {\
 
203
                if (pat[k] == '\\') {\
 
204
                        for (j=k; j<m; j++)\
 
205
                                pat[j] = pat[j+1]; /* including '\0' */\
 
206
                        m--;\
 
207
                }\
 
208
        }\
 
209
        char_tr(pat, &m);   /* will change pat, and m if WHOLELINE is ON */\
 
210
        if(m >= MAXPATT) {\
 
211
                fprintf(stderr, "%s: pattern too long (has > %d chars)\n", Progname, MAXPATT);\
 
212
                if (!EXITONERROR) {\
 
213
                        errno = AGREP_ERROR;\
 
214
                        return -1;\
 
215
                }\
 
216
                else exit(2);\
 
217
        }\
 
218
        if(D == 0) {\
 
219
                if(m > LONG_EXAC) m_preprocess(pat);\
 
220
                else prep_bm(pat, m);\
 
221
        }\
 
222
        else if (DNA) prep4(pat, m);\
 
223
        else    if(m >= LONG_APPX) am_preprocess(pat);\
 
224
        else {\
 
225
                prep(pat, m, D);\
 
226
                initmask(pat, Mask, m, 0, &endposition);\
 
227
        }
 
228
 
 
229
#if     AGREP_POINTER
 
230
        if (fd != -1) {
 
231
#endif  /*AGREP_POINTER*/
 
232
                alloc_buf(fd, &text, 2*BlockSize+2*Max_record+MAXPATT);
 
233
                text[offset-1] = '\n';  /* initial case */
 
234
                for(i=0; i < Max_record; i++) text[i] = 0;   /* security zone */
 
235
                start = offset;   
 
236
                if(WHOLELINE) {
 
237
                        start--;
 
238
                        CurrentByteOffset --;
 
239
                }
 
240
 
 
241
                while( (num_read = fill_buf(fd, text+offset, 2*BlockSize)) > 0) 
 
242
                {
 
243
                        buf_end = end = offset + num_read -1 ;
 
244
                        oldCurrentByteOffset = CurrentByteOffset;
 
245
 
 
246
                        if (first_time) {
 
247
                                if ((TCOMPRESSED == ON) && tuncompressible(text+offset, num_read)) {
 
248
                                        EASYSEARCH = text[offset+SIGNATURE_LEN-1];
 
249
                                        start += SIGNATURE_LEN;
 
250
                                        CurrentByteOffset += SIGNATURE_LEN;
 
251
                                        if (!EASYSEARCH) {
 
252
                                                fprintf(stderr, "not compressed for easy-search: can miss some matches in: %s\n", CurrentFileName);
 
253
                                        }
 
254
#if     MEASURE_TIMES
 
255
                                        gettimeofday(&initt, NULL);
 
256
#endif  /*MEASURE_TIMES*/
 
257
                                        if (samepattern || ((newm = quick_tcompress(FREQ_FILE, HASH_FILE, pat, m, newpat, Max_record-8, EASYSEARCH)) > 0)) {
 
258
                                                oldm = m;
 
259
                                                oldpat = pat;
 
260
                                                m = newm;
 
261
                                                pat = newpat;
 
262
                                        }
 
263
#if     MEASURE_TIMES
 
264
                                        gettimeofday(&finalt, NULL);
 
265
                                        INFILTER_ms +=  (finalt.tv_sec*1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
 
266
#endif  /*MEASURE_TIMES*/
 
267
                                }
 
268
                                else TCOMPRESSED = OFF;
 
269
 
 
270
                                PROCESS_PATTERN /* must be AFTER we know that it is a compressed pattern... */
 
271
 
 
272
                                for(i=1; i<=m; i++) text[2*BlockSize+offset+i] = pat[m-1];
 
273
                                /* to make sure the skip loop in bm() won't go out of bound in later iterations */
 
274
                                first_time = 0;
 
275
                        }
 
276
 
 
277
                        if (!DELIMITER) {
 
278
                                while ((text[end]  != '\n') && (end > offset)) end--;
 
279
                                text[start-1] = '\n';
 
280
                        }
 
281
                        else {
 
282
                                unsigned char *newbuf = text + end + 1;
 
283
                                newbuf = backward_delimiter(newbuf, text+offset, D_pattern, D_length, OUTTAIL);        /* see agrep.c/'d' */
 
284
                                if (newbuf < text+offset+D_length) newbuf = text + end + 1;
 
285
                                end = newbuf - text - 1;
 
286
                                memcpy(text+start-D_length, D_pattern, D_length);
 
287
                        }
 
288
                        residue = buf_end - end + 1 ;
 
289
 
 
290
                        /* SGREP_PROCESS */
 
291
                        /* No harm in sending a few extra parameters even if they are unused: they are not accessed in monkey*()s */
 
292
                        if(D==0)  {
 
293
                                if(m > LONG_EXAC) {
 
294
                                        if (-1 == monkey(pat, m, text+start, text+end, oldpat, oldm)) {
 
295
                                                free_buf(fd, text);
 
296
                                                return -1;
 
297
                                        }
 
298
                                }
 
299
                                else {
 
300
                                        if (-1 == bm(pat, m, text+start, text+end, oldpat, oldm)) {
 
301
                                                free_buf(fd, text);
 
302
                                                return -1;
 
303
                                        }
 
304
                                }
 
305
                        }
 
306
                        else {
 
307
                                if(DNA) {
 
308
                                        if (-1 == monkey4( pat, m, text+start, text+end, D , oldpat, oldm )) {
 
309
                                                free_buf(fd, text);
 
310
                                                return -1;
 
311
                                        }
 
312
                                }
 
313
                                else {
 
314
                                        if(m >= LONG_APPX) {
 
315
                                                if (-1 == a_monkey(pat, m, text+start, text+end, D, oldpat, oldm)) {
 
316
                                                        free_buf(fd, text);
 
317
                                                        return -1;
 
318
                                                }
 
319
                                        }
 
320
                                        else {
 
321
                                                if (-1 == agrep(pat, m, text+start, text+end, D, oldpat, oldm)) {
 
322
                                                        free_buf(fd, text);
 
323
                                                        return -1;
 
324
                                                }
 
325
                                        }
 
326
                                }
 
327
                        }
 
328
                        if(FILENAMEONLY && (num_of_matched - prev_num_of_matched) && (NEW_FILE || !POST_FILTER)) {
 
329
                                if (agrep_finalfp != NULL)
 
330
                                        fprintf(agrep_finalfp, "%s", CurrentFileName);
 
331
                                else {
 
332
                                        int outindex;
 
333
                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
334
                                                        (CurrentFileName[outindex] != '\0'); outindex++) {
 
335
                                                agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
336
                                        }
 
337
                                        if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
338
                                                OUTPUT_OVERFLOW;
 
339
                                                free_buf(fd, text);
 
340
                                                return -1;
 
341
                                        }
 
342
                                        agrep_outpointer += outindex;
 
343
                                }
 
344
                                if (PRINTFILETIME) {
 
345
                                        char *s = aprint_file_time(CurrentFileTime);
 
346
                                        if (agrep_finalfp != NULL)
 
347
                                                fprintf(agrep_finalfp, "%s", s);
 
348
                                        else {
 
349
                                                int outindex;
 
350
                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
351
                                                                (s[outindex] != '\0'); outindex++) {
 
352
                                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
353
                                                }
 
354
                                                if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
355
                                                        OUTPUT_OVERFLOW;
 
356
                                                        free_buf(fd, text);
 
357
                                                        return -1;
 
358
                                                }
 
359
                                                agrep_outpointer += outindex;
 
360
                                        }
 
361
                                }
 
362
                                if (agrep_finalfp != NULL)
 
363
                                        fprintf(agrep_finalfp, "\n");
 
364
                                else {
 
365
                                        if (agrep_outpointer+1>=agrep_outlen) {
 
366
                                                OUTPUT_OVERFLOW;
 
367
                                                free_buf(fd, text);
 
368
                                                return -1;
 
369
                                        }
 
370
                                        else agrep_outbuffer[agrep_outpointer++] = '\n';
 
371
                                }
 
372
 
 
373
                                free_buf(fd, text);
 
374
                                NEW_FILE = OFF;
 
375
                                return 0; 
 
376
                        }
 
377
 
 
378
                        CurrentByteOffset = oldCurrentByteOffset + end - start + 1;     /* for a new iteration: avoid complicated calculations below */
 
379
                        start = offset - residue ;
 
380
                        if(start < Max_record) {
 
381
                                start = Max_record; 
 
382
                        }
 
383
                        /* strncpy(text+start, text+end, residue); */
 
384
                        memcpy(text+start, text+end, residue);
 
385
                        start++;
 
386
                        if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
 
387
                            ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
 
388
                                free_buf(fd, text);
 
389
                                return 0;       /* done */
 
390
                        }
 
391
                } /* end of while(num_read = ...) */
 
392
                if (!DELIMITER) {
 
393
                        text[start-1] = '\n';
 
394
                        text[start+residue] = '\n';
 
395
                }
 
396
                else {
 
397
                        if (start > D_length) memcpy(text+start-D_length, D_pattern, D_length);
 
398
                        memcpy(text+start+residue, D_pattern, D_length);
 
399
                }
 
400
                end = start + residue - 2;
 
401
                if(residue > 1) {
 
402
                        /* SGREP_PROCESS */
 
403
                        /* No harm in sending a few extra parameters even if they are unused: they are not accessed in monkey*()s */
 
404
                        if(D==0)  {
 
405
                                if(m > LONG_EXAC) {
 
406
                                        if (-1 == monkey(pat, m, text+start, text+end, oldpat, oldm)) {
 
407
                                                free_buf(fd, text);
 
408
                                                return -1;
 
409
                                        }
 
410
                                }
 
411
                                else {
 
412
                                        if (-1 == bm(pat, m, text+start, text+end, oldpat, oldm)) {
 
413
                                                free_buf(fd, text);
 
414
                                                return -1;
 
415
                                        }
 
416
                                }
 
417
                        }
 
418
                        else {
 
419
                                if(DNA) {
 
420
                                        if (-1 == monkey4( pat, m, text+start, text+end, D , oldpat, oldm )) {
 
421
                                                free_buf(fd, text);
 
422
                                                return -1;
 
423
                                        }
 
424
                                }
 
425
                                else {
 
426
                                        if(m >= LONG_APPX) {
 
427
                                                if (-1 == a_monkey(pat, m, text+start, text+end, D, oldpat, oldm)) {
 
428
                                                        free_buf(fd, text);
 
429
                                                        return -1;
 
430
                                                }
 
431
                                        }
 
432
                                        else {
 
433
                                                if (-1 == agrep(pat, m, text+start, text+end, D, oldpat, oldm)) {
 
434
                                                        free_buf(fd, text);
 
435
                                                        return -1;
 
436
                                                }
 
437
                                        }
 
438
                                }
 
439
                        }
 
440
                        if(FILENAMEONLY && (num_of_matched - prev_num_of_matched) && (NEW_FILE || !POST_FILTER)) {
 
441
                                if (agrep_finalfp != NULL)
 
442
                                        fprintf(agrep_finalfp, "%s", CurrentFileName);
 
443
                                else {
 
444
                                        int outindex;
 
445
                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
446
                                                        (CurrentFileName[outindex] != '\0'); outindex++) {
 
447
                                                agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
448
                                        }
 
449
                                        if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
450
                                                OUTPUT_OVERFLOW;
 
451
                                                free_buf(fd, text);
 
452
                                                return -1;
 
453
                                        }
 
454
                                        agrep_outpointer += outindex;
 
455
                                }
 
456
                                if (PRINTFILETIME) {
 
457
                                        char *s = aprint_file_time(CurrentFileTime);
 
458
                                        if (agrep_finalfp != NULL)
 
459
                                                fprintf(agrep_finalfp, "%s", s);
 
460
                                        else {
 
461
                                                int outindex;
 
462
                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
463
                                                                (s[outindex] != '\0'); outindex++) {
 
464
                                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
465
                                                }
 
466
                                                if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
467
                                                        OUTPUT_OVERFLOW;
 
468
                                                        free_buf(fd, text);
 
469
                                                        return -1;
 
470
                                                }
 
471
                                                agrep_outpointer += outindex;
 
472
                                        }
 
473
                                }
 
474
                                if (agrep_finalfp != NULL)
 
475
                                        fprintf(agrep_finalfp, "\n");
 
476
                                else {
 
477
                                        if (agrep_outpointer+1>=agrep_outlen) {
 
478
                                                OUTPUT_OVERFLOW;
 
479
                                                free_buf(fd, text);
 
480
                                                return -1;
 
481
                                        }
 
482
                                        else agrep_outbuffer[agrep_outpointer++] = '\n';
 
483
                                }
 
484
 
 
485
                                free_buf(fd, text);
 
486
                                NEW_FILE = OFF;
 
487
                                return 0; 
 
488
                        }
 
489
                }
 
490
                free_buf(fd, text);
 
491
                return 0;
 
492
#if     AGREP_POINTER
 
493
        }
 
494
        else {  /* as if only one iteration of the while-loop and offset = 0 */
 
495
                tempbuf = (CHARTYPE*)malloc(m);
 
496
                text = (CHARTYPE *)agrep_inbuffer;
 
497
                num_read = agrep_inlen;
 
498
                start = 0;
 
499
                buf_end = end = num_read - 1;
 
500
#if     0
 
501
                if (WHOLELINE) {
 
502
                        start --;
 
503
                        CurrentByteOffset --;
 
504
                }
 
505
#endif
 
506
                if ((TCOMPRESSED == ON) && tuncompressible(text+1, num_read)) {
 
507
                        EASYSEARCH = text[offset+SIGNATURE_LEN-1];
 
508
                        start += SIGNATURE_LEN;
 
509
                        CurrentByteOffset += SIGNATURE_LEN;
 
510
                        if (!EASYSEARCH) {
 
511
                                fprintf(stderr, "not compressed for easy-search: can miss some matches in: %s\n", CurrentFileName);
 
512
                        }
 
513
#if     MEASURE_TIMES
 
514
                        gettimeofday(&initt, NULL);
 
515
#endif  /*MEASURE_TIMES*/
 
516
                        if (samepattern || ((newm = quick_tcompress(FREQ_FILE, HASH_FILE, pat, m, newpat, Max_record-8, EASYSEARCH)) > 0)) {
 
517
                                oldm = m;
 
518
                                oldpat = pat;
 
519
                                m = newm;
 
520
                                pat = newpat;
 
521
                        }
 
522
#if     MEASURE_TIMES
 
523
                        gettimeofday(&finalt, NULL);
 
524
                        INFILTER_ms +=  (finalt.tv_sec*1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
 
525
#endif  /*MEASURE_TIMES*/
 
526
                }
 
527
                else TCOMPRESSED = OFF;
 
528
 
 
529
                PROCESS_PATTERN /* must be after we know whether it is compressed or not */
 
530
 
 
531
                memcpy(tempbuf, text+end+1, m); /* save portion being overwritten */
 
532
                for(i=1; i<=m; i++) text[end+i] = pat[m-1];
 
533
                /* to make sure the skip loop in bm() won't go out of bound in later iterations */
 
534
 
 
535
                        if (!DELIMITER)
 
536
                                while(text[end]  != '\n' && end > 1) end--;
 
537
                        else {
 
538
                                unsigned char *newbuf = text + end + 1;
 
539
                                newbuf = backward_delimiter(newbuf, text, D_pattern, D_length, OUTTAIL);        /* see agrep.c/'d' */
 
540
                                if (newbuf < text+offset+D_length) newbuf = text + end + 1;
 
541
                                end = newbuf - text - 1;
 
542
                        }
 
543
                        /* text[0] = text[end] = r_newline; : the user must ensure that the delimiter is there at text[0] and occurs somewhere before text[end ] */
 
544
 
 
545
                        /* An exact copy of the above SGREP_PROCESS */
 
546
                        /* No harm in sending a few extra parameters even if they are unused: they are not accessed in monkey*()s */
 
547
                        if(D==0)  {
 
548
                                if(m > LONG_EXAC) {
 
549
                                        if (-1 == monkey(pat, m, text+start, text+end, oldpat, oldm)) {
 
550
                                                free_buf(fd, text);
 
551
                                                memcpy(text+end+1, tempbuf, m); /* restore */
 
552
                                                free(tempbuf);
 
553
                                                return -1;
 
554
                                        }
 
555
                                }
 
556
                                else {
 
557
                                        if (-1 == bm(pat, m, text+start, text+end, oldpat, oldm)) {
 
558
                                                free_buf(fd, text);
 
559
                                                memcpy(text+end+1, tempbuf, m); /* restore */
 
560
                                                free(tempbuf);
 
561
                                                return -1;
 
562
                                        }
 
563
                                }
 
564
                        }
 
565
                        else {
 
566
                                if(DNA) {
 
567
                                        if (-1 == monkey4( pat, m, text+start, text+end, D , oldpat, oldm )) {
 
568
                                                free_buf(fd, text);
 
569
                                                memcpy(text+end+1, tempbuf, m); /* restore */
 
570
                                                free(tempbuf);
 
571
                                                return -1;
 
572
                                        }
 
573
                                }
 
574
                                else {
 
575
                                        if(m >= LONG_APPX) {
 
576
                                                if (-1 == a_monkey(pat, m, text+start, text+end, D, oldpat, oldm)) {
 
577
                                                        free_buf(fd, text);
 
578
                                                        memcpy(text+end+1, tempbuf, m); /* restore */
 
579
                                                        free(tempbuf);
 
580
                                                        return -1;
 
581
                                                }
 
582
                                        }
 
583
                                        else {
 
584
                                                if (-1 == agrep(pat, m, text+start, text+end, D, oldpat, oldm)) {
 
585
                                                        free_buf(fd, text);
 
586
                                                        memcpy(text+end+1, tempbuf, m); /* restore */
 
587
                                                        free(tempbuf);
 
588
                                                        return -1;
 
589
                                                }
 
590
                                        }
 
591
                                }
 
592
                        }
 
593
                        if(FILENAMEONLY && (num_of_matched - prev_num_of_matched) && (NEW_FILE || !POST_FILTER)) {      /* externally set */
 
594
                                if (agrep_finalfp != NULL)
 
595
                                        fprintf(agrep_finalfp, "%s", CurrentFileName);
 
596
                                else {
 
597
                                        int outindex;
 
598
                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
599
                                                        (CurrentFileName[outindex] != '\0'); outindex++) {
 
600
                                                agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
601
                                        }
 
602
                                        if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
603
                                                OUTPUT_OVERFLOW;
 
604
                                                free_buf(fd, text);
 
605
                                                return -1;
 
606
                                        }
 
607
                                        agrep_outpointer += outindex;
 
608
                                }
 
609
                                if (PRINTFILETIME) {
 
610
                                        char *s = aprint_file_time(CurrentFileTime);
 
611
                                        if (agrep_finalfp != NULL)
 
612
                                                fprintf(agrep_finalfp, "%s", s);
 
613
                                        else {
 
614
                                                int outindex;
 
615
                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
616
                                                                (s[outindex] != '\0'); outindex++) {
 
617
                                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
618
                                                }
 
619
                                                if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
620
                                                        OUTPUT_OVERFLOW;
 
621
                                                        free_buf(fd, text);
 
622
                                                        return -1;
 
623
                                                }
 
624
                                                agrep_outpointer += outindex;
 
625
                                        }
 
626
                                }
 
627
                                if (agrep_finalfp != NULL)
 
628
                                        fprintf(agrep_finalfp, "\n");
 
629
                                else {
 
630
                                        if (agrep_outpointer+1>=agrep_outlen) {
 
631
                                                OUTPUT_OVERFLOW;
 
632
                                                free_buf(fd, text);
 
633
                                                return -1;
 
634
                                        }
 
635
                                        else agrep_outbuffer[agrep_outpointer++] = '\n';
 
636
                                }
 
637
 
 
638
                                free_buf(fd, text);
 
639
                                NEW_FILE = OFF;
 
640
                        }
 
641
 
 
642
                memcpy(text+end+1, tempbuf, m); /* restore */
 
643
                free(tempbuf);
 
644
                return 0;
 
645
        }
 
646
#endif  /*AGREP_POINTER*/
124
647
} /* end sgrep */
125
648
 
126
649
/* SUN: bm assumes that the content of text[n]...text[n+m-1] is 
127
650
pat[m-1] such that the skip loop is guaranteed to terminated */
128
651
 
129
 
bm(pat, m, text, textend)
130
 
        CHARTYPE *text, *textend, *pat;  int m;
 
652
int
 
653
bm(pat, m, text, textend, oldpat, oldm)
 
654
CHARTYPE *text, *textend, *pat, *oldpat;
 
655
int m, oldm;
131
656
{
132
 
register int shift;
133
 
register int  m1, j, d1; 
134
 
 
135
 
/*
136
 
printf("%d\t", textend - text);
137
 
printf("%c, %c", *text, *textend);
138
 
*/
139
 
 
140
 
d1 = shift_1;    /* at least 1 */
141
 
m1 = m - 1;
142
 
shift = 0;       
143
 
while (text <= textend) {
144
 
        shift = SHIFT[*(text += shift)];
145
 
        while(shift) {          
146
 
                shift = SHIFT[*(text += shift)];
147
 
                shift = SHIFT[*(text += shift)];
148
 
                shift = SHIFT[*(text += shift)];
149
 
        }
 
657
        int PRINTED = 0;
 
658
        register int shift;
 
659
        register int  m1, j, d1; 
 
660
        CHARTYPE *textbegin = text;
 
661
        int newlen;
 
662
        CHARTYPE *textstart;
 
663
        CHARTYPE *curtextbegin;
 
664
        CHARTYPE *curtextend;
 
665
#if     MEASURE_TIMES
 
666
        struct timeval initt, finalt;
 
667
#endif
 
668
        CHARTYPE *lastout = text;
 
669
 
 
670
        d1 = shift_1;    /* at least 1 */
 
671
        m1 = m - 1;
 
672
        shift = 0;       
 
673
        while (text <= textend) {
 
674
                textstart = text;
 
675
                shift = SHIFT[*(text += shift)];
 
676
                while(shift) {         
 
677
                        shift = SHIFT[*(text += shift)];
 
678
                        shift = SHIFT[*(text += shift)];
 
679
                        shift = SHIFT[*(text += shift)];
 
680
                }
 
681
                CurrentByteOffset += text - textstart;
150
682
                j = 0;
151
683
                while(TR[pat[m1 - j]] == TR[*(text - j)]) {
152
 
                        if(++j == m)  break;       /* if statement can be
153
 
                                                    saved, but for safty ... */
 
684
                        if(++j == m)  break;       /* if statement can be saved, but for safty ... */
154
685
                }
155
 
                if (j == m ) { 
156
 
                        if(text > textend) return;
 
686
                if (j == m ) { 
 
687
                        if(text > textend) return 0;
157
688
                        if(WORDBOUND) {
158
 
                                if(TR[*(text+1)] != W_DELIM) goto CONT;
159
 
                                if(TR[*(text-m)] != W_DELIM) goto CONT;
160
 
                        }
 
689
                                /* if(isalnum(*(unsigned char *)(text+1))) goto CONT; --> fixed by SHIOZAKI Takehiko <takehi-s@ascii.co.jp> */
 
690
                                if((text+1 <= textend) && isalnum(*(unsigned char *)(text+1)) && isalnum(*(unsigned char *)text)) {
 
691
                                        shift = 1;      /* bg 4/27/97 */
 
692
                                        goto WCONT;     /* as if there was no match */
 
693
                                }
 
694
                                /* if(isalnum(*(unsigned char *)(text-m))) goto CONT; --> fixed by SHIOZAKI Takehiko <takehi-s@ascii.co.jp> */
 
695
                                if((textbegin <= (text-m)) && isalnum(*(unsigned char *)(text-m)) && isalnum(*(unsigned char *)(text-m+1))) {
 
696
                                        shift = 1;      /* bg 4/27/97 */
 
697
                                        goto WCONT;     /* as if there was no match */
 
698
                                }
 
699
                                /* changed by Udi 11/7/94 to avoid having to set TR[] to W_delim */
 
700
                        }
 
701
 
 
702
                        if (TCOMPRESSED == ON) {
 
703
                                /* Don't update CurrentByteOffset here: only before outputting properly */
 
704
                                if (!DELIMITER) {
 
705
                                        curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
 
706
                                        if (*curtextbegin == '\n') curtextbegin ++;
 
707
                                        curtextend = curtextbegin; /*text-m*/; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
 
708
                                        if (*curtextend == '\n') curtextend ++;
 
709
                                }
 
710
                                else {
 
711
                                        curtextbegin = backward_delimiter(text, textbegin, tc_D_pattern, tc_D_length, OUTTAIL);
 
712
                                        if (!OUTTAIL) {
 
713
                                                curtextend = forward_delimiter(curtextbegin+D_length/*text-m*/, textend, tc_D_pattern, tc_D_length, OUTTAIL);
 
714
                                        } else {
 
715
                                                curtextend = forward_delimiter(curtextbegin/*text-m*/, textend, tc_D_pattern, tc_D_length, OUTTAIL);
 
716
                                        }
 
717
                                }
 
718
                        }
 
719
                        else {
 
720
                                /* Don't update CurrentByteOffset here: only before outputting properly */
 
721
                                if (!DELIMITER) {
 
722
                                        curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
 
723
                                        if (*curtextbegin == '\n') curtextbegin ++;
 
724
                                        curtextend = curtextbegin /*text-m*/; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
 
725
                                        if (*curtextend == '\n') curtextend ++;
 
726
                                }
 
727
                                else {
 
728
                                        curtextbegin = backward_delimiter(text, textbegin, D_pattern, D_length, OUTTAIL);
 
729
                                        if (!OUTTAIL) {
 
730
                                                curtextend = forward_delimiter(curtextbegin+D_length/*text-m*/, textend, D_pattern, D_length, OUTTAIL);
 
731
                                        } else {
 
732
                                                curtextend = forward_delimiter(curtextbegin/*text-m*/, textend, D_pattern, D_length, OUTTAIL);
 
733
                                        }
 
734
                                }
 
735
                        }
 
736
 
 
737
                        if (TCOMPRESSED == ON) {
 
738
#if     MEASURE_TIMES
 
739
                                gettimeofday(&initt, NULL);
 
740
#endif  /*MEASURE_TIMES*/
 
741
                                if (-1 == exists_tcompressed_word(pat, m, curtextbegin, text - curtextbegin + m, EASYSEARCH))
 
742
                                        goto CONT;      /* as if there was no match */
 
743
#if     MEASURE_TIMES
 
744
                                gettimeofday(&finalt, NULL);
 
745
                                FILTERALGO_ms +=  (finalt.tv_sec *1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
 
746
#endif  /*MEASURE_TIMES*/
 
747
                        }
 
748
 
 
749
                        textbegin = curtextend; /* (curtextend - 1 > textbegin ? curtextend - 1 : curtextend); */
161
750
                        num_of_matched++;
162
 
                        if(FILENAMEONLY) return;
163
 
                        if(!(COUNT)) {
164
 
                                if(FNAME) printf("%s: ", CurrentFileName);
165
 
                                while(*(--text) != '\n');
166
 
                                while(*(++text) != '\n') putchar(*(text));
167
 
                                putchar(*text);
168
 
                        }
169
 
                        else { while(*text != '\n') text++; } 
 
751
                        if(FILENAMEONLY) return 0;
 
752
                        if(!COUNT) {
 
753
                                if (!INVERSE) {
 
754
                                        if(FNAME && (NEW_FILE || !POST_FILTER)) {
 
755
                                                char    nextchar = (POST_FILTER == ON)?'\n':' ';
 
756
                                                char    *prevstring = (POST_FILTER == ON)?"\n":"";
 
757
 
 
758
                                                if (agrep_finalfp != NULL)
 
759
                                                        fprintf(agrep_finalfp, "%s%s", prevstring, CurrentFileName);
 
760
                                                else {
 
761
                                                        int outindex;
 
762
                                                        if (prevstring[0] != '\0') {
 
763
                                                                if(agrep_outpointer + 1 >= agrep_outlen) {
 
764
                                                                        OUTPUT_OVERFLOW;
 
765
                                                                        return -1;
 
766
                                                                }
 
767
                                                                else agrep_outbuffer[agrep_outpointer ++] = prevstring[0];
 
768
                                                        }
 
769
                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
770
                                                                        (CurrentFileName[outindex] != '\0'); outindex++) {
 
771
                                                                agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
772
                                                        }
 
773
                                                        if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
774
                                                                OUTPUT_OVERFLOW;
 
775
                                                                return -1;
 
776
                                                        }
 
777
                                                        agrep_outpointer += outindex;
 
778
                                                }
 
779
                                                if (PRINTFILETIME) {
 
780
                                                        char *s = aprint_file_time(CurrentFileTime);
 
781
                                                        if (agrep_finalfp != NULL)
 
782
                                                                fprintf(agrep_finalfp, "%s", s);
 
783
                                                        else {
 
784
                                                                int outindex;
 
785
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
786
                                                                                (s[outindex] != '\0'); outindex++) {
 
787
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
788
                                                                }
 
789
                                                                if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
790
                                                                        OUTPUT_OVERFLOW;
 
791
                                                                        return -1;
 
792
                                                                }
 
793
                                                                agrep_outpointer += outindex;
 
794
                                                        }
 
795
                                                }
 
796
                                                if (agrep_finalfp != NULL)
 
797
                                                        fprintf(agrep_finalfp, ":%c", nextchar);
 
798
                                                else {
 
799
                                                        if (agrep_outpointer+2>= agrep_outlen) {
 
800
                                                                OUTPUT_OVERFLOW;
 
801
                                                                return -1;
 
802
                                                        }
 
803
                                                        else {
 
804
                                                                agrep_outbuffer[agrep_outpointer++] = ':';
 
805
                                                                agrep_outbuffer[agrep_outpointer++] = nextchar;
 
806
                                                        }
 
807
                                                }
 
808
 
 
809
                                                NEW_FILE = OFF;
 
810
                                                PRINTED = 1;
 
811
                                        }
 
812
 
 
813
                                        if(BYTECOUNT) {
 
814
                                                if (agrep_finalfp != NULL)
 
815
                                                        fprintf(agrep_finalfp, "%d= ", CurrentByteOffset);
 
816
                                                else {
 
817
                                                        char s[32];
 
818
                                                        int  outindex;
 
819
                                                        sprintf(s, "%d=", CurrentByteOffset);
 
820
                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
821
                                                                        (s[outindex] != '\0'); outindex++) {
 
822
                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
823
                                                        }
 
824
                                                        if (s[outindex] != '\0') {
 
825
                                                                OUTPUT_OVERFLOW;
 
826
                                                                return -1;
 
827
                                                        }
 
828
                                                        agrep_outpointer += outindex;
 
829
                                                }
 
830
                                                PRINTED = 1;
 
831
                                        }
 
832
 
 
833
                                        if (PRINTOFFSET) {
 
834
                                                if (agrep_finalfp != NULL)
 
835
                                                        fprintf(agrep_finalfp, "@%d{%d} ", CurrentByteOffset - (text -curtextbegin), curtextend-curtextbegin);
 
836
                                                else {
 
837
                                                        char s[32];
 
838
                                                        int outindex;
 
839
                                                        sprintf(s, "@%d{%d} ", CurrentByteOffset - (text -curtextbegin), curtextend-curtextbegin);
 
840
                                                        for (outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
841
                                                                         (s[outindex] != '\0'); outindex ++) {
 
842
                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
843
                                                        }
 
844
                                                        if (s[outindex] != '\0') {
 
845
                                                                OUTPUT_OVERFLOW;
 
846
                                                                return -1;
 
847
                                                        }
 
848
                                                        agrep_outpointer += outindex;
 
849
                                                }
 
850
                                                PRINTED = 1;
 
851
                                        }
 
852
 
 
853
                                        CurrentByteOffset += textbegin - text;
 
854
                                        text = textbegin;
 
855
 
 
856
 
 
857
                                        if (PRINTRECORD) {
 
858
                                        if (TCOMPRESSED == ON) {
 
859
#if     MEASURE_TIMES
 
860
                                                gettimeofday(&initt, NULL);
 
861
#endif  /*MEASURE_TIMES*/
 
862
                                                if (agrep_finalfp != NULL)
 
863
                                                        newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_finalfp, -1, EASYSEARCH);
 
864
                                                else {
 
865
                                                        if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
866
                                                                if (agrep_outpointer + newlen + 1 >= agrep_outlen) {
 
867
                                                                        OUTPUT_OVERFLOW;
 
868
                                                                        return -1;
 
869
                                                                }
 
870
                                                                agrep_outpointer += newlen;
 
871
                                                        }
 
872
                                                }
 
873
#if     MEASURE_TIMES
 
874
                                                gettimeofday(&finalt, NULL);
 
875
                                                OUTFILTER_ms +=  (finalt.tv_sec*1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
 
876
#endif  /*MEASURE_TIMES*/
 
877
                                        }
 
878
                                        else {
 
879
                                                if (agrep_finalfp != NULL) {
 
880
                                                        fwrite(curtextbegin, 1, curtextend - curtextbegin, agrep_finalfp);
 
881
                                                }
 
882
                                                else {
 
883
                                                        if (agrep_outpointer + curtextend - curtextbegin >= agrep_outlen) {
 
884
                                                                OUTPUT_OVERFLOW;
 
885
                                                                return -1;
 
886
                                                        }
 
887
                                                        memcpy(agrep_outbuffer+agrep_outpointer, curtextbegin, curtextend-curtextbegin);
 
888
                                                        agrep_outpointer += curtextend - curtextbegin;
 
889
                                                }
 
890
                                        }
 
891
                                        }
 
892
                                        else if (PRINTED) {
 
893
                                                if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);
 
894
                                                else agrep_outbuffer[agrep_outpointer ++] = '\n';
 
895
                                                PRINTED = 0;
 
896
                                        }
 
897
                                }
 
898
                                else {  /* INVERSE */
 
899
                                        if (!SILENT) {
 
900
                                        if (TCOMPRESSED == ON) { /* INVERSE: Don't care about filtering time */
 
901
                                                if (agrep_finalfp != NULL)
 
902
                                                        newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_finalfp, -1, EASYSEARCH);
 
903
                                                else {
 
904
                                                        if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
905
                                                                if (newlen + agrep_outpointer >= agrep_outlen) {
 
906
                                                                        OUTPUT_OVERFLOW;
 
907
                                                                        return -1;
 
908
                                                                }
 
909
                                                                agrep_outpointer += newlen;
 
910
                                                        }
 
911
                                                }
 
912
                                                lastout=textbegin;
 
913
                                                CurrentByteOffset += textbegin - text;
 
914
                                                text = textbegin;
 
915
                                        }
 
916
                                        else { /* NOT TCOMPRESSED */
 
917
                                                if (agrep_finalfp != NULL)
 
918
                                                        fwrite(lastout, 1, curtextbegin-lastout, agrep_finalfp);
 
919
                                                else {
 
920
                                                        if (curtextbegin - lastout + agrep_outpointer >= agrep_outlen) {
 
921
                                                                OUTPUT_OVERFLOW;
 
922
                                                                return -1;
 
923
                                                        }
 
924
                                                        memcpy(agrep_outbuffer+agrep_outpointer, lastout, curtextbegin-lastout);
 
925
                                                        agrep_outpointer += (curtextbegin - lastout);
 
926
                                                }
 
927
                                                lastout=textbegin;
 
928
                                                CurrentByteOffset += textbegin - text;
 
929
                                                text = textbegin;
 
930
                                        } /* TCOMPRESSED */
 
931
                                        } /* !SILENT */
 
932
                                } /* INVERSE */
 
933
                        }
 
934
                        else {  /* COUNT */
 
935
                                CurrentByteOffset += textbegin - text;
 
936
                                text = textbegin;
 
937
                        }
 
938
                        if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
 
939
                            ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) return 0;   /* done */
 
940
 
170
941
CONT:
171
 
                        shift = 1;
172
 
                }
 
942
                        if (m == 1) shift = 0; else shift = 1;  /* ZZZZZZZZZZZZZZZZ check it out later */
 
943
                }
173
944
                else shift = d1;
174
 
  }
175
 
return;
 
945
WCONT:  ;
 
946
        }
 
947
 
 
948
        if (!SILENT && INVERSE && !COUNT && (lastout <= textend)) {
 
949
                if (TCOMPRESSED == ON) { /* INVERSE: Don't care about filtering time */
 
950
                        if (agrep_finalfp != NULL)
 
951
                                newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_finalfp, -1, EASYSEARCH);
 
952
                        else {
 
953
                                if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
954
                                        if (newlen + agrep_outpointer >= agrep_outlen) {
 
955
                                                OUTPUT_OVERFLOW;
 
956
                                                return -1;
 
957
                                        }
 
958
                                        agrep_outpointer += newlen;
 
959
                                }
 
960
                        }
 
961
                }
 
962
                else { /* NOT TCOMPRESSED */
 
963
                        if (agrep_finalfp != NULL)
 
964
                                fwrite(lastout, 1, textend-lastout + 1, agrep_finalfp);
 
965
                        else {
 
966
                                if (textend - lastout + 1 + agrep_outpointer >= agrep_outlen) {
 
967
                                        OUTPUT_OVERFLOW;
 
968
                                        return -1;
 
969
                                }
 
970
                                memcpy(agrep_outbuffer+agrep_outpointer, lastout, textend-lastout + 1);
 
971
                                agrep_outpointer += (textend - lastout + 1);
 
972
                        }
 
973
                } /* TCOMPRESSED */
 
974
        }
 
975
 
 
976
        return 0;
176
977
}
177
978
 
178
 
  
179
979
/* initmask() initializes the mask table for the pattern                    */ 
180
980
/* endposition is a mask for the endposition of the pattern                 */
181
981
/* endposition will contain k mask bits if the pattern contains k fragments */
 
982
static void
182
983
initmask(pattern, Mask, m, D, endposition)
183
 
CHARTYPE *pattern; unsigned *Mask; register int m, D; unsigned *endposition;
 
984
CHARTYPE *pattern; 
 
985
unsigned *Mask; 
 
986
register int m, D; 
 
987
unsigned *endposition;
184
988
{
185
 
  register unsigned Bit1, c;
186
 
  register int i, j, frag_num;
 
989
        register unsigned Bit1, c;
 
990
        register int i, j, frag_num;
187
991
 
188
 
  Bit1 = 1 << 31;    /* the first bit of Bit1 is 1, others 0.  */
189
 
  frag_num = D+1; *endposition = 0;
190
 
  for (i = 0; i < frag_num; i++) *endposition = *endposition | (Bit1 >> i);
191
 
  *endposition = *endposition >> (m - frag_num);
192
 
  for(i = 0; i < m; i++) 
193
 
          if (pattern[i] == '^' || pattern[i] == '$') {
194
 
              pattern[i] = '\n'; 
195
 
          }
196
 
  for(i = 0; i < MAXSYM; i++) Mask[i] = ~0;
197
 
  for(i = 0; i < m; i++)     /* initialize the mask table */
198
 
  {  c = pattern[i];
199
 
     for ( j = 0; j < m; j++)
200
 
           if( c == pattern[j] )
201
 
               Mask[c] = Mask[c] & ~( Bit1 >> j ) ;
202
 
  }
 
992
        /* Bit1 = 1 << 31;*/    /* the first bit of Bit1 is 1, others 0.  */
 
993
        Bit1 = (unsigned)0x80000000;
 
994
        frag_num = D+1; 
 
995
        *endposition = 0;
 
996
        for (i = 0; i < frag_num; i++) *endposition = *endposition | (Bit1 >> i);
 
997
        *endposition = *endposition >> (m - frag_num);
 
998
        for(i = 0; i < m; i++) 
 
999
                if (pattern[i] == '^' || pattern[i] == '$') {
 
1000
                        pattern[i] = '\n'; 
 
1001
                }
 
1002
        for(i = 0; i < MAXSYM; i++) Mask[i] = ~0;
 
1003
        for(i = 0; i < m; i++)     /* initialize the mask table */
 
1004
        {  
 
1005
                c = pattern[i];
 
1006
                for ( j = 0; j < m; j++)
 
1007
                        if( c == pattern[j] )
 
1008
                                Mask[c] = Mask[c] & ~( Bit1 >> j ) ;
 
1009
        }
203
1010
}
204
1011
 
 
1012
static void
205
1013
prep(Pattern, M, D)             /* preprocessing for partitioning_bm */
206
 
        CHARTYPE *Pattern;  /* can be fine-tuned to choose a better partition */
207
 
        register int M, D;
 
1014
CHARTYPE *Pattern;  /* can be fine-tuned to choose a better partition */
 
1015
register int M, D;
208
1016
{
209
 
register int i, j, k, p, shift;
210
 
register unsigned m;
211
 
unsigned hash, b_size = 3;
 
1017
        register int i, j, k, p, shift;
 
1018
        register unsigned m;
 
1019
        unsigned hash, b_size = 3;
212
1020
        m = M/(D+1);
213
1021
        p = M - m*(D+1);
214
1022
        for (i = 0; i < MAXSYM; i++) SHIFT[i] = m;
215
1023
        for (i = M-1; i>=p ; i--) {
216
1024
                shift = (M-1-i)%m;
217
1025
                hash = Pattern[i];
218
 
                if(SHIFT[hash] > shift) SHIFT[hash] = shift;
 
1026
                if((int)(SHIFT[hash]) > (int)(shift)) SHIFT[hash] = shift;
219
1027
        }
220
1028
#ifdef DEBUG
221
1029
        for(i=0; i<M; i++) printf(" %d,", SHIFT[Pattern[i]]);
243
1051
                        hash = (hash << 2) + Pattern[j-k];
244
1052
                }
245
1053
#ifdef DEBUG
246
 
        printf(" hash = %d,", hash);
 
1054
                printf(" hash = %d,", hash);
247
1055
#endif
248
1056
                MEMBER[hash] = 1;
249
1057
        }
250
1058
}
251
1059
 
252
 
 
253
 
agrep( pat, M, text, textend, D ) 
254
 
int M, D ; register CHARTYPE *text, *textend, *pat;
 
1060
int
 
1061
agrep( pat, M, text, textend, D, oldpat, oldM) 
 
1062
int M, D, oldM; 
 
1063
register CHARTYPE *text, *textend, *pat, *oldpat;
255
1064
{
256
 
  register int i;
257
 
  register int m = M/(D+1);
258
 
  register CHARTYPE *textstart;
259
 
  register int shift, HASH;
260
 
  int  j=0, k, m1, d1;
261
 
  int  n, cdx;
262
 
  int  Candidate[MaxCan][2], round, lastend=0;
263
 
  unsigned R1[MaxError+1], R2[MaxError+1]; 
264
 
  register unsigned int r1, endpos, c; 
265
 
  unsigned currentpos;
266
 
  unsigned Bit1;
267
 
  unsigned r_newline;
 
1065
        register int i;
 
1066
        register int m = M/(D+1);
 
1067
        register CHARTYPE *textbegin;
 
1068
        CHARTYPE *textstart;
 
1069
        register int shift, HASH;
 
1070
        int  j=0, k, d1;
 
1071
        int  n, cdx;
 
1072
        int  Candidate[MaxCan][2], round, lastend=0;
 
1073
        unsigned R1[MaxError+1], R2[MaxError+1]; 
 
1074
        register unsigned int r1, endpos, c; 
 
1075
        unsigned currentpos;
 
1076
        unsigned Bit1;
 
1077
        unsigned r_newline;
 
1078
        int oldbyteoffset;
 
1079
        CHARTYPE *lastout = text;
 
1080
        int newlen;
268
1081
 
269
 
  Candidate[0][0] = Candidate[0][1] = 0; 
270
 
  d1 = shift_1;
271
 
  cdx = 0;
272
 
  if(m < 3) r1 = m;
273
 
  else r1 = 3;
274
 
  textstart = text;
275
 
  shift = m-1;
276
 
  while (text < textend) {
277
 
        shift = SHIFT[*(text += shift)];
278
 
        while(shift) {
279
 
                shift = SHIFT[*(text += shift)];
280
 
                shift = SHIFT[*(text += shift)];
281
 
        }
282
 
                j = 1; HASH = *text;
283
 
                while(j < r1) { HASH = (HASH << 2) + *(text-j);
284
 
                                j++; }
285
 
                if (MEMBER[HASH]) { 
286
 
                        i = text - textstart;
287
 
                        if((i - M - D - 10) > Candidate[cdx][1]) {      
 
1082
        Candidate[0][0] = Candidate[0][1] = 0; 
 
1083
        d1 = shift_1;
 
1084
        cdx = 0;
 
1085
        if(m < 3) r1 = m;
 
1086
        else r1 = 3;
 
1087
        textbegin = text;
 
1088
        shift = m-1;
 
1089
        while (text < textend) {
 
1090
                textstart = text;
 
1091
                shift = SHIFT[*(text += shift)];
 
1092
                while(shift) {
 
1093
                        shift = SHIFT[*(text += shift)];
 
1094
                        shift = SHIFT[*(text += shift)];
 
1095
                }
 
1096
                CurrentByteOffset += text - textstart;
 
1097
                j = 1; 
 
1098
                HASH = *text;
 
1099
                while(j < r1) { 
 
1100
                        HASH = (HASH << 2) + *(text-j);
 
1101
                        j++; 
 
1102
                }
 
1103
                if (MEMBER[HASH]) { 
 
1104
                        i = text - textbegin;
 
1105
                        if((i - M - D - 10) > Candidate[cdx][1]) {      
288
1106
                                Candidate[++cdx][0] = i-M-D-2;
289
 
                                Candidate[cdx][1] = i+M+D; }
290
 
                        else Candidate[cdx][1] = i+M+D;
 
1107
                                Candidate[cdx][1] = i+M+D; 
 
1108
                        }
 
1109
                        else Candidate[cdx][1] = i+M+D;
291
1110
                        shift = d1;
292
 
                }
 
1111
                }
293
1112
                else shift = d1;
294
 
  }
295
 
 
296
 
 
297
 
  text = textstart;
298
 
  n = textend - textstart;
299
 
  r_newline = '\n';
300
 
  /* for those candidate areas, find the D-error matches                     */
301
 
  if(Candidate[1][0] < 0) Candidate[1][0] = 0;
302
 
  endpos = endposition;                /* the mask table and the endposition */
303
 
  Bit1 = (1 << 31);
304
 
  for(round = 0; round <= cdx; round++)
305
 
  {  i = Candidate[round][0] ; 
306
 
     if(Candidate[round][1] > n) Candidate[round][1] = n;
307
 
     if(i < 0) i = 0;
308
 
#ifdef DEBUG
309
 
     printf("round: %d, start=%d, end=%d, ", round, i, Candidate[round][1]);
 
1113
        }
 
1114
 
 
1115
        CurrentByteOffset += (textbegin - text);
 
1116
        text = textbegin;
 
1117
        n = textend - textbegin;
 
1118
        r_newline = '\n';
 
1119
        /* for those candidate areas, find the D-error matches                     */
 
1120
        if(Candidate[1][0] < 0) Candidate[1][0] = 0;
 
1121
        endpos = endposition;                /* the mask table and the endposition */
 
1122
        /* Bit1 = (1 << 31); */
 
1123
        Bit1 = (unsigned)0x80000000;
 
1124
        oldbyteoffset = CurrentByteOffset;
 
1125
        for(round = 0; round <= cdx; round++)
 
1126
        {  
 
1127
                i = Candidate[round][0] ; 
 
1128
                if(Candidate[round][1] > n) Candidate[round][1] = n;
 
1129
                if(i < 0) i = 0;
 
1130
                CurrentByteOffset = oldbyteoffset+i;
 
1131
                R1[0] = R2[0] = ~0;
 
1132
                R1[1] = R2[1] = ~Bit1;
 
1133
                for(k = 1; k <= D; k++) R1[k] = R2[k] = (R1[k-1] >> 1) & R1[k-1];
 
1134
                while (i < Candidate[round][1])                     
 
1135
                {  
 
1136
                        c = text[i++];
 
1137
                        CurrentByteOffset ++;
 
1138
                        if(c == r_newline) {
 
1139
                                for(k = 0 ; k <= D; k++) R1[k] = R2[k] = (~0 );
 
1140
                        }
 
1141
                        r1 = Mask[c];
 
1142
                        R1[0] = (R2[0] >> 1) | r1;
 
1143
                        for(k=1; k<=D; k++)
 
1144
                                R1[k] = ((R2[k] >> 1) | r1) & R2[k-1] & ((R1[k-1] & R2[k-1]) >> 1);
 
1145
                        if((R1[D] & endpos) == 0) { 
 
1146
                                num_of_matched++;
 
1147
                                if(FILENAMEONLY) return 0; 
 
1148
                                currentpos = i;
 
1149
                                if(i <= lastend) {
 
1150
                                        CurrentByteOffset += lastend - i;
 
1151
                                        i = lastend;
 
1152
                                }
 
1153
                                else {
 
1154
                                        int oldcurrentpos = currentpos;
 
1155
                                        if (-1 == s_output(text, &currentpos, textbegin, textend, &lastout, pat, M, oldpat, oldM)) return -1;
 
1156
                                        CurrentByteOffset += currentpos - oldcurrentpos;
 
1157
                                        i = currentpos; 
 
1158
                                }
 
1159
                                lastend = i;
 
1160
                                for(k=0; k<=D; k++) R1[k] = R2[k] = ~0;
 
1161
                                if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
 
1162
                                    ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) return 0;   /* done */
 
1163
                        }
 
1164
 
 
1165
                        /* copying the code to save a few instructions.
 
1166
                        you need to understand the shift-or algorithm
 
1167
                        to figure this one... */
 
1168
 
 
1169
                        c = text[i++];
 
1170
                        CurrentByteOffset ++;
 
1171
                        if(c == r_newline) {
 
1172
                                for(k = 0 ; k <= D; k++) R1[k] = R2[k] = (~0 );
 
1173
                        }
 
1174
                        r1 = Mask[c];
 
1175
                        R2[0] = (R1[0] >> 1) | r1;
 
1176
                        for(k = 1; k <= D; k++)
 
1177
                                R2[k] = ((R1[k] >> 1) | r1) & R1[k-1] & ((R1[k-1] & R2[k-1]) >> 1);
 
1178
                        if((R2[D] & endpos) == 0) { 
 
1179
                                currentpos = i;
 
1180
                                num_of_matched++;
 
1181
                                if(FILENAMEONLY) return 0; 
 
1182
                                if(i <= lastend) {
 
1183
                                        CurrentByteOffset += lastend - i;
 
1184
                                        i = lastend;
 
1185
                                }
 
1186
                                else {
 
1187
                                        int oldcurrentpos = currentpos;
 
1188
                                        if (-1 == s_output(text, &currentpos, textbegin, textend, &lastout, pat, M, oldpat, oldM)) return -1;
 
1189
                                        CurrentByteOffset += currentpos - oldcurrentpos;
 
1190
                                        i = currentpos; 
 
1191
                                }
 
1192
                                lastend = i;
 
1193
                                for(k=0; k<=D; k++) R1[k] = R2[k] = ~0;
 
1194
                                if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
 
1195
                                    ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) return 0;   /* done */
 
1196
                        }
 
1197
                }
 
1198
        }
 
1199
 
 
1200
 
 
1201
        if (!SILENT && INVERSE && !COUNT && (lastout <= textend)) {
 
1202
                if (TCOMPRESSED == ON) { /* INVERSE: Don't care about filtering time */
 
1203
                        if (agrep_finalfp != NULL)
 
1204
                                newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_finalfp, -1, EASYSEARCH);
 
1205
                        else {
 
1206
                                if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
1207
                                        if (newlen + agrep_outpointer >= agrep_outlen) {
 
1208
                                                OUTPUT_OVERFLOW;
 
1209
                                                return -1;
 
1210
                                        }
 
1211
                                        agrep_outpointer += newlen;
 
1212
                                }
 
1213
                        }
 
1214
                }
 
1215
                else { /* NOT TCOMPRESSED */
 
1216
                        if (agrep_finalfp != NULL)
 
1217
                                fwrite(lastout, 1, textend-lastout + 1, agrep_finalfp);
 
1218
                        else {
 
1219
                                if (textend - lastout + 1 + agrep_outpointer >= agrep_outlen) {
 
1220
                                        OUTPUT_OVERFLOW;
 
1221
                                        return -1;
 
1222
                                }
 
1223
                                memcpy(agrep_outbuffer+agrep_outpointer, lastout, textend-lastout + 1);
 
1224
                                agrep_outpointer += (textend - lastout + 1);
 
1225
                        }
 
1226
                } /* TCOMPRESSED */
 
1227
        }
 
1228
 
 
1229
        return 0;
 
1230
}
 
1231
 
 
1232
/* Don't update CurrentByteOffset here: done by caller */
 
1233
int
 
1234
s_output(text, i, textbegin, textend, lastout, pat, m, oldpat, oldm) 
 
1235
int *i; /* in, out */
 
1236
int m, oldm; 
 
1237
CHARTYPE *text, *textbegin, *textend, *pat, *oldpat;
 
1238
CHARTYPE **lastout;     /* in, out */
 
1239
{
 
1240
        int PRINTED = 0;
 
1241
        int newlen; 
 
1242
        int oldi;
 
1243
        CHARTYPE *curtextbegin;
 
1244
        CHARTYPE *curtextend;
 
1245
#if     MEASURE_TIMES
 
1246
        struct timeval initt, finalt;
310
1247
#endif
311
 
     R1[0] = R2[0] = ~0;
312
 
     R1[1] = R2[1] = ~Bit1;
313
 
     for(k = 1; k <= D; k++) R1[k] = R2[k] = (R1[k-1] >> 1) & R1[k-1];
314
 
     while (i < Candidate[round][1])                     
315
 
     {  
316
 
            c = text[i++];
317
 
            if(c == r_newline) {
318
 
               for(k = 0 ; k <= D; k++) R1[k] = R2[k] = (~0 );
319
 
            }
320
 
            r1 = Mask[c];
321
 
            R1[0] = (R2[0] >> 1) | r1;
322
 
            for(k=1; k<=D; k++)
323
 
                R1[k] = ((R2[k] >> 1) | r1) & R2[k-1] & ((R1[k-1] & R2[k-1]) >> 1);
324
 
            if((R1[D] & endpos) == 0) { 
325
 
                                    num_of_matched++;
326
 
                                    if(FILENAMEONLY) { return; }
327
 
                                    currentpos = i;
328
 
                                    if(i <= lastend) i = lastend;
329
 
                                    else {
330
 
                                       s_output(text, &currentpos); 
331
 
                                       i = currentpos; 
332
 
                                    }
333
 
                                    lastend = i;
334
 
                                    for(k=0; k<=D; k++) R1[k] = R2[k] = ~0;
335
 
                                  }
336
 
            c = text[i++];
337
 
            if(c == r_newline) {
338
 
                for(k = 0 ; k <= D; k++) R1[k] = R2[k] = (~0 );
339
 
            }
340
 
            r1 = Mask[c];
341
 
            R2[0] = (R1[0] >> 1) | r1;
342
 
            for(k = 1; k <= D; k++)
343
 
                R2[k] = ((R1[k] >> 1) | r1) & R1[k-1] & ((R1[k-1] & R2[k-1]) >> 1);
344
 
            if((R2[D] & endpos) == 0) { currentpos = i;
345
 
                                    num_of_matched++;
346
 
                                    if(FILENAMEONLY) { return; }
347
 
                                    if(i <= lastend) i = lastend;
348
 
                                    else {
349
 
                                       s_output(text, &currentpos); 
350
 
                                       i = currentpos; 
351
 
                                    }
352
 
                                    lastend = i;
353
 
                                    for(k=0; k<=D; k++) R1[k] = R2[k] = ~0;
354
 
                                  }
355
 
     }
356
 
  }
357
 
  return;
358
 
}
359
 
 
360
 
s_output (text, i) 
361
 
int *i; CHARTYPE *text;
362
 
{
363
 
int kk, bp;
364
 
        if(SILENT) return;
365
 
        if(COUNT) {
366
 
                while(text[*i] != '\n') *i = *i + 1; 
367
 
                return;
368
 
        }
369
 
        if(FNAME == ON) printf("%s: ", CurrentFileName);
370
 
        bp = *i;
371
 
        while(text[--bp] != '\n');
372
 
        while(text[++bp] != '\n') putchar(text[bp]);
373
 
        putchar('\n');
374
 
        *i = bp;
375
 
}
376
 
 
377
 
 
 
1248
 
 
1249
        if(SILENT) return 0;
 
1250
        if (TCOMPRESSED == ON) {
 
1251
                if (!DELIMITER) {
 
1252
                        curtextbegin = text + *i; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
 
1253
                        if (*curtextbegin == '\n') curtextbegin ++;
 
1254
                        curtextend = curtextbegin /*text -m + *i*/ /* + 1 agrep() has i++ */; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
 
1255
                        if (*curtextend == '\n') curtextend ++;
 
1256
                }
 
1257
                else {
 
1258
                        curtextbegin = backward_delimiter(text + *i, text, tc_D_pattern, tc_D_length, OUTTAIL);
 
1259
                        curtextend = forward_delimiter(curtextbegin /*text -m + *i*/ /* + 1 agrep() has i++ */, textend, tc_D_pattern, tc_D_length, OUTTAIL);
 
1260
                }
 
1261
        }
 
1262
        else {
 
1263
                if (!DELIMITER) {
 
1264
                        curtextbegin = text + *i; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
 
1265
                        if (*curtextbegin == '\n') curtextbegin ++;
 
1266
                        curtextend = curtextbegin /*text -m + *i*/ /* + 1 agrep() has i++ */; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
 
1267
                        if (*curtextend == '\n') curtextend ++;
 
1268
                }
 
1269
                else {
 
1270
                        curtextbegin = backward_delimiter(text + *i, text, D_pattern, D_length, OUTTAIL);
 
1271
                        curtextend = forward_delimiter(curtextbegin /*text -m + *i*/ /* + 1 agrep() has i++ */, textend, D_pattern, D_length, OUTTAIL);
 
1272
                }
 
1273
        }
 
1274
 
 
1275
        if (TCOMPRESSED == ON) {
 
1276
#if     MEASURE_TIMES
 
1277
                gettimeofday(&initt, NULL);
 
1278
#endif  /*MEASURE_TIMES*/
 
1279
                if (-1 == exists_tcompressed_word(pat, m, curtextbegin, text  + *i - curtextbegin + m, EASYSEARCH)) {
 
1280
                        num_of_matched --;
 
1281
                        return 0;
 
1282
                }
 
1283
#if     MEASURE_TIMES
 
1284
                gettimeofday(&finalt, NULL);
 
1285
                FILTERALGO_ms +=  (finalt.tv_sec *1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
 
1286
#endif  /*MEASURE_TIMES*/
 
1287
        }
 
1288
 
 
1289
        textbegin = curtextend; /*(curtextend - 1 > textbegin ? curtextend - 1 : curtextend); */
 
1290
        oldi = *i;
 
1291
        *i += textbegin - (text + *i);
 
1292
        if(COUNT) return 0;
 
1293
 
 
1294
 
 
1295
        if (INVERSE) {
 
1296
                if (TCOMPRESSED == ON) { /* INVERSE: Don't care about filtering time */
 
1297
                        if (agrep_finalfp != NULL)
 
1298
                                newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, *lastout, curtextbegin - *lastout, agrep_finalfp, -1, EASYSEARCH);
 
1299
                        else {
 
1300
                                if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, *lastout, curtextbegin - *lastout, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
1301
                                        if (newlen + agrep_outpointer >= agrep_outlen) {
 
1302
                                                OUTPUT_OVERFLOW;
 
1303
                                                return -1;
 
1304
                                        }
 
1305
                                        agrep_outpointer += newlen;
 
1306
                                }
 
1307
                        }
 
1308
                        *lastout=textbegin;
 
1309
                        CurrentByteOffset += textbegin - text;
 
1310
                        text = textbegin;
 
1311
                }
 
1312
                else { /* NOT TCOMPRESSED */
 
1313
                        if (agrep_finalfp != NULL)
 
1314
                                fwrite(*lastout, 1, curtextbegin-*lastout, agrep_finalfp);
 
1315
                        else {
 
1316
                                if (curtextbegin - *lastout + agrep_outpointer >= agrep_outlen) {
 
1317
                                        OUTPUT_OVERFLOW;
 
1318
                                        return -1;
 
1319
                                }
 
1320
                                memcpy(agrep_outbuffer+agrep_outpointer, *lastout, curtextbegin-*lastout);
 
1321
                                agrep_outpointer += (curtextbegin - *lastout);
 
1322
                        }
 
1323
                        *lastout=textbegin;
 
1324
                        CurrentByteOffset += textbegin - text;
 
1325
                        text = textbegin;
 
1326
                } /* TCOMPRESSED */
 
1327
                return 0;
 
1328
        }
 
1329
 
 
1330
        if(FNAME && (NEW_FILE || !POST_FILTER)) {
 
1331
                char    nextchar = (POST_FILTER == ON)?'\n':' ';
 
1332
                char    *prevstring = (POST_FILTER == ON)?"\n":"";
 
1333
 
 
1334
                if (agrep_finalfp != NULL)
 
1335
                        fprintf(agrep_finalfp, "%s%s", prevstring, CurrentFileName);
 
1336
                else {
 
1337
                        int outindex;
 
1338
                        if (prevstring[0] != '\0') {
 
1339
                                if(agrep_outpointer + 1 >= agrep_outlen) {
 
1340
                                        OUTPUT_OVERFLOW;
 
1341
                                        return -1;
 
1342
                                }
 
1343
                                else agrep_outbuffer[agrep_outpointer ++] = prevstring[0];
 
1344
                        }
 
1345
                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1346
                                        (CurrentFileName[outindex] != '\0'); outindex++) {
 
1347
                                agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
1348
                        }
 
1349
                        if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1350
                                OUTPUT_OVERFLOW;
 
1351
                                return -1;
 
1352
                        }
 
1353
                        agrep_outpointer += outindex;
 
1354
                }
 
1355
                if (PRINTFILETIME) {
 
1356
                        char *s = aprint_file_time(CurrentFileTime);
 
1357
                        if (agrep_finalfp != NULL)
 
1358
                                fprintf(agrep_finalfp, "%s", s);
 
1359
                        else {
 
1360
                                int outindex;
 
1361
                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1362
                                                (s[outindex] != '\0'); outindex++) {
 
1363
                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1364
                                }
 
1365
                                if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1366
                                        OUTPUT_OVERFLOW;
 
1367
                                        return -1;
 
1368
                                }
 
1369
                                agrep_outpointer += outindex;
 
1370
                        }
 
1371
                }
 
1372
                if (agrep_finalfp != NULL)
 
1373
                        fprintf(agrep_finalfp, ":%c", nextchar);
 
1374
                else {
 
1375
                        if (agrep_outpointer+2>= agrep_outlen) {
 
1376
                                OUTPUT_OVERFLOW;
 
1377
                                return -1;
 
1378
                        }
 
1379
                        else {
 
1380
                                agrep_outbuffer[agrep_outpointer++] = ':';
 
1381
                                agrep_outbuffer[agrep_outpointer++] = nextchar;
 
1382
                        }
 
1383
                }
 
1384
 
 
1385
                NEW_FILE = OFF;
 
1386
                PRINTED = 1;
 
1387
        }
 
1388
 
 
1389
        if(BYTECOUNT) {
 
1390
                if (agrep_finalfp != NULL)
 
1391
                        fprintf(agrep_finalfp, "%d= ", CurrentByteOffset);
 
1392
                else {
 
1393
                        char s[32];
 
1394
                        int  outindex;
 
1395
                        sprintf(s, "%d= ", CurrentByteOffset);
 
1396
                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1397
                                        (s[outindex] != '\0'); outindex++) {
 
1398
                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1399
                        }
 
1400
                        if (s[outindex] != '\0') {
 
1401
                                OUTPUT_OVERFLOW;
 
1402
                                return -1;
 
1403
                        }
 
1404
                        agrep_outpointer += outindex;
 
1405
                }
 
1406
                PRINTED = 1;
 
1407
        }
 
1408
 
 
1409
        if (PRINTOFFSET) {
 
1410
                if (agrep_finalfp != NULL)
 
1411
                        fprintf(agrep_finalfp, "@%d{%d} ", CurrentByteOffset - (text + oldi-curtextbegin), curtextend-curtextbegin);
 
1412
                else {
 
1413
                        char s[32];
 
1414
                        int outindex;
 
1415
                        sprintf(s, "@%d{%d} ", CurrentByteOffset - (text + oldi-curtextbegin), curtextend-curtextbegin);
 
1416
                        for (outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1417
                                         (s[outindex] != '\0'); outindex ++) {
 
1418
                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1419
                        }
 
1420
                        if (s[outindex] != '\0') {
 
1421
                                OUTPUT_OVERFLOW;
 
1422
                                return -1;
 
1423
                        }
 
1424
                        agrep_outpointer += outindex;
 
1425
                }
 
1426
                PRINTED = 1;
 
1427
        }
 
1428
        if (PRINTRECORD) {
 
1429
 
 
1430
        if (TCOMPRESSED == ON) {
 
1431
#if     MEASURE_TIMES
 
1432
                gettimeofday(&initt, NULL);
 
1433
#endif  /*MEASURE_TIMES*/
 
1434
                if (agrep_finalfp != NULL) {
 
1435
                        newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_finalfp, -1, EASYSEARCH);
 
1436
                }
 
1437
                else {
 
1438
                        if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
1439
                                if (agrep_outpointer + newlen + 1 >= agrep_outlen) {
 
1440
                                        OUTPUT_OVERFLOW;
 
1441
                                        return -1;
 
1442
                                }
 
1443
                                agrep_outpointer += newlen;
 
1444
                        }
 
1445
                }
 
1446
#if     MEASURE_TIMES
 
1447
                gettimeofday(&finalt, NULL);
 
1448
                OUTFILTER_ms +=  (finalt.tv_sec*1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
 
1449
#endif  /*MEASURE_TIMES*/
 
1450
        }
 
1451
        else {
 
1452
                if (agrep_finalfp != NULL) {
 
1453
                        fwrite(curtextbegin, 1, curtextend - curtextbegin, agrep_finalfp);
 
1454
                }
 
1455
                else {
 
1456
                        if (agrep_outpointer + curtextend - curtextbegin >= agrep_outlen) {
 
1457
                                OUTPUT_OVERFLOW;
 
1458
                                return -1;
 
1459
                        }
 
1460
                        memcpy(agrep_outbuffer + agrep_outpointer, curtextbegin, curtextend - curtextbegin);
 
1461
                        agrep_outpointer += curtextend - curtextbegin;
 
1462
                }
 
1463
        }
 
1464
        }
 
1465
        else if (PRINTED) {
 
1466
                if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);
 
1467
                else agrep_outbuffer[agrep_outpointer ++] = '\n';
 
1468
                PRINTED = 0;
 
1469
        }
 
1470
        return 0;
 
1471
}
 
1472
 
 
1473
static void
378
1474
prep_bm(Pattern, m)      
379
 
        unsigned char *Pattern;
380
 
        register m;
 
1475
unsigned char *Pattern;
 
1476
register m;
381
1477
{
382
 
int i, j;
383
 
unsigned hash;
384
 
unsigned char lastc;
 
1478
        int i;
 
1479
        unsigned hash;
 
1480
        unsigned char lastc;
385
1481
        for (i = 0; i < MAXSYM; i++) SHIFT[i] = m;
386
1482
        for (i = m-1; i>=0; i--) {
387
1483
                hash = TR[Pattern[i]];
388
 
                if(SHIFT[hash] >= m - 1) SHIFT[hash] = m-1-i;
 
1484
                if((int)(SHIFT[hash]) >= (int)(m - 1)) SHIFT[hash] = m-1-i;
389
1485
        }
390
1486
        shift_1 = m-1;
 
1487
        /* shift_1 records the previous occurrence of the last character of
 
1488
        the pattern.  When we match this last character but do not have a match,
 
1489
        we can shift until we reach the next occurrence from the right. */
391
1490
        lastc = TR[Pattern[m-1]];
392
1491
        for (i= m-2; i>=0; i--) {
393
1492
                if(TR[Pattern[i]] == lastc )
394
 
                        { shift_1 = m-1 - i;  i = -1; }
395
 
        }
396
 
        if(shift_1 == 0) shift_1 = 1;
397
 
        if(NOUPPER) for(i='A'; i<='Z'; i++) SHIFT[i] = SHIFT[i +  'a' - 'A'];
 
1493
                { 
 
1494
                        shift_1 = m-1 - i;  
 
1495
                        i = -1; 
 
1496
                }
 
1497
        }
 
1498
        if(shift_1 == 0) shift_1 = 1; /* can never happen - Udi 11/7/94 */
 
1499
        if(NOUPPER) for(i=0; i<MAXSYM; i++) {
 
1500
                if (isupper(i)) SHIFT[i] = SHIFT[tolower(i)];
 
1501
                /* SHIFT[i] = SHIFT[i +  'a' - 'A']; */
 
1502
        }
398
1503
#ifdef DEBUG
399
 
        for(i='a'; i<='z'; i++) printf("%c: %d", i, SHIFT[i]); printf("\n");
400
 
        for(i='A'; i<='Z'; i++) printf("%c: %d", i, SHIFT[i]); printf("\n");
401
 
#endif
402
 
}
403
 
 
 
1504
        for(i='a'; i<='z'; i++) printf("%c: %d", i, SHIFT[i]); 
 
1505
        printf("\n");
 
1506
        for(i='A'; i<='Z'; i++) printf("%c: %d", i, SHIFT[i]); 
 
1507
        printf("\n");
 
1508
#endif
 
1509
}
 
1510
 
 
1511
/* monkey uses two characters for delta_1 shifting */
 
1512
 
 
1513
CHARTYPE SHIFT_2[MAX_SHIFT_2];
 
1514
 
 
1515
int
 
1516
monkey( pat, m, text, textend  ) 
 
1517
register int m  ; 
 
1518
register CHARTYPE *text, *textend, *pat;
 
1519
{
 
1520
        int PRINTED = 0;
 
1521
        register unsigned hash;
 
1522
        register CHARTYPE shift;
 
1523
        register int  m1, j; 
 
1524
        CHARTYPE *textbegin = text;
 
1525
        CHARTYPE *textstart;
 
1526
        int newlen;
 
1527
        CHARTYPE *curtextbegin;
 
1528
        CHARTYPE *curtextend;
 
1529
#if     MEASURE_TIMES
 
1530
        struct timeval initt, finalt;
 
1531
#endif
 
1532
        CHARTYPE *lastout = text;
 
1533
 
 
1534
        m1 = m - 1;
 
1535
        text = text+m1;
 
1536
        CurrentByteOffset += m1;
 
1537
        while (text < textend) {
 
1538
                textstart = text;
 
1539
                hash = TR[*text];
 
1540
                hash = (hash << 3) + TR[*(text-1)];
 
1541
                shift = SHIFT_2[hash];
 
1542
                while(shift) {
 
1543
                        text = text + shift;
 
1544
                        hash = (TR[*text] << 3) + TR[*(text-1)];
 
1545
                        shift = SHIFT_2[hash];
 
1546
                }
 
1547
                CurrentByteOffset += text - textstart;
 
1548
                j = 0;
 
1549
                while(TR[pat[m1 - j]] == TR[*(text - j)]) { 
 
1550
                        if(++j == m) break; 
 
1551
                }
 
1552
                if (j == m ) {
 
1553
                        if(text > textend) return 0; /* Udi: used to be >= for some reason */
 
1554
                        /* added by Udi 11/7/94 */
 
1555
                        if(WORDBOUND) {
 
1556
                                /* if(isalnum(*(unsigned char *)(text+1))) goto CONT; --> fixed by SHIOZAKI Takehiko <takehi-s@ascii.co.jp> */
 
1557
                                if((text+1 <= textend) && isalnum(*(unsigned char *)(text+1)) && isalnum(*(unsigned char *)text)) {
 
1558
                                        goto CONT;      /* as if there was no match */
 
1559
                                }
 
1560
                                /* if(isalnum(*(unsigned char *)(text-m))) goto CONT; --> fixed by SHIOZAKI Takehiko <takehi-s@ascii.co.jp> */
 
1561
                                if((textbegin <= (text-m)) && isalnum(*(unsigned char *)(text-m)) && isalnum(*(unsigned char *)(text-m+1))) {
 
1562
                                        goto CONT;      /* as if there was no match */
 
1563
                                }
 
1564
                                /* changed by Udi 11/7/94 to avoid having to set TR[] to W_delim */
 
1565
                        }
 
1566
 
 
1567
                        if (TCOMPRESSED == ON) {
 
1568
                                /* Don't update CurrentByteOffset here: only before outputting properly */
 
1569
                                if (!DELIMITER) {
 
1570
                                        curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
 
1571
                                        if (*curtextbegin == '\n') curtextbegin ++;
 
1572
                                        curtextend = curtextbegin /*text-m*/; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
 
1573
                                        if (*curtextend == '\n') curtextend ++;
 
1574
                                }
 
1575
                                else {
 
1576
                                        curtextbegin = backward_delimiter(text, textbegin, tc_D_pattern, tc_D_length, OUTTAIL);
 
1577
                                        curtextend = forward_delimiter(curtextbegin /*text -m*/, textend, tc_D_pattern, tc_D_length, OUTTAIL);
 
1578
                                }
 
1579
                        }
 
1580
                        else {
 
1581
                                /* Don't update CurrentByteOffset here: only before outputting properly */
 
1582
                                if (!DELIMITER) {
 
1583
                                        curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
 
1584
                                        if (*curtextbegin == '\n') curtextbegin ++;
 
1585
                                        curtextend = curtextbegin /*text-m*/; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
 
1586
                                        if (*curtextend == '\n') curtextend ++;
 
1587
                                }
 
1588
                                else {
 
1589
                                        curtextbegin = backward_delimiter(text, textbegin, D_pattern, D_length, OUTTAIL);
 
1590
                                        curtextend = forward_delimiter(curtextbegin/*text -m*/, textend, D_pattern, D_length, OUTTAIL);
 
1591
                                }
 
1592
                        }
 
1593
 
 
1594
                        if (TCOMPRESSED == ON) {
 
1595
#if     MEASURE_TIMES
 
1596
                                gettimeofday(&initt, NULL);
 
1597
#endif  /*MEASURE_TIMES*/
 
1598
                                if (-1 == exists_tcompressed_word(pat, m, curtextbegin, text - curtextbegin + m, EASYSEARCH))
 
1599
                                        goto CONT;      /* as if there was no match */
 
1600
#if     MEASURE_TIMES
 
1601
                                gettimeofday(&finalt, NULL);
 
1602
                                FILTERALGO_ms +=  (finalt.tv_sec *1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
 
1603
#endif  /*MEASURE_TIMES*/
 
1604
                        }
 
1605
 
 
1606
                        textbegin = curtextend; /*(curtextend - 1 > textbegin ? curtextend - 1 : curtextend); */
 
1607
                        num_of_matched++;
 
1608
                        if(FILENAMEONLY)  return 0;
 
1609
                        if (!COUNT) {
 
1610
                                if (!INVERSE) {
 
1611
                                        if(FNAME && (NEW_FILE || !POST_FILTER)) {
 
1612
                                                char    nextchar = (POST_FILTER == ON)?'\n':' ';
 
1613
                                                char    *prevstring = (POST_FILTER == ON)?"\n":"";
 
1614
 
 
1615
                                                if (agrep_finalfp != NULL)
 
1616
                                                        fprintf(agrep_finalfp, "%s%s", prevstring, CurrentFileName);
 
1617
                                                else {
 
1618
                                                        int outindex;
 
1619
                                                        if (prevstring[0] != '\0') {
 
1620
                                                                if(agrep_outpointer + 1 >= agrep_outlen) {
 
1621
                                                                        OUTPUT_OVERFLOW;
 
1622
                                                                        return -1;
 
1623
                                                                }
 
1624
                                                                else agrep_outbuffer[agrep_outpointer ++] = prevstring[0];
 
1625
                                                        }
 
1626
                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1627
                                                                        (CurrentFileName[outindex] != '\0'); outindex++) {
 
1628
                                                                agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
1629
                                                        }
 
1630
                                                        if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1631
                                                                OUTPUT_OVERFLOW;
 
1632
                                                                return -1;
 
1633
                                                        }
 
1634
                                                        agrep_outpointer += outindex;
 
1635
                                                }
 
1636
                                                if (PRINTFILETIME) {
 
1637
                                                        char *s = aprint_file_time(CurrentFileTime);
 
1638
                                                        if (agrep_finalfp != NULL)
 
1639
                                                                fprintf(agrep_finalfp, "%s", s);
 
1640
                                                        else {
 
1641
                                                                int outindex;
 
1642
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1643
                                                                                (s[outindex] != '\0'); outindex++) {
 
1644
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1645
                                                                }
 
1646
                                                                if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1647
                                                                        OUTPUT_OVERFLOW;
 
1648
                                                                        return -1;
 
1649
                                                                }
 
1650
                                                                agrep_outpointer += outindex;
 
1651
                                                        }
 
1652
                                                }
 
1653
                                                if (agrep_finalfp != NULL)
 
1654
                                                        fprintf(agrep_finalfp, ":%c", nextchar);
 
1655
                                                else {
 
1656
                                                        if (agrep_outpointer+2>= agrep_outlen) {
 
1657
                                                                OUTPUT_OVERFLOW;
 
1658
                                                                return -1;
 
1659
                                                        }
 
1660
                                                        else {
 
1661
                                                                agrep_outbuffer[agrep_outpointer++] = ':';
 
1662
                                                                agrep_outbuffer[agrep_outpointer++] = nextchar;
 
1663
                                                        }
 
1664
                                                }
 
1665
 
 
1666
                                                NEW_FILE = OFF;
 
1667
                                                PRINTED = 1;
 
1668
                                        }
 
1669
 
 
1670
                                        if(BYTECOUNT) {
 
1671
                                                if (agrep_finalfp != NULL)
 
1672
                                                        fprintf(agrep_finalfp, "%d= ", CurrentByteOffset);
 
1673
                                                else {
 
1674
                                                        char s[32];
 
1675
                                                        int  outindex;
 
1676
                                                        sprintf(s, "%d= ", CurrentByteOffset);
 
1677
                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1678
                                                                        (s[outindex] != '\0'); outindex++) {
 
1679
                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1680
                                                        }
 
1681
                                                        if (s[outindex] != '\0') {
 
1682
                                                                OUTPUT_OVERFLOW;
 
1683
                                                                return -1;
 
1684
                                                        }
 
1685
                                                        agrep_outpointer += outindex;
 
1686
                                                }
 
1687
                                                PRINTED = 1;
 
1688
                                        }
 
1689
 
 
1690
                                        if (PRINTOFFSET) {
 
1691
                                                if (agrep_finalfp != NULL)
 
1692
                                                        fprintf(agrep_finalfp, "@%d{%d} ", CurrentByteOffset - (text -curtextbegin), curtextend-curtextbegin);
 
1693
                                                else {
 
1694
                                                        char s[32];
 
1695
                                                        int outindex;
 
1696
                                                        sprintf(s, "@%d{%d} ", CurrentByteOffset - (text -curtextbegin), curtextend-curtextbegin);
 
1697
                                                        for (outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1698
                                                                         (s[outindex] != '\0'); outindex ++) {
 
1699
                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1700
                                                        }
 
1701
                                                        if (s[outindex] != '\0') {
 
1702
                                                                OUTPUT_OVERFLOW;
 
1703
                                                                return -1;
 
1704
                                                        }
 
1705
                                                        agrep_outpointer += outindex;
 
1706
                                                }
 
1707
                                                PRINTED = 1;
 
1708
                                        }
 
1709
 
 
1710
                                        CurrentByteOffset += textbegin - text;
 
1711
                                        text = textbegin;
 
1712
 
 
1713
                                        if (PRINTRECORD) {
 
1714
                                        if (TCOMPRESSED == ON) {
 
1715
#if     MEASURE_TIMES
 
1716
                                                gettimeofday(&initt, NULL);
 
1717
#endif  /*MEASURE_TIMES*/
 
1718
                                                if (agrep_finalfp != NULL)
 
1719
                                                        newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_finalfp, -1, EASYSEARCH);
 
1720
                                                else {
 
1721
                                                        if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
1722
                                                                if (agrep_outpointer + newlen + 1 >= agrep_outlen) {
 
1723
                                                                        OUTPUT_OVERFLOW;
 
1724
                                                                        return -1;
 
1725
                                                                }
 
1726
                                                                agrep_outpointer += newlen;
 
1727
                                                        }
 
1728
                                                }
 
1729
#if     MEASURE_TIMES
 
1730
                                                gettimeofday(&finalt, NULL);
 
1731
                                                OUTFILTER_ms +=  (finalt.tv_sec*1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
 
1732
#endif  /*MEASURE_TIMES*/
 
1733
                                        }
 
1734
                                        else {
 
1735
                                                if (agrep_finalfp != NULL) {
 
1736
                                                        fwrite(curtextbegin, 1, curtextend - curtextbegin, agrep_finalfp);
 
1737
                                                }
 
1738
                                                else {
 
1739
                                                        if (agrep_outpointer + curtextend - curtextbegin >= agrep_outlen) {
 
1740
                                                                OUTPUT_OVERFLOW;
 
1741
                                                                return -1;
 
1742
                                                        }
 
1743
                                                        memcpy(agrep_outbuffer+agrep_outpointer, curtextbegin, curtextend-curtextbegin);
 
1744
                                                        agrep_outpointer += curtextend - curtextbegin;
 
1745
                                                }
 
1746
                                        }
 
1747
                                        }
 
1748
                                        else if (PRINTED) {
 
1749
                                                if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);
 
1750
                                                else agrep_outbuffer[agrep_outpointer ++] = '\n';
 
1751
                                                PRINTED = 0;
 
1752
                                        }
 
1753
                                }
 
1754
                                else {  /* INVERSE */
 
1755
                                        if (!SILENT) {
 
1756
                                        if (TCOMPRESSED == ON) { /* INVERSE: Don't care about filtering time */
 
1757
                                                if (agrep_finalfp != NULL)
 
1758
                                                        newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_finalfp, -1, EASYSEARCH);
 
1759
                                                else {
 
1760
                                                        if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
1761
                                                                if (newlen + agrep_outpointer >= agrep_outlen) {
 
1762
                                                                        OUTPUT_OVERFLOW;
 
1763
                                                                        return -1;
 
1764
                                                                }
 
1765
                                                                agrep_outpointer += newlen;
 
1766
                                                        }
 
1767
                                                }
 
1768
                                                lastout=textbegin;
 
1769
                                                CurrentByteOffset += textbegin - text;
 
1770
                                                text = textbegin;
 
1771
                                        }
 
1772
                                        else { /* NOT TCOMPRESSED */
 
1773
                                                if (agrep_finalfp != NULL)
 
1774
                                                        fwrite(lastout, 1, curtextbegin-lastout, agrep_finalfp);
 
1775
                                                else {
 
1776
                                                        if (curtextbegin - lastout + agrep_outpointer >= agrep_outlen) {
 
1777
                                                                OUTPUT_OVERFLOW;
 
1778
                                                                return -1;
 
1779
                                                        }
 
1780
                                                        memcpy(agrep_outbuffer+agrep_outpointer, lastout, curtextbegin-lastout);
 
1781
                                                        agrep_outpointer += (curtextbegin - lastout);
 
1782
                                                }
 
1783
                                                lastout=textbegin;
 
1784
                                                CurrentByteOffset += textbegin - text;
 
1785
                                                text = textbegin;
 
1786
                                        } /* TCOMPRESSED */
 
1787
                                        } /* !SILENT */
 
1788
                                } /* INVERSE */
 
1789
                        }
 
1790
                        else {  /* COUNT */
 
1791
                                CurrentByteOffset += textbegin - text;
 
1792
                                text = textbegin;
 
1793
                        }
 
1794
 
 
1795
                        /* Counteract the ++ below */
 
1796
                        text --;
 
1797
                        CurrentByteOffset --;
 
1798
                        if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
 
1799
                            ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) return 0;   /* done */
 
1800
                }
 
1801
        CONT:
 
1802
                text++;
 
1803
                CurrentByteOffset ++;
 
1804
        }
 
1805
 
 
1806
        if (!SILENT && INVERSE && !COUNT && (lastout <= textend)) {
 
1807
                if (TCOMPRESSED == ON) { /* INVERSE: Don't care about filtering time */
 
1808
                        if (agrep_finalfp != NULL)
 
1809
                                newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_finalfp, -1, EASYSEARCH);
 
1810
                        else {
 
1811
                                if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
1812
                                        if (newlen + agrep_outpointer >= agrep_outlen) {
 
1813
                                                OUTPUT_OVERFLOW;
 
1814
                                                return -1;
 
1815
                                        }
 
1816
                                        agrep_outpointer += newlen;
 
1817
                                }
 
1818
                        }
 
1819
                }
 
1820
                else { /* NOT TCOMPRESSED */
 
1821
                        if (agrep_finalfp != NULL)
 
1822
                                fwrite(lastout, 1, textend-lastout + 1, agrep_finalfp);
 
1823
                        else {
 
1824
                                if (textend - lastout + 1 + agrep_outpointer >= agrep_outlen) {
 
1825
                                        OUTPUT_OVERFLOW;
 
1826
                                        return -1;
 
1827
                                }
 
1828
                                memcpy(agrep_outbuffer+agrep_outpointer, lastout, textend-lastout + 1);
 
1829
                                agrep_outpointer += (textend - lastout + 1);
 
1830
                        }
 
1831
                } /* TCOMPRESSED */
 
1832
        }
 
1833
 
 
1834
        return 0;
 
1835
}
404
1836
 
405
1837
/* a_monkey() the approximate monkey move */
406
 
 
 
1838
int
407
1839
a_monkey( pat, m, text, textend, D ) 
408
 
register int m, D ; register CHARTYPE *text, *textend, *pat;
 
1840
register int m, D ; 
 
1841
register CHARTYPE *text, *textend, *pat;
409
1842
{
410
 
register CHARTYPE *oldtext;
411
 
register unsigned hash, i, hashmask, suffix_error; 
412
 
register int  m1 = m-1-D, j, pos; 
 
1843
        int PRINTED = 0;
 
1844
        register CHARTYPE *oldtext;
 
1845
        CHARTYPE *curtextbegin;
 
1846
        CHARTYPE *curtextend;
 
1847
        register unsigned hash, hashmask, suffix_error; 
 
1848
        register int  m1 = m-1-D, pos; 
 
1849
        CHARTYPE *textbegin = text;
 
1850
        CHARTYPE *textstart;
 
1851
        CHARTYPE *lastout = text;
 
1852
        int newlen;
413
1853
 
414
 
        hashmask = Hashmask;
415
 
        oldtext  = text;
416
 
        while (text < textend) {
417
 
                text = text+m1;
418
 
                suffix_error = 0;
419
 
                while(suffix_error <= D) {
 
1854
        hashmask = Hashmask;
 
1855
        oldtext  = text;
 
1856
        while (text < textend) {
 
1857
                textstart = text;
 
1858
                text = text+m1;
 
1859
                suffix_error = 0;
 
1860
                while(suffix_error <= D) {
420
1861
                        hash = *text--;
421
1862
                        while(MEMBER_1[hash]) {
422
 
                                hash = ((hash << LOG_ASCII) + *(text--)) & hashmask;
 
1863
                                hash = ((hash << LOG_ASCII) + *(text--)) & hashmask;
423
1864
                        }
424
1865
                        suffix_error++;
425
 
                }
426
 
                if(text <= oldtext) {
427
 
                           if((pos = verify(m, 2*m+D, D, pat, oldtext)) > 0)  {
 
1866
                }
 
1867
                CurrentByteOffset += text - textstart;
 
1868
                if(text <= oldtext) {
 
1869
                        if((pos = verify(m, 2*m+D, D, pat, oldtext)) > 0)  {
 
1870
                                CurrentByteOffset += (oldtext+pos - text);
428
1871
                                text = oldtext+pos;
429
 
                                if(text > textend) return;
 
1872
                                if(text > textend) return 0;
 
1873
 
 
1874
                                /* Don't update CurrentByteOffset here: only before outputting properly */
 
1875
                                if (TCOMPRESSED == ON) {
 
1876
                                        if (!DELIMITER) {
 
1877
                                                curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
 
1878
                                                if (*curtextbegin == '\n') curtextbegin ++;
 
1879
                                                curtextend = curtextbegin /*text -m*/; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
 
1880
                                                if (*curtextend == '\n') curtextend ++;
 
1881
                                        }
 
1882
                                        else {
 
1883
                                                curtextbegin = backward_delimiter(text, textbegin, tc_D_pattern, tc_D_length, OUTTAIL);
 
1884
                                                curtextend = forward_delimiter(curtextbegin /*text -m*/, textend, tc_D_pattern, tc_D_length, OUTTAIL);
 
1885
                                        }
 
1886
                                }
 
1887
                                else {
 
1888
                                        if (!DELIMITER) {
 
1889
                                                curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
 
1890
                                                if (*curtextbegin == '\n') curtextbegin ++;
 
1891
                                                curtextend = curtextbegin/*text -m*/; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
 
1892
                                                if (*curtextend == '\n') curtextend ++;
 
1893
                                        }
 
1894
                                        else {
 
1895
                                                curtextbegin = backward_delimiter(text, textbegin, D_pattern, D_length, OUTTAIL);
 
1896
                                                curtextend = forward_delimiter(curtextbegin/*text -m*/, textend, D_pattern, D_length, OUTTAIL);
 
1897
                                        }
 
1898
                                }
 
1899
                                textbegin = curtextend; /* (curtextend - 1 > textbegin ? curtextend - 1 : curtextend); */
 
1900
 
430
1901
                                num_of_matched++;
431
 
                                if(FILENAMEONLY) return;
432
 
                                if(!(COUNT)) {
433
 
                                        if(FNAME) printf("%s: ", CurrentFileName);
434
 
                                        while(*(--text) != '\n');
435
 
                                        while(*(++text) != '\n') putchar(*text);
436
 
                                        printf("\n");
437
 
                                }
438
 
                                else {
439
 
                                        while(*text != '\n') text++;
440
 
                                }
441
 
                           }
442
 
                           else { 
443
 
                                text = oldtext + m;
444
 
                           }
445
 
                }
446
 
                oldtext = text; 
447
 
        }
448
 
}
449
 
 
450
 
/* monkey uses two characters for delta_1 shifting */
451
 
 
452
 
CHARTYPE SHIFT_2[MAX_SHIFT_2];
453
 
 
454
 
monkey( pat, m, text, textend  ) 
455
 
register int m  ; register CHARTYPE *text, *textend, *pat;
456
 
{
457
 
register unsigned hash, i; 
458
 
register CHARTYPE shift;
459
 
register int  m1, j; 
460
 
register unsigned r_newline;
461
 
 
462
 
r_newline = '\n';
463
 
 
464
 
  m1 = m - 1;
465
 
  text = text+m1;
466
 
  while (text < textend) {
467
 
        hash = *text;
468
 
        hash = (hash << 3) + *(text-1);
469
 
        shift = SHIFT_2[hash];
470
 
        while(shift) {
471
 
                text = text + shift;
472
 
                hash = (*text << 3) + *(text-1);
473
 
                shift = SHIFT_2[hash];
474
 
        }
475
 
        j = 0;
476
 
        while(TR[pat[m1 - j]] == TR[*(text - j)]) { if(++j == m) break; }
477
 
        if (j == m ) { 
478
 
                if(text >= textend) return;
479
 
                num_of_matched++;
480
 
                if(FILENAMEONLY)  return;
481
 
                if(COUNT) {
482
 
                          while (*text != r_newline) text++;
483
 
                          text--;
484
 
                }
485
 
                else {
486
 
                          if(FNAME) printf("%s: ", CurrentFileName);
487
 
                          while(*(--text) != r_newline);
488
 
                          while(*(++text) != r_newline) putchar(*text);
489
 
                          printf("\n");
490
 
                          text--;
491
 
                }
492
 
        }
493
 
        text++;
494
 
  }
495
 
}
496
 
 
 
1902
                                if(FILENAMEONLY) return 0;
 
1903
                                if(!COUNT) {
 
1904
                                        if (!INVERSE) {
 
1905
                                                if(FNAME && (NEW_FILE || !POST_FILTER)) {
 
1906
                                                        char    nextchar = (POST_FILTER == ON)?'\n':' ';
 
1907
                                                        char    *prevstring = (POST_FILTER == ON)?"\n":"";
 
1908
 
 
1909
                                                        if (agrep_finalfp != NULL)
 
1910
                                                                fprintf(agrep_finalfp, "%s%s", prevstring, CurrentFileName);
 
1911
                                                        else {
 
1912
                                                                int outindex;
 
1913
                                                                if (prevstring[0] != '\0') {
 
1914
                                                                        if(agrep_outpointer + 1 >= agrep_outlen) {
 
1915
                                                                                OUTPUT_OVERFLOW;
 
1916
                                                                                return -1;
 
1917
                                                                        }
 
1918
                                                                        else agrep_outbuffer[agrep_outpointer ++] = prevstring[0];
 
1919
                                                                }
 
1920
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1921
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
1922
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
1923
                                                                }
 
1924
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1925
                                                                        OUTPUT_OVERFLOW;
 
1926
                                                                        return -1;
 
1927
                                                                }
 
1928
                                                                agrep_outpointer += outindex;
 
1929
                                                        }
 
1930
                                                        if (PRINTFILETIME) {
 
1931
                                                                char *s = aprint_file_time(CurrentFileTime);
 
1932
                                                                if (agrep_finalfp != NULL)
 
1933
                                                                        fprintf(agrep_finalfp, "%s", s);
 
1934
                                                                else {
 
1935
                                                                        int outindex;
 
1936
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1937
                                                                                        (s[outindex] != '\0'); outindex++) {
 
1938
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1939
                                                                        }
 
1940
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1941
                                                                                OUTPUT_OVERFLOW;
 
1942
                                                                                return -1;
 
1943
                                                                        }
 
1944
                                                                        agrep_outpointer += outindex;
 
1945
                                                                }
 
1946
                                                        }
 
1947
                                                        if (agrep_finalfp != NULL)
 
1948
                                                                fprintf(agrep_finalfp, ":%c", nextchar);
 
1949
                                                        else {
 
1950
                                                                if (agrep_outpointer+2>= agrep_outlen) {
 
1951
                                                                        OUTPUT_OVERFLOW;
 
1952
                                                                        return -1;
 
1953
                                                                }
 
1954
                                                                else {
 
1955
                                                                        agrep_outbuffer[agrep_outpointer++] = ':';
 
1956
                                                                        agrep_outbuffer[agrep_outpointer++] = nextchar;
 
1957
                                                                }
 
1958
                                                        }
 
1959
 
 
1960
                                                        NEW_FILE = OFF;
 
1961
                                                        PRINTED = 1;
 
1962
                                                }
 
1963
 
 
1964
                                                if(BYTECOUNT) {
 
1965
                                                        if (agrep_finalfp != NULL)
 
1966
                                                                fprintf(agrep_finalfp, "%d= ", CurrentByteOffset);
 
1967
                                                        else {
 
1968
                                                                char s[32];
 
1969
                                                                int  outindex;
 
1970
                                                                sprintf(s, "%d= ", CurrentByteOffset);
 
1971
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1972
                                                                                (s[outindex] != '\0'); outindex++) {
 
1973
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1974
                                                                }
 
1975
                                                                if (s[outindex] != '\0') {
 
1976
                                                                        OUTPUT_OVERFLOW;
 
1977
                                                                        return -1;
 
1978
                                                                }
 
1979
                                                                agrep_outpointer += outindex;
 
1980
                                                        }
 
1981
                                                        PRINTED = 1;
 
1982
                                                }
 
1983
 
 
1984
                                                if (PRINTOFFSET) {
 
1985
                                                        if (agrep_finalfp != NULL)
 
1986
                                                                fprintf(agrep_finalfp, "@%d{%d} ", CurrentByteOffset - (text -curtextbegin), curtextend-curtextbegin);
 
1987
                                                        else {
 
1988
                                                                char s[32];
 
1989
                                                                int outindex;
 
1990
                                                                sprintf(s, "@%d{%d} ", CurrentByteOffset - (text -curtextbegin), curtextend-curtextbegin);
 
1991
                                                                for (outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1992
                                                                                 (s[outindex] != '\0'); outindex ++) {
 
1993
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1994
                                                                }
 
1995
                                                                if (s[outindex] != '\0') {
 
1996
                                                                        OUTPUT_OVERFLOW;
 
1997
                                                                        return -1;
 
1998
                                                                }
 
1999
                                                                agrep_outpointer += outindex;
 
2000
                                                        }
 
2001
                                                        PRINTED = 1;
 
2002
                                                }
 
2003
 
 
2004
                                                CurrentByteOffset += textbegin - text;
 
2005
                                                text = textbegin;
 
2006
 
 
2007
                                                if (PRINTRECORD) {
 
2008
                                                if (TCOMPRESSED == ON) {
 
2009
#if     MEASURE_TIMES
 
2010
                                                        gettimeofday(&initt, NULL);
 
2011
#endif  /*MEASURE_TIMES*/
 
2012
                                                        if (agrep_finalfp != NULL)
 
2013
                                                                newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_finalfp, -1, EASYSEARCH);
 
2014
                                                        else {
 
2015
                                                                if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
2016
                                                                        if (agrep_outpointer + newlen + 1 >= agrep_outlen) {
 
2017
                                                                                OUTPUT_OVERFLOW;
 
2018
                                                                                return -1;
 
2019
                                                                        }
 
2020
                                                                        agrep_outpointer += newlen;
 
2021
                                                                }
 
2022
                                                        }
 
2023
#if     MEASURE_TIMES
 
2024
                                                        gettimeofday(&finalt, NULL);
 
2025
                                                        OUTFILTER_ms +=  (finalt.tv_sec*1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
 
2026
#endif  /*MEASURE_TIMES*/
 
2027
                                                }
 
2028
                                                else {
 
2029
                                                        if (agrep_finalfp != NULL) {
 
2030
                                                                fwrite(curtextbegin, 1, curtextend - curtextbegin, agrep_finalfp);
 
2031
                                                        }
 
2032
                                                        else {
 
2033
                                                                if (agrep_outpointer + curtextend - curtextbegin >= agrep_outlen) {
 
2034
                                                                        OUTPUT_OVERFLOW;
 
2035
                                                                        return -1;
 
2036
                                                                }
 
2037
                                                                memcpy(agrep_outbuffer+agrep_outpointer, curtextbegin, curtextend-curtextbegin);
 
2038
                                                                agrep_outpointer += curtextend - curtextbegin;
 
2039
                                                        }
 
2040
                                                }
 
2041
                                                }
 
2042
                                                else if (PRINTED) {
 
2043
                                                        if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);
 
2044
                                                        else agrep_outbuffer[agrep_outpointer ++] = '\n';
 
2045
                                                        PRINTED = 0;
 
2046
                                                }
 
2047
                                        }
 
2048
                                        else {  /* INVERSE */
 
2049
                                                if (!SILENT) {
 
2050
                                                if (TCOMPRESSED == ON) { /* INVERSE: Don't care about filtering time */
 
2051
                                                        if (agrep_finalfp != NULL)
 
2052
                                                                newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_finalfp, -1, EASYSEARCH);
 
2053
                                                        else {
 
2054
                                                                if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
2055
                                                                        if (newlen + agrep_outpointer >= agrep_outlen) {
 
2056
                                                                                OUTPUT_OVERFLOW;
 
2057
                                                                                return -1;
 
2058
                                                                        }
 
2059
                                                                        agrep_outpointer += newlen;
 
2060
                                                                }
 
2061
                                                        }
 
2062
                                                        lastout=textbegin;
 
2063
                                                        CurrentByteOffset += textbegin - text;
 
2064
                                                        text = textbegin;
 
2065
                                                }
 
2066
                                                else { /* NOT TCOMPRESSED */
 
2067
                                                        if (agrep_finalfp != NULL)
 
2068
                                                                fwrite(lastout, 1, curtextbegin-lastout, agrep_finalfp);
 
2069
                                                        else {
 
2070
                                                                if (curtextbegin - lastout + agrep_outpointer >= agrep_outlen) {
 
2071
                                                                        OUTPUT_OVERFLOW;
 
2072
                                                                        return -1;
 
2073
                                                                }
 
2074
                                                                memcpy(agrep_outbuffer+agrep_outpointer, lastout, curtextbegin-lastout);
 
2075
                                                                agrep_outpointer += (curtextbegin - lastout);
 
2076
                                                        }
 
2077
                                                        lastout=textbegin;
 
2078
                                                        CurrentByteOffset += textbegin - text;
 
2079
                                                        text = textbegin;
 
2080
                                                } /* TCOMPRESSED */
 
2081
                                                } /* !SILENT */
 
2082
                                        } /* INVERSE */
 
2083
                                }
 
2084
                                else {  /* COUNT */
 
2085
                                        CurrentByteOffset += textbegin - text;
 
2086
                                        text = textbegin;
 
2087
                                }
 
2088
                                if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
 
2089
                                    ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) return 0;   /* done */
 
2090
                        }
 
2091
                        else {
 
2092
                                CurrentByteOffset += (oldtext + m - text);
 
2093
                                text = oldtext + m;
 
2094
                        }
 
2095
                }
 
2096
                oldtext = text;
 
2097
        }
 
2098
 
 
2099
        if (!SILENT && INVERSE && !COUNT && (lastout <= textend)) {
 
2100
                if (TCOMPRESSED == ON) { /* INVERSE: Don't care about filtering time */
 
2101
                        if (agrep_finalfp != NULL)
 
2102
                                newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_finalfp, -1, EASYSEARCH);
 
2103
                        else {
 
2104
                                if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
2105
                                        if (newlen + agrep_outpointer >= agrep_outlen) {
 
2106
                                                OUTPUT_OVERFLOW;
 
2107
                                                return -1;
 
2108
                                        }
 
2109
                                        agrep_outpointer += newlen;
 
2110
                                }
 
2111
                        }
 
2112
                }
 
2113
                else { /* NOT TCOMPRESSED */
 
2114
                        if (agrep_finalfp != NULL)
 
2115
                                fwrite(lastout, 1, textend-lastout + 1, agrep_finalfp);
 
2116
                        else {
 
2117
                                if (textend - lastout + 1 + agrep_outpointer >= agrep_outlen) {
 
2118
                                        OUTPUT_OVERFLOW;
 
2119
                                        return -1;
 
2120
                                }
 
2121
                                memcpy(agrep_outbuffer+agrep_outpointer, lastout, textend-lastout + 1);
 
2122
                                agrep_outpointer += (textend - lastout + 1);
 
2123
                        }
 
2124
                } /* TCOMPRESSED */
 
2125
        }
 
2126
 
 
2127
        return 0;
 
2128
}
 
2129
 
 
2130
static void
497
2131
am_preprocess(Pattern)
498
2132
CHARTYPE *Pattern;
499
2133
{
500
 
int i, j, m;
501
 
unsigned hash;
 
2134
        int i, m;
502
2135
        m = strlen(Pattern);
503
2136
        for (i = 1, Hashmask = 1 ; i<16 ; i++) Hashmask = (Hashmask << 1) + 1 ;
504
2137
        for (i = 0; i < MAXMEMBER_1; i++) MEMBER_1[i] = 0;
505
2138
        for (i = m-1; i>=0; i--) {
506
2139
                MEMBER_1[Pattern[i]] = 1;
507
 
        }
 
2140
        }
508
2141
        for (i = m-1; i > 0; i--) {
509
 
                MEMBER_1[(Pattern[i] << LOG_ASCII) + Pattern[i-1]] = 1;
 
2142
                MEMBER_1[(Pattern[i] << LOG_ASCII) + Pattern[i-1]] = 1;
510
2143
        }
511
2144
}
512
2145
 
513
 
 
 
2146
int
514
2147
verify(m, n, D, pat, text)
515
2148
register int m, n, D;
516
2149
CHARTYPE *pat, *text;
517
2150
{   
518
 
    int A[MAXPATT], B[MAXPATT];
519
 
    register int last = D;      
520
 
    register int cost = 0;  
521
 
    register int k, i, c;
522
 
    register int m1 = m+1;
523
 
    CHARTYPE *textend = text+n;
524
 
    CHARTYPE *textbegin = text;
 
2151
        int A[MAXPATT], B[MAXPATT];
 
2152
        register int last = D;      
 
2153
        register int cost = 0;  
 
2154
        register int k, i, c;
 
2155
        register int m1 = m+1;
 
2156
        CHARTYPE *textend = text+n;
 
2157
        CHARTYPE *textbegin = text;
525
2158
 
526
 
   for (i = 0; i <= m1; i++)  A[i] = B[i] = i;
527
 
   while (text < textend)
528
 
   {
529
 
       for (k = 1; k <= last; k++)
530
 
       {
531
 
           cost = B[k-1]+1; 
532
 
           if (pat[k-1] != *text)
533
 
           {   if (B[k]+1 < cost) cost = B[k]+1; 
534
 
               if (A[k-1]+1 < cost) cost = A[k-1]+1; }
535
 
           else cost = cost -1; 
536
 
           A[k] = cost; 
537
 
       }
538
 
       if(pat[last] == *text++) { A[last+1] = B[last]; last++; }
539
 
       if(A[last] < D) A[last+1] = A[last++]+1;
540
 
       while (A[last] > D) last = last - 1;
541
 
       if(last >= m) return(text - textbegin - 1);
542
 
       if(*text == '\n') {
543
 
            last = D;
544
 
            for(c = 0; c<=m1; c++) A[c] = B[c] = c;
545
 
       }
546
 
       for (k = 1; k <= last; k++)
547
 
       {
548
 
           cost = A[k-1]+1; 
549
 
           if (pat[k-1] != *text)
550
 
           {   if (A[k]+1 < cost) cost = A[k]+1; 
551
 
               if (B[k-1]+1 < cost) cost = B[k-1]+1; }
552
 
           else cost = cost -1; 
553
 
           B[k] = cost;
554
 
       }
555
 
       if(pat[last] == *text++) { B[last+1] = A[last]; last++; }
556
 
       if(B[last] < D) B[last+1] = B[last++]+1;
557
 
       while (B[last] > D) last = last -1;
558
 
       if(last >= m)   return(text - textbegin - 1);
559
 
       if(*text == '\n') {
560
 
            last = D;
561
 
            for(c = 0; c<=m1; c++) A[c] = B[c] = c;
562
 
       }
563
 
   }    
564
 
   return(0);
 
2159
        for (i = 0; i <= m1; i++)  A[i] = B[i] = i;
 
2160
        while (text < textend)
 
2161
        {
 
2162
                for (k = 1; k <= last; k++)
 
2163
                {
 
2164
                        cost = B[k-1]+1; 
 
2165
                        if (pat[k-1] != *text)
 
2166
                        {   
 
2167
                                if (B[k]+1 < cost) cost = B[k]+1; 
 
2168
                                if (A[k-1]+1 < cost) cost = A[k-1]+1; 
 
2169
                        }
 
2170
                        else cost = cost -1; 
 
2171
                        A[k] = cost; 
 
2172
                }
 
2173
                if(pat[last] == *text++) { 
 
2174
                        A[last+1] = B[last]; 
 
2175
                        last++; 
 
2176
                }
 
2177
                if(A[last] < D) A[last+1] = A[last++]+1;
 
2178
                while (A[last] > D) last = last - 1;
 
2179
                if(last >= m) return(text - textbegin - 1);
 
2180
                if(*text == '\n') {
 
2181
                        last = D;
 
2182
                        for(c = 0; c<=m1; c++) A[c] = B[c] = c;
 
2183
                }
 
2184
                for (k = 1; k <= last; k++)
 
2185
                {
 
2186
                        cost = A[k-1]+1; 
 
2187
                        if (pat[k-1] != *text)
 
2188
                        {   
 
2189
                                if (A[k]+1 < cost) cost = A[k]+1; 
 
2190
                                if (B[k-1]+1 < cost) cost = B[k-1]+1; 
 
2191
                        }
 
2192
                        else cost = cost -1; 
 
2193
                        B[k] = cost;
 
2194
                }
 
2195
                if(pat[last] == *text++) { 
 
2196
                        B[last+1] = A[last]; 
 
2197
                        last++; 
 
2198
                }
 
2199
                if(B[last] < D) B[last+1] = B[last++]+1;
 
2200
                while (B[last] > D) last = last -1;
 
2201
                if(last >= m)   return(text - textbegin - 1);
 
2202
                if(*text == '\n') {
 
2203
                        last = D;
 
2204
                        for(c = 0; c<=m1; c++) A[c] = B[c] = c;
 
2205
                }
 
2206
        }    
 
2207
        return(0);
565
2208
}
566
2209
 
567
2210
/* preprocessing for monkey()   */
568
 
 
 
2211
static void
569
2212
m_preprocess(Pattern)
570
2213
CHARTYPE *Pattern;
571
2214
{
572
 
int i, j, m;
573
 
unsigned hash;
 
2215
        int i, j, m;
 
2216
        unsigned hash;
574
2217
        m = strlen(Pattern);
575
2218
        for (i = 0; i < MAX_SHIFT_2; i++) SHIFT_2[i] = m;
576
2219
        for (i = m-1; i>=1; i--) {
577
 
                hash = Pattern[i];
 
2220
                hash = TR[Pattern[i]];
578
2221
                hash = hash << 3;
579
2222
                for (j = 0; j< MAXSYM; j++) {
580
2223
                        if(SHIFT_2[hash+j] == m) SHIFT_2[hash+j] = m-1;
581
2224
                }
582
 
                hash = hash + Pattern[i-1];
583
 
                if(SHIFT_2[hash] >= m - 1) SHIFT_2[hash] = m-1-i;
 
2225
                hash = hash + TR[Pattern[i-1]];
 
2226
                if((int)(SHIFT_2[hash]) >= (int)(m - 1)) SHIFT_2[hash] = m-1-i;
584
2227
        }
585
2228
        shift_1 = m-1;
586
2229
        for (i= m-2; i>=0; i--) {
587
 
                if(Pattern[i] == Pattern[m-1] )
588
 
                        { shift_1 = m-1 - i;  i = -1; }
 
2230
                if(TR[Pattern[i]] == TR[Pattern[m-1]] )
 
2231
                { 
 
2232
                        shift_1 = m-1 - i;  
 
2233
                        i = -1; 
 
2234
                }
589
2235
        }
590
2236
        if(shift_1 == 0) shift_1 = 1;
591
2237
        SHIFT_2[0] = 0;
593
2239
 
594
2240
/* monkey4() the approximate monkey move */
595
2241
 
596
 
char *MEMBER_D;
 
2242
char *MEMBER_D = NULL;
597
2243
 
 
2244
int
598
2245
monkey4( pat, m, text, textend, D  ) 
599
 
register int m, D ; register unsigned char *text, *pat, *textend;
 
2246
register int m, D ; 
 
2247
register unsigned char *text, *pat, *textend;
600
2248
{
601
 
register unsigned char *oldtext;
602
 
register unsigned hash, i, hashmask, suffix_error; 
603
 
register int m1=m-1-D, j, pos; 
 
2249
        int PRINTED = 0;
 
2250
        register unsigned char *oldtext;
 
2251
        register unsigned hash, hashmask, suffix_error; 
 
2252
        register int m1=m-1-D, pos; 
 
2253
        CHARTYPE *textbegin = text;
 
2254
        CHARTYPE *textstart;
 
2255
        CHARTYPE *curtextbegin;
 
2256
        CHARTYPE *curtextend;
 
2257
        CHARTYPE *lastout = text;
 
2258
        int newlen;
604
2259
 
605
 
  hashmask = Hashmask;
606
 
  oldtext = text ;
607
 
  while (text < textend) {
608
 
     text = text + m1;
609
 
     suffix_error = 0;
610
 
     while(suffix_error <= D) {
611
 
        hash = char_map[*text--];
612
 
        hash = ((hash << LOG_DNA) + char_map[*(text--)]) & hashmask;
613
 
        while(MEMBER_D[hash]) {
614
 
              hash = ((hash << LOG_DNA) + char_map[*(text--)]) & hashmask;
615
 
        }
616
 
        suffix_error++;
617
 
     }
618
 
     if(text <= oldtext) {
619
 
                           if((pos = verify(m, 2*m+D, D, pat, oldtext)) > 0)  {
 
2260
        hashmask = Hashmask;
 
2261
        oldtext = text ;
 
2262
        while (text < textend) {
 
2263
                textstart = text;
 
2264
                text = text + m1;
 
2265
                suffix_error = 0;
 
2266
                while(suffix_error <= D) {
 
2267
                        hash = char_map[*text--];
 
2268
                        hash = ((hash << LOG_DNA) + char_map[*(text--)]) & hashmask;
 
2269
                        while(MEMBER_D[hash]) {
 
2270
                                hash = ((hash << LOG_DNA) + char_map[*(text--)]) & hashmask;
 
2271
                        }
 
2272
                        suffix_error++;
 
2273
                }
 
2274
                CurrentByteOffset += text - textstart;
 
2275
                if(text <= oldtext) {
 
2276
                        if((pos = verify(m, 2*m+D, D, pat, oldtext)) > 0)  {
 
2277
                                CurrentByteOffset += (oldtext+pos - text);
620
2278
                                text = oldtext+pos;
621
 
                                if(text > textend) return;
 
2279
                                if(text > textend) return 0;
 
2280
 
 
2281
                                if (TCOMPRESSED == ON) {
 
2282
                                        /* Don't update CurrentByteOffset here: only before outputting properly */
 
2283
                                        if (!DELIMITER) {
 
2284
                                                curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
 
2285
                                                if (*curtextbegin == '\n') curtextbegin ++;
 
2286
                                                curtextend = curtextbegin /*text -m*/; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
 
2287
                                                if (*curtextend == '\n') curtextend ++;
 
2288
                                        }
 
2289
                                        else {
 
2290
                                                curtextbegin = backward_delimiter(text, textbegin, tc_D_pattern, tc_D_length, OUTTAIL);
 
2291
                                                curtextend = forward_delimiter(curtextbegin/*text -m*/, textend, tc_D_pattern, tc_D_length, OUTTAIL);
 
2292
                                        }
 
2293
                                }
 
2294
                                else {
 
2295
                                        /* Don't update CurrentByteOffset here: only before outputting properly */
 
2296
                                        if (!DELIMITER) {
 
2297
                                                curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
 
2298
                                                if (*curtextbegin == '\n') curtextbegin ++;
 
2299
                                                curtextend = curtextbegin /*text -m*/; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
 
2300
                                                if (*curtextend == '\n') curtextend ++;
 
2301
                                        }
 
2302
                                        else {
 
2303
                                                curtextbegin = backward_delimiter(text, textbegin, D_pattern, D_length, OUTTAIL);
 
2304
                                                curtextend = forward_delimiter(curtextbegin/*text -m*/, textend, D_pattern, D_length, OUTTAIL);
 
2305
                                        }
 
2306
                                }
 
2307
                                textbegin = curtextend; /*(curtextend - 1 > textbegin ? curtextend - 1 : curtextend); */
 
2308
 
622
2309
                                num_of_matched++;
623
 
                                if(FILENAMEONLY) return;
624
 
                                if(!(COUNT)) {
625
 
                                        if(FNAME) printf("%s:", CurrentFileName);
626
 
                                        while(*(--text) != '\n');
627
 
                                        while(*(++text) != '\n') putchar(*text);
628
 
                                        printf("\n");
629
 
                                        text++;
630
 
                                }
631
 
                                else {
632
 
                                        while(*text != '\n') text++;
633
 
                                        text++;
634
 
                                }
635
 
                           }
636
 
                           else text = oldtext + m;
637
 
     }
638
 
     oldtext = text; 
639
 
  }
 
2310
                                if(FILENAMEONLY) return 0;
 
2311
                                if(!COUNT) {
 
2312
                                        if (!INVERSE) {
 
2313
                                                if(FNAME && (NEW_FILE || !POST_FILTER)) {
 
2314
                                                        char    nextchar = (POST_FILTER == ON)?'\n':' ';
 
2315
                                                        char    *prevstring = (POST_FILTER == ON)?"\n":"";
 
2316
 
 
2317
                                                        if (agrep_finalfp != NULL)
 
2318
                                                                fprintf(agrep_finalfp, "%s%s", prevstring, CurrentFileName);
 
2319
                                                        else {
 
2320
                                                                int outindex;
 
2321
                                                                if (prevstring[0] != '\0') {
 
2322
                                                                        if(agrep_outpointer + 1 >= agrep_outlen) {
 
2323
                                                                                OUTPUT_OVERFLOW;
 
2324
                                                                                return -1;
 
2325
                                                                        }
 
2326
                                                                        else agrep_outbuffer[agrep_outpointer ++] = prevstring[0];
 
2327
                                                                }
 
2328
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
2329
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
2330
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
2331
                                                                }
 
2332
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
2333
                                                                        OUTPUT_OVERFLOW;
 
2334
                                                                        return -1;
 
2335
                                                                }
 
2336
                                                                agrep_outpointer += outindex;
 
2337
                                                        }
 
2338
                                                        if (PRINTFILETIME) {
 
2339
                                                                char *s = aprint_file_time(CurrentFileTime);
 
2340
                                                                if (agrep_finalfp != NULL)
 
2341
                                                                        fprintf(agrep_finalfp, "%s", s);
 
2342
                                                                else {
 
2343
                                                                        int outindex;
 
2344
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
2345
                                                                                        (s[outindex] != '\0'); outindex++) {
 
2346
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
2347
                                                                        }
 
2348
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
2349
                                                                                OUTPUT_OVERFLOW;
 
2350
                                                                                return -1;
 
2351
                                                                        }
 
2352
                                                                        agrep_outpointer += outindex;
 
2353
                                                                }
 
2354
                                                        }
 
2355
                                                        if (agrep_finalfp != NULL)
 
2356
                                                                fprintf(agrep_finalfp, ":%c", nextchar);
 
2357
                                                        else {
 
2358
                                                                if (agrep_outpointer+2>= agrep_outlen) {
 
2359
                                                                        OUTPUT_OVERFLOW;
 
2360
                                                                        return -1;
 
2361
                                                                }
 
2362
                                                                else {
 
2363
                                                                        agrep_outbuffer[agrep_outpointer++] = ':';
 
2364
                                                                        agrep_outbuffer[agrep_outpointer++] = nextchar;
 
2365
                                                                }
 
2366
                                                        }
 
2367
 
 
2368
                                                        NEW_FILE = OFF;
 
2369
                                                        PRINTED = 1;
 
2370
                                                }
 
2371
 
 
2372
                                                if(BYTECOUNT) {
 
2373
                                                        if (agrep_finalfp != NULL)
 
2374
                                                                fprintf(agrep_finalfp, "%d= ", CurrentByteOffset);
 
2375
                                                        else {
 
2376
                                                                char s[32];
 
2377
                                                                int  outindex;
 
2378
                                                                sprintf(s, "%d= ", CurrentByteOffset);
 
2379
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
2380
                                                                                (s[outindex] != '\0'); outindex++) {
 
2381
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
2382
                                                                }
 
2383
                                                                if (s[outindex] != '\0') {
 
2384
                                                                        OUTPUT_OVERFLOW;
 
2385
                                                                        return -1;
 
2386
                                                                }
 
2387
                                                                agrep_outpointer += outindex;
 
2388
                                                        }
 
2389
                                                        PRINTED = 1;
 
2390
                                                }
 
2391
 
 
2392
                                                if (PRINTOFFSET) {
 
2393
                                                        if (agrep_finalfp != NULL)
 
2394
                                                                fprintf(agrep_finalfp, "@%d{%d} ", CurrentByteOffset - (text -curtextbegin), curtextend-curtextbegin);
 
2395
                                                        else {
 
2396
                                                                char s[32];
 
2397
                                                                int outindex;
 
2398
                                                                sprintf(s, "@%d{%d} ", CurrentByteOffset - (text -curtextbegin), curtextend-curtextbegin);
 
2399
                                                                for (outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
2400
                                                                                 (s[outindex] != '\0'); outindex ++) {
 
2401
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
2402
                                                                }
 
2403
                                                                if (s[outindex] != '\0') {
 
2404
                                                                        OUTPUT_OVERFLOW;
 
2405
                                                                        return -1;
 
2406
                                                                }
 
2407
                                                                agrep_outpointer += outindex;
 
2408
                                                        }
 
2409
                                                        PRINTED = 1;
 
2410
                                                }
 
2411
 
 
2412
                                                CurrentByteOffset += textbegin + 1 - text;
 
2413
                                                text = textbegin + 1;
 
2414
 
 
2415
                                                if (PRINTRECORD) {
 
2416
                                                if (TCOMPRESSED == ON) {
 
2417
#if     MEASURE_TIMES
 
2418
                                                        gettimeofday(&initt, NULL);
 
2419
#endif  /*MEASURE_TIMES*/
 
2420
                                                        if (agrep_finalfp != NULL)
 
2421
                                                                newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_finalfp, -1, EASYSEARCH);
 
2422
                                                        else {
 
2423
                                                                if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
2424
                                                                        if (agrep_outpointer + newlen + 1 >= agrep_outlen) {
 
2425
                                                                                OUTPUT_OVERFLOW;
 
2426
                                                                                return -1;
 
2427
                                                                        }
 
2428
                                                                        agrep_outpointer += newlen;
 
2429
                                                                }
 
2430
                                                        }
 
2431
#if     MEASURE_TIMES
 
2432
                                                        gettimeofday(&finalt, NULL);
 
2433
                                                        OUTFILTER_ms +=  (finalt.tv_sec*1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
 
2434
#endif  /*MEASURE_TIMES*/
 
2435
                                                }
 
2436
                                                else {
 
2437
                                                        if (agrep_finalfp != NULL) {
 
2438
                                                                fwrite(curtextbegin, 1, curtextend - curtextbegin, agrep_finalfp);
 
2439
                                                        }
 
2440
                                                        else {
 
2441
                                                                if (agrep_outpointer + curtextend - curtextbegin >= agrep_outlen) {
 
2442
                                                                        OUTPUT_OVERFLOW;
 
2443
                                                                        return -1;
 
2444
                                                                }
 
2445
                                                                memcpy(agrep_outbuffer+agrep_outpointer, curtextbegin, curtextend-curtextbegin);
 
2446
                                                                agrep_outpointer += curtextend - curtextbegin;
 
2447
                                                        }
 
2448
                                                }
 
2449
                                                }
 
2450
                                                else if (PRINTED) {
 
2451
                                                        if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);
 
2452
                                                        else agrep_outbuffer[agrep_outpointer ++] = '\n';
 
2453
                                                        PRINTED = 0;
 
2454
                                                }
 
2455
                                        }
 
2456
                                        else {  /* INVERSE */
 
2457
                                                if (!SILENT) {
 
2458
                                                if (TCOMPRESSED == ON) { /* INVERSE: Don't care about filtering time */
 
2459
                                                        if (agrep_finalfp != NULL)
 
2460
                                                                newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_finalfp, -1, EASYSEARCH);
 
2461
                                                        else {
 
2462
                                                                if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
2463
                                                                        if (newlen + agrep_outpointer >= agrep_outlen) {
 
2464
                                                                                OUTPUT_OVERFLOW;
 
2465
                                                                                return -1;
 
2466
                                                                        }
 
2467
                                                                        agrep_outpointer += newlen;
 
2468
                                                                }
 
2469
                                                        }
 
2470
                                                        lastout=textbegin;
 
2471
                                                        CurrentByteOffset += textbegin + 1 - text;
 
2472
                                                        text = textbegin + 1;
 
2473
                                                }
 
2474
                                                else { /* NOT TCOMPRESSED */
 
2475
                                                        if (agrep_finalfp != NULL)
 
2476
                                                                fwrite(lastout, 1, curtextbegin-lastout, agrep_finalfp);
 
2477
                                                        else {
 
2478
                                                                if (curtextbegin - lastout + agrep_outpointer >= agrep_outlen) {
 
2479
                                                                        OUTPUT_OVERFLOW;
 
2480
                                                                        return -1;
 
2481
                                                                }
 
2482
                                                                memcpy(agrep_outbuffer+agrep_outpointer, lastout, curtextbegin-lastout);
 
2483
                                                                agrep_outpointer += (curtextbegin - lastout);
 
2484
                                                        }
 
2485
                                                        lastout=textbegin;
 
2486
                                                        CurrentByteOffset += textbegin + 1 - text;
 
2487
                                                        text = textbegin + 1;
 
2488
                                                } /* TCOMPRESSED */
 
2489
                                                } /* !SILENT */
 
2490
                                        } /* INVERSE */
 
2491
                                }
 
2492
                                else {  /* COUNT */
 
2493
                                        CurrentByteOffset += textbegin + 1 - text;
 
2494
                                        text = textbegin + 1 ;
 
2495
                                }
 
2496
                                if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
 
2497
                                    ((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) return 0;   /* done */
 
2498
                        }
 
2499
                        else { CurrentByteOffset += (oldtext + m - text); text = oldtext + m; }
 
2500
                }
 
2501
                oldtext = text; 
 
2502
        }
 
2503
 
 
2504
        if (!SILENT && INVERSE && !COUNT && (lastout <= textend)) {
 
2505
                if (TCOMPRESSED == ON) { /* INVERSE: Don't care about filtering time */
 
2506
                        if (agrep_finalfp != NULL)
 
2507
                                newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_finalfp, -1, EASYSEARCH);
 
2508
                        else {
 
2509
                                if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
 
2510
                                        if (newlen + agrep_outpointer >= agrep_outlen) {
 
2511
                                                OUTPUT_OVERFLOW;
 
2512
                                                return -1;
 
2513
                                        }
 
2514
                                        agrep_outpointer += newlen;
 
2515
                                }
 
2516
                        }
 
2517
                }
 
2518
                else { /* NOT TCOMPRESSED */
 
2519
                        if (agrep_finalfp != NULL)
 
2520
                                fwrite(lastout, 1, textend-lastout + 1, agrep_finalfp);
 
2521
                        else {
 
2522
                                if (textend - lastout + 1 + agrep_outpointer >= agrep_outlen) {
 
2523
                                        OUTPUT_OVERFLOW;
 
2524
                                        return -1;
 
2525
                                }
 
2526
                                memcpy(agrep_outbuffer+agrep_outpointer, lastout, textend-lastout + 1);
 
2527
                                agrep_outpointer += (textend - lastout + 1);
 
2528
                        }
 
2529
                } /* TCOMPRESSED */
 
2530
        }
 
2531
 
 
2532
        return 0;
640
2533
}
641
 
 
 
2534
 
 
2535
static void
642
2536
prep4(Pattern, m)
643
 
char *Pattern; int m;
 
2537
char *Pattern; 
 
2538
int m;
644
2539
{
645
 
int i, j, k;
646
 
unsigned hash;
 
2540
        int i, j, k;
 
2541
        unsigned hash;
647
2542
 
648
 
for(i=0; i< MAXSYM; i++) char_map[i] = 0;
649
 
char_map['a'] = char_map['A'] = 4;
650
 
char_map['g'] = char_map['g'] = 1;
651
 
char_map['t'] = char_map['t'] = 2;
652
 
char_map['c'] = char_map['c'] = 3;
653
 
char_map['n'] = char_map['n'] = 5;
 
2543
        for(i=0; i< MAXSYM; i++) char_map[i] = 0;
 
2544
        char_map['a'] = char_map['A'] = 4;
 
2545
        char_map['g'] = char_map['g'] = 1;
 
2546
        char_map['t'] = char_map['t'] = 2;
 
2547
        char_map['c'] = char_map['c'] = 3;
 
2548
        char_map['n'] = char_map['n'] = 5;
654
2549
 
655
2550
        BSize = blog(4, m);
656
 
        for (i = 1, Hashmask = 1 ; i<BSize*LOG_DNA; i++) Hashmask = (Hashmask << 1) + 1 ;
 
2551
        for (i = 1, Hashmask = 1 ; i<(int)(BSize*LOG_DNA); i++) Hashmask = (Hashmask << 1) + 1 ;
 
2552
        if (MEMBER_D != NULL) free(MEMBER_D);
657
2553
        MEMBER_D = (char *) malloc((Hashmask+1)  * sizeof(char));
658
2554
#ifdef DEBUG
659
2555
        printf("BSize = %d", BSize);
660
2556
#endif 
661
2557
        for (i=0; i <= Hashmask; i++) MEMBER_D[i] = 0;
662
 
        for (j=0; j < BSize; j++) {
663
 
            for(i=m-1; i >= j; i--) {
664
 
               hash = 0;
665
 
               for(k=0; k <= j; k++) 
666
 
                  hash = (hash << LOG_DNA) +char_map[Pattern[i-k]]; 
 
2558
        for (j=0; j < (int)BSize; j++) {
 
2559
                for(i=m-1; i >= j; i--) {
 
2560
                        hash = 0;
 
2561
                        for(k=0; k <= j; k++) 
 
2562
                                hash = (hash << LOG_DNA) +char_map[Pattern[i-k]]; 
667
2563
#ifdef DEBUG
668
 
               printf("< %d >, ", hash);
 
2564
                        printf("< %d >, ", hash);
669
2565
#endif
670
 
               MEMBER_D[hash] = 1;
671
 
            }
672
 
        }
 
2566
                        MEMBER_D[hash] = 1;
 
2567
                }
 
2568
        }
673
2569
}
674
2570
 
 
2571
int
675
2572
blog(base, m )
676
2573
int base, m;
677
2574
{
678
 
int i, exp;
 
2575
        int i, exp;
679
2576
        exp = base;
680
 
        m = m + m/2;
 
2577
        m = m + m/2;
681
2578
        for (i = 1; exp < m; i++) exp = exp * base;
682
2579
        return(i);
683
2580
}