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. */
4
5
extern unsigned Init1, Init[], Mask[], endposition, D_endpos, AND, NO_ERR_MASK;
5
extern int DELIMITER, FILENAMEONLY, INVERSE;
6
extern int DELIMITER, FILENAMEONLY, INVERSE, PRINTFILETIME;
6
7
extern CHAR CurrentFileName[];
7
extern int I, num_of_matched, TRUNCATE;
8
extern long CurrentFileTime;
9
extern int I, num_of_matched, prev_num_of_matched, TRUNCATE;
11
extern int CurrentByteOffset;
13
extern CHAR *agrep_inbuffer;
14
extern int agrep_inlen;
15
extern int agrep_initialfd;
16
extern int EXITONERROR;
17
extern int agrep_inpointer;
19
extern FILE *agrep_finalfp;
20
extern CHAR *agrep_outbuffer;
21
extern int agrep_outlen;
22
extern int agrep_outpointer;
24
extern int NEW_FILE, POST_FILTER;
26
extern int LIMITOUTPUT, LIMITPERFILE;
9
29
asearch(old_D_pat, text, D)
10
CHAR old_D_pat[]; int text; register unsigned D;
12
register unsigned i, c, r1, r2, CMask, r_NO_ERR, r_Init1;
13
register unsigned A0, B0, A1, B1, endpos;
14
unsigned A2, B2, A3, B3, A4, B4;
15
unsigned A[MaxError+1], B[MaxError+1];
18
int D_length, FIRSTROUND, ResidueSize, lasti, l, k, buffer_end, j=0;
20
CHAR buffer[2*Max_record+1];
22
if (I == 0) Init1 = 037777777777;
24
asearch0(old_D_pat, text, D);
26
D_length = strlen(old_D_pat);
27
buffer[Max_record-1] = '\n';
29
for ( i=1; i<D_length; i++) D_Mask = (D_Mask<<1) | D_Mask;
32
r_Init1 = Init1; /* put Init1 in register */
33
r_NO_ERR = NO_ERR_MASK; /* put NO_ERR_MASK in register */
36
A0 = B0 = A1 = B1 = A2 = B2 = A3 = B3 = A4 = B4 = Init[0];
37
for(k=0; k<=D; k++) A[k] = B[k] = Init[0];
40
while ((l = fill_buf(text, buffer + Max_record, Max_record)) > 0)
41
{ i = Max_record; end = Max_record + l ;
45
for(k=0; k<D_length; k++) {
46
if(old_D_pat[k] != buffer[Max_record+k]) break;
52
strncpy(buffer+end, old_D_pat, D_length);
53
buffer[end+D_length] = '\0';
54
end = end + D_length; }
60
A0 = ((B0 >>1 ) & CMask) | r1;
62
r2 = B0 | (((A0 | B0) >> 1) & r_NO_ERR);
63
A1 = ((B1 >>1 ) & CMask) | r2 | r1 ;
64
if(D == 1) goto Nextchar;
66
r2 = B1 | (((A1 | B1) >> 1) & r_NO_ERR);
67
A2 = ((B2 >>1 ) & CMask) | r2 | r1 ;
68
if(D == 2) goto Nextchar;
70
r2 = B2 | (((A2 | B2) >> 1) & r_NO_ERR);
71
A3 = ((B3 >>1 ) & CMask) | r2 | r1 ;
72
if(D == 3) goto Nextchar;
74
r2 = B3 | (((A3 | B3) >> 1) & r_NO_ERR);
75
A4 = ((B4 >>1 ) & CMask) | r2 | r1 ;
76
if(D == 4) goto Nextchar;
84
if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
88
printf("%s\n", CurrentFileName);
90
printout_end = i - D_length - 1;
91
if(!(lasti >= Max_record + l - 1))
92
output(buffer, lasti, printout_end, j);
94
lasti = i - D_length; /* point to starting position of D_pat */
96
for(k=0; k<= D; k++) {
100
A[0] = (((B[0]>>1) & CMask) | r1) & D_Mask;
101
for(k=1; k<= D; k++) {
103
r2 = B[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
104
A[k] = (((B[k]>>1)&CMask) | r1 | r2) ;
106
A0 = A[0]; B0 = B[0]; A1 = A[1]; B1 = B[1]; A2 = A[2]; B2 = B[2];
107
A3 = A[3]; B3 = B[3]; A4 = A[4]; B4 = B[4];
112
B0 = ((A0 >> 1 ) & CMask) | r1;
114
printf("Mask = %o, B0 = %o\n", CMask, B0);
117
r2 = A0 | (((A0 | B0) >> 1) & r_NO_ERR);
118
B1 = ((A1 >>1 ) & CMask) | r2 | r1 ;
119
if(D == 1) goto Nextchar1;
121
r2 = A1 | (((A1 | B1) >> 1) & r_NO_ERR);
122
B2 = ((A2 >>1 ) & CMask) | r2 | r1 ;
123
if(D == 2) goto Nextchar1;
125
r2 = A2 | (((A2 | B2) >> 1) & r_NO_ERR);
126
B3 = ((A3 >>1 ) & CMask) | r2 | r1 ;
127
if(D == 3) goto Nextchar1;
129
r2 = A3 | (((A3 | B3) >> 1) & r_NO_ERR);
130
B4 = ((A4 >>1 ) & CMask) | r2 | r1 ;
131
if(D == 4) goto Nextchar1;
135
if ( D == 1) r1 = B1;
136
if ( D == 2) r1 = B2;
137
if ( D == 3) r1 = B3;
138
if ( D == 4) r1 = B4;
139
if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
143
printf("%s\n", CurrentFileName);
145
printout_end = i - D_length -1 ;
146
if(!(lasti >= Max_record + l - 1))
147
output(buffer, lasti, printout_end, j);
149
lasti = i - D_length ;
151
for(k=0; k<= D; k++) {
155
B[0] = (((A[0]>>1)&CMask) | r1) & D_Mask;
156
for(k=1; k<= D; k++) {
158
r2 = A[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
159
B[k] = (((A[k]>>1)&CMask) | r1 | r2) ;
161
A0 = A[0]; B0 = B[0]; A1 = A[1]; B1 = B[1]; A2 = A[2]; B2 = B[2];
162
A3 = A[3]; B3 = B[3]; A4 = A[4]; B4 = B[4];
169
ResidueSize = Max_record + l - lasti;
170
if(ResidueSize > Max_record) {
171
ResidueSize = Max_record;
173
strncpy(buffer+Max_record-ResidueSize, buffer+lasti, ResidueSize);
174
lasti = Max_record - ResidueSize;
175
if(lasti == 0) lasti = 1;
34
register unsigned i, c, r1, r2, CMask, r_NO_ERR, r_Init1;
35
register unsigned A0, B0, A1, B1, endpos;
36
unsigned A2, B2, A3, B3, A4, B4;
37
unsigned A[MaxError+1], B[MaxError+1];
40
int D_length, FIRSTROUND, ResidueSize, lasti, l, k, j=0;
43
/* CHAR *tempbuf = NULL; */ /* used only when text == -1 */
45
if (I == 0) Init1 = (unsigned)037777777777;
47
return asearch0(old_D_pat, text, D);
50
D_length = strlen(old_D_pat);
52
for ( i=1; i<D_length; i++) D_Mask = (D_Mask<<1) | D_Mask;
55
r_Init1 = Init1; /* put Init1 in register */
56
r_NO_ERR = NO_ERR_MASK; /* put NO_ERR_MASK in register */
59
A0 = B0 = A1 = B1 = A2 = B2 = A3 = B3 = A4 = B4 = Init[0];
60
for(k=0; k<=D; k++) A[k] = B[k] = Init[0];
64
#endif /*AGREP_POINTER*/
66
alloc_buf(text, &buffer, Max_record+BlockSize+1);
67
buffer[Max_record-1] = '\n';
69
while ((l = fill_buf(text, buffer + Max_record, BlockSize)) > 0)
72
end = Max_record + l ;
76
for(k=0; k<D_length; k++) {
77
if(old_D_pat[k] != buffer[Max_record+k]) break;
83
if (l < BlockSize) { /* copy pattern and '\0' at end of buffer */
84
strncpy(buffer+end, old_D_pat, D_length);
85
buffer[end+D_length] = '\0';
89
/* ASEARCH_PROCESS: the while-loop below */
95
A0 = ((B0 >>1 ) & CMask) | r1;
97
r2 = B0 | (((A0 | B0) >> 1) & r_NO_ERR);
98
A1 = ((B1 >>1 ) & CMask) | r2 | r1 ;
99
if(D == 1) goto Nextcharfile;
101
r2 = B1 | (((A1 | B1) >> 1) & r_NO_ERR);
102
A2 = ((B2 >>1 ) & CMask) | r2 | r1 ;
103
if(D == 2) goto Nextcharfile;
105
r2 = B2 | (((A2 | B2) >> 1) & r_NO_ERR);
106
A3 = ((B3 >>1 ) & CMask) | r2 | r1 ;
107
if(D == 3) goto Nextcharfile;
109
r2 = B3 | (((A3 | B3) >> 1) & r_NO_ERR);
110
A4 = ((B4 >>1 ) & CMask) | r2 | r1 ;
111
if(D == 4) goto Nextcharfile;
114
CurrentByteOffset ++;
117
if (DELIMITER) CurrentByteOffset -= D_length;
118
else CurrentByteOffset -= 1;
120
if ( D == 1) r1 = A1;
121
if ( D == 2) r1 = A2;
122
if ( D == 3) r1 = A3;
123
if ( D == 4) r1 = A4;
124
if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
126
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
129
if (agrep_finalfp != NULL)
130
fprintf(agrep_finalfp, "%s", CurrentFileName);
133
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
134
(CurrentFileName[outindex] != '\0'); outindex++) {
135
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
137
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
139
free_buf(text, buffer);
142
agrep_outpointer += outindex;
145
char *s = aprint_file_time(CurrentFileTime);
146
if (agrep_finalfp != NULL)
147
fprintf(agrep_finalfp, "%s", s);
150
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
151
(s[outindex] != '\0'); outindex++) {
152
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
154
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
156
free_buf(text, buffer);
159
agrep_outpointer += outindex;
162
if (agrep_finalfp != NULL)
163
fprintf(agrep_finalfp, "\n");
165
if (agrep_outpointer+1>=agrep_outlen) {
167
free_buf(text, buffer);
170
else agrep_outbuffer[agrep_outpointer++] = '\n';
175
memcpy(buffer+end-D_length, tempbuf, D_length+1);
178
free_buf(text, buffer);
182
printout_end = i - D_length - 1 ;
183
if ((text != -1) && !(lasti >= Max_record + l - 1)) {
184
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
186
else if ((text == -1) && !(lasti >= l)) {
187
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
189
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
190
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
191
free_buf(text, buffer);
195
lasti = i - D_length; /* point to starting position of D_pat */
197
for(k=0; k<= D; k++) {
201
A[0] = (((B[0]>>1) & CMask) | r1) & D_Mask;
202
for(k=1; k<= D; k++) {
204
r2 = B[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
205
A[k] = (((B[k]>>1)&CMask) | r1 | r2) ;
217
if (DELIMITER) CurrentByteOffset += 1*D_length;
218
else CurrentByteOffset += 1*1;
223
B0 = ((A0 >> 1 ) & CMask) | r1;
224
/* printf("Mask = %o, B0 = %on", CMask, B0); */
226
r2 = A0 | (((A0 | B0) >> 1) & r_NO_ERR);
227
B1 = ((A1 >>1 ) & CMask) | r2 | r1 ;
228
if(D == 1) goto Nextchar1file;
230
r2 = A1 | (((A1 | B1) >> 1) & r_NO_ERR);
231
B2 = ((A2 >>1 ) & CMask) | r2 | r1 ;
232
if(D == 2) goto Nextchar1file;
234
r2 = A2 | (((A2 | B2) >> 1) & r_NO_ERR);
235
B3 = ((A3 >>1 ) & CMask) | r2 | r1 ;
236
if(D == 3) goto Nextchar1file;
238
r2 = A3 | (((A3 | B3) >> 1) & r_NO_ERR);
239
B4 = ((A4 >>1 ) & CMask) | r2 | r1 ;
240
if(D == 4) goto Nextchar1file;
243
CurrentByteOffset ++;
246
if (DELIMITER) CurrentByteOffset -= D_length;
247
else CurrentByteOffset -= 1;
249
if ( D == 1) r1 = B1;
250
if ( D == 2) r1 = B2;
251
if ( D == 3) r1 = B3;
252
if ( D == 4) r1 = B4;
253
if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
255
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
257
free_buf(text, buffer);
259
if (agrep_finalfp != NULL)
260
fprintf(agrep_finalfp, "%s", CurrentFileName);
263
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
264
(CurrentFileName[outindex] != '\0'); outindex++) {
265
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
267
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
269
free_buf(text, buffer);
272
agrep_outpointer += outindex;
275
char *s = aprint_file_time(CurrentFileTime);
276
if (agrep_finalfp != NULL)
277
fprintf(agrep_finalfp, "%s", s);
280
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
281
(s[outindex] != '\0'); outindex++) {
282
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
284
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
286
free_buf(text, buffer);
289
agrep_outpointer += outindex;
292
if (agrep_finalfp != NULL)
293
fprintf(agrep_finalfp, "\n");
295
if (agrep_outpointer+1>=agrep_outlen) {
297
free_buf(text, buffer);
300
else agrep_outbuffer[agrep_outpointer++] = '\n';
305
memcpy(buffer+end-D_length, tempbuf, D_length+1);
308
free_buf(text, buffer);
312
printout_end = i - D_length - 1 ;
313
if((text != -1) && !(lasti >= Max_record + l - 1)) {
314
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
316
else if ((text == -1) && !(lasti >= l)) {
317
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
319
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
320
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
321
free_buf(text, buffer);
325
lasti = i - D_length ;
327
for(k=0; k<= D; k++) {
331
B[0] = (((A[0]>>1)&CMask) | r1) & D_Mask;
332
for(k=1; k<= D; k++) {
334
r2 = A[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
335
B[k] = (((A[k]>>1)&CMask) | r1 | r2) ;
347
if (DELIMITER) CurrentByteOffset += 1*D_length;
348
else CurrentByteOffset += 1*1;
356
ResidueSize = Max_record + l - lasti;
357
if(ResidueSize > Max_record) {
358
ResidueSize = Max_record;
361
strncpy(buffer+Max_record-ResidueSize, buffer+lasti, ResidueSize);
362
lasti = Max_record - ResidueSize;
363
if(lasti == 0) lasti = 1;
366
free_buf(text, buffer);
372
/* if (DELIMITER) tempbuf = (CHAR*)malloc(D_length + 1); */
373
buffer = (CHAR *)agrep_inbuffer;
376
/* buffer[end-1] = '\n'; */ /* at end of the text. */
377
/* buffer[0] = '\n'; */ /* in front of the text. */
380
for(k=0; k<D_length; k++) {
381
if(old_D_pat[k] != buffer[k]) break;
385
memcpy(tempbuf, buffer+end, D_length+1);
386
strncpy(buffer+end, old_D_pat, D_length);
387
buffer[end+D_length] = '\0';
388
end = end + D_length;
392
/* An exact copy of the above ASEARCH_PROCESS: the while-loop below */
398
A0 = ((B0 >>1 ) & CMask) | r1;
400
r2 = B0 | (((A0 | B0) >> 1) & r_NO_ERR);
401
A1 = ((B1 >>1 ) & CMask) | r2 | r1 ;
402
if(D == 1) goto Nextcharmem;
404
r2 = B1 | (((A1 | B1) >> 1) & r_NO_ERR);
405
A2 = ((B2 >>1 ) & CMask) | r2 | r1 ;
406
if(D == 2) goto Nextcharmem;
408
r2 = B2 | (((A2 | B2) >> 1) & r_NO_ERR);
409
A3 = ((B3 >>1 ) & CMask) | r2 | r1 ;
410
if(D == 3) goto Nextcharmem;
412
r2 = B3 | (((A3 | B3) >> 1) & r_NO_ERR);
413
A4 = ((B4 >>1 ) & CMask) | r2 | r1 ;
414
if(D == 4) goto Nextcharmem;
417
CurrentByteOffset ++;
420
if (DELIMITER) CurrentByteOffset -= D_length;
421
else CurrentByteOffset -= 1;
423
if ( D == 1) r1 = A1;
424
if ( D == 2) r1 = A2;
425
if ( D == 3) r1 = A3;
426
if ( D == 4) r1 = A4;
427
if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
429
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
432
if (agrep_finalfp != NULL)
433
fprintf(agrep_finalfp, "%s", CurrentFileName);
436
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
437
(CurrentFileName[outindex] != '\0'); outindex++) {
438
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
440
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
442
free_buf(text, buffer);
445
agrep_outpointer += outindex;
448
char *s = aprint_file_time(CurrentFileTime);
449
if (agrep_finalfp != NULL)
450
fprintf(agrep_finalfp, "%s", s);
453
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
454
(s[outindex] != '\0'); outindex++) {
455
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
457
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
459
free_buf(text, buffer);
462
agrep_outpointer += outindex;
465
if (agrep_finalfp != NULL)
466
fprintf(agrep_finalfp, "\n");
468
if (agrep_outpointer+1>=agrep_outlen) {
470
free_buf(text, buffer);
473
else agrep_outbuffer[agrep_outpointer++] = '\n';
478
memcpy(buffer+end-D_length, tempbuf, D_length+1);
481
free_buf(text, buffer);
485
printout_end = i - D_length - 1 ;
486
if ((text != -1) && !(lasti >= Max_record + l - 1)) {
487
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
489
else if ((text == -1) && !(lasti >= l)) {
490
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
492
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
493
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
494
free_buf(text, buffer);
498
lasti = i - D_length; /* point to starting position of D_pat */
500
for(k=0; k<= D; k++) {
504
A[0] = (((B[0]>>1) & CMask) | r1) & D_Mask;
505
for(k=1; k<= D; k++) {
507
r2 = B[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
508
A[k] = (((B[k]>>1)&CMask) | r1 | r2) ;
520
if (DELIMITER) CurrentByteOffset += 1*D_length;
521
else CurrentByteOffset += 1*1;
526
B0 = ((A0 >> 1 ) & CMask) | r1;
527
/* printf("Mask = %o, B0 = %on", CMask, B0); */
529
r2 = A0 | (((A0 | B0) >> 1) & r_NO_ERR);
530
B1 = ((A1 >>1 ) & CMask) | r2 | r1 ;
531
if(D == 1) goto Nextchar1mem;
533
r2 = A1 | (((A1 | B1) >> 1) & r_NO_ERR);
534
B2 = ((A2 >>1 ) & CMask) | r2 | r1 ;
535
if(D == 2) goto Nextchar1mem;
537
r2 = A2 | (((A2 | B2) >> 1) & r_NO_ERR);
538
B3 = ((A3 >>1 ) & CMask) | r2 | r1 ;
539
if(D == 3) goto Nextchar1mem;
541
r2 = A3 | (((A3 | B3) >> 1) & r_NO_ERR);
542
B4 = ((A4 >>1 ) & CMask) | r2 | r1 ;
543
if(D == 4) goto Nextchar1mem;
546
CurrentByteOffset ++;
549
if (DELIMITER) CurrentByteOffset -= D_length;
550
else CurrentByteOffset -= 1;
552
if ( D == 1) r1 = B1;
553
if ( D == 2) r1 = B2;
554
if ( D == 3) r1 = B3;
555
if ( D == 4) r1 = B4;
556
if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
558
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
560
free_buf(text, buffer);
562
if (agrep_finalfp != NULL)
563
fprintf(agrep_finalfp, "%s", CurrentFileName);
566
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
567
(CurrentFileName[outindex] != '\0'); outindex++) {
568
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
570
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
572
free_buf(text, buffer);
575
agrep_outpointer += outindex;
578
char *s = aprint_file_time(CurrentFileTime);
579
if (agrep_finalfp != NULL)
580
fprintf(agrep_finalfp, "%s", s);
583
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
584
(s[outindex] != '\0'); outindex++) {
585
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
587
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
589
free_buf(text, buffer);
592
agrep_outpointer += outindex;
595
if (agrep_finalfp != NULL)
596
fprintf(agrep_finalfp, "\n");
598
if (agrep_outpointer+1>=agrep_outlen) {
600
free_buf(text, buffer);
603
else agrep_outbuffer[agrep_outpointer++] = '\n';
608
memcpy(buffer+end-D_length, tempbuf, D_length+1);
611
free_buf(text, buffer);
615
printout_end = i - D_length - 1 ;
616
if((text != -1) && !(lasti >= Max_record + l - 1)) {
617
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
619
else if ((text == -1) && !(lasti >= l)) {
620
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
622
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
623
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
624
free_buf(text, buffer);
628
lasti = i - D_length ;
630
for(k=0; k<= D; k++) {
634
B[0] = (((A[0]>>1)&CMask) | r1) & D_Mask;
635
for(k=1; k<= D; k++) {
637
r2 = A[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
638
B[k] = (((A[k]>>1)&CMask) | r1 | r2) ;
650
if (DELIMITER) CurrentByteOffset += 1*D_length;
651
else CurrentByteOffset += 1*1;
657
memcpy(buffer+end, tempbuf, D_length+1);
663
#endif /*AGREP_POINTER*/
181
667
asearch0(old_D_pat, text, D)
182
CHAR old_D_pat[]; int text; register unsigned D;
184
register unsigned i, c, r1, r2, r3, CMask, r_NO_ERR, r_Init1, end, endpos;
185
unsigned A[MaxError+2], B[MaxError+2];
187
int D_length, FIRSTROUND, ResidueSize, lasti, l, k, buffer_end, j=0;
189
CHAR buffer[BlockSize+Max_record+1];
191
D_length = strlen(old_D_pat);
192
buffer[Max_record-1] = '\n';
194
for ( i=1; i<D_length; i++) D_Mask = (D_Mask<<1) | D_Mask;
197
r_Init1 = Init1; /* put Init1 in register */
198
r_NO_ERR = NO_ERR_MASK; /* put NO_ERR_MASK in register */
201
for(k=0; k<=D; k++) A[k] = B[k] = Init[0];
204
while ((l = fill_buf(text, buffer + Max_record, Max_record)) > 0)
205
{ i = Max_record; end = Max_record + l ;
210
strncpy(buffer+end, old_D_pat, D_length);
211
buffer[end+D_length] = '\0';
212
end = end + D_length; }
218
A[0] = (((B[0] >> 1)) & CMask | r1 ) ;
219
for(k=1; k<=D; k++) {
221
r2 = B[k-1] | (((A[k-1]|B[k-1])>>1) & r_NO_ERR);
222
A[k] = ((B[k] >> 1) & CMask) | r2 | r1;
227
if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
231
printf("%s\n", CurrentFileName);
233
printout_end = i - D_length - 1;
234
if(!(lasti >= Max_record + l - 1))
235
output(buffer, lasti, printout_end, j);
237
lasti = i - D_length; /* point to starting position of D_pat */
238
for(k=0; k<= D; k++) {
242
A[0] = (((B[0]>>1) & CMask) | r1) & D_Mask;
243
for(k=1; k<= D; k++) {
245
r2 = B[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
246
A[k] = (((B[k]>>1)&CMask) | r1 | r2) ;
252
B[0] = ((A[0] >> 1 ) & CMask) | r1;
253
for(k=1; k<=D; k++) {
255
r2 = A[k-1] | (((A[k-1]|B[k-1])>>1) & r_NO_ERR);
256
B[k] = ((A[k] >> 1) & CMask) | r2 | r1;
261
if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
265
printf("%s\n", CurrentFileName);
267
printout_end = i - D_length -1 ;
268
if(!(lasti >= Max_record + l - 1))
269
output(buffer, lasti, printout_end, j);
271
lasti = i - D_length ;
272
for(k=0; k<= D; k++) {
276
B[0] = (((A[0]>>1)&CMask) | r1) & D_Mask;
277
for(k=1; k<= D; k++) {
279
r2 = A[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
280
B[k] = (((A[k]>>1)&CMask) | r1 | r2) ;
288
ResidueSize = Max_record + l - lasti;
289
if(ResidueSize > Max_record) {
290
ResidueSize = Max_record;
292
strncpy(buffer+Max_record-ResidueSize, buffer+lasti, ResidueSize);
293
lasti = Max_record - ResidueSize;
294
if(lasti == 0) lasti = 1;
302
fill_buf(fd, buf, record_size)
303
int fd, record_size; unsigned char *buf;
307
while(total_read < record_size && num_read > 0) {
308
num_read = read(fd, buf+total_read, 4096);
309
total_read = total_read + num_read;
672
register unsigned i, c, r1, r2, CMask, r_NO_ERR, r_Init1, end, endpos;
673
unsigned A[MaxError+2], B[MaxError+2];
675
int D_length, FIRSTROUND, ResidueSize, lasti, l, k, j=0;
678
/* CHAR *tempbuf = NULL;*/ /* used only when text == -1 */
680
D_length = strlen(old_D_pat);
682
for ( i=1; i<D_length; i++) D_Mask = (D_Mask<<1) | D_Mask;
685
r_Init1 = Init1; /* put Init1 in register */
686
r_NO_ERR = NO_ERR_MASK; /* put NO_ERR_MASK in register */
689
for(k=0; k<=D; k++) A[k] = B[k] = Init[0];
693
#endif /*AGREP_POINTER*/
695
alloc_buf(text, &buffer, BlockSize+Max_record+1);
696
buffer[Max_record-1] = '\n';
697
while ((l = fill_buf(text, buffer + Max_record, BlockSize)) > 0)
700
end = Max_record + l ;
706
strncpy(buffer+end, old_D_pat, D_length);
707
buffer[end+D_length] = '\0';
708
end = end + D_length;
711
/* ASEARCH0_PROCESS: the while-loop below */
715
CurrentByteOffset ++;
718
A[0] = (((B[0] >> 1)) & CMask | r1 ) ;
719
for(k=1; k<=D; k++) {
721
r2 = B[k-1] | (((A[k-1]|B[k-1])>>1) & r_NO_ERR);
722
A[k] = ((B[k] >> 1) & CMask) | r2 | r1;
726
if (DELIMITER) CurrentByteOffset -= D_length;
727
else CurrentByteOffset -= 1;
729
if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
731
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
734
if (agrep_finalfp != NULL)
735
fprintf(agrep_finalfp, "%s", CurrentFileName);
738
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
739
(CurrentFileName[outindex] != '\0'); outindex++) {
740
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
742
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
744
free_buf(text, buffer);
747
agrep_outpointer += outindex;
750
char *s = aprint_file_time(CurrentFileTime);
751
if (agrep_finalfp != NULL)
752
fprintf(agrep_finalfp, "%s", s);
755
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
756
(s[outindex] != '\0'); outindex++) {
757
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
759
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
761
free_buf(text, buffer);
764
agrep_outpointer += outindex;
767
if (agrep_finalfp != NULL)
768
fprintf(agrep_finalfp, "\n");
770
if (agrep_outpointer+1>=agrep_outlen) {
772
free_buf(text, buffer);
775
else agrep_outbuffer[agrep_outpointer++] = '\n';
780
memcpy(buffer+end-D_length, tempbuf, D_length+1);
783
free_buf(text, buffer);
787
printout_end = i - D_length - 1;
788
if((text != -1) && !(lasti >= Max_record + l - 1)) {
789
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
791
else if ((text == -1) && !(lasti >= l)) {
792
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
794
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
795
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
796
free_buf(text, buffer);
800
lasti = i - D_length; /* point to starting position of D_pat */
801
for(k=0; k<= D; k++) {
805
A[0] = (((B[0]>>1) & CMask) | r1) & D_Mask;
806
for(k=1; k<= D; k++) {
808
r2 = B[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
809
A[k] = (((B[k]>>1)&CMask) | r1 | r2) ;
811
if (DELIMITER) CurrentByteOffset += 1*D_length;
812
else CurrentByteOffset += 1*1;
815
CurrentByteOffset ++;
818
B[0] = ((A[0] >> 1 ) & CMask) | r1;
819
for(k=1; k<=D; k++) {
821
r2 = A[k-1] | (((A[k-1]|B[k-1])>>1) & r_NO_ERR);
822
B[k] = ((A[k] >> 1) & CMask) | r2 | r1;
826
if (DELIMITER) CurrentByteOffset -= D_length;
827
else CurrentByteOffset -= 1;
829
if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
831
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
834
if (agrep_finalfp != NULL)
835
fprintf(agrep_finalfp, "%s", CurrentFileName);
838
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
839
(CurrentFileName[outindex] != '\0'); outindex++) {
840
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
842
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
844
free_buf(text, buffer);
847
agrep_outpointer += outindex;
850
char *s = aprint_file_time(CurrentFileTime);
851
if (agrep_finalfp != NULL)
852
fprintf(agrep_finalfp, "%s", s);
855
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
856
(s[outindex] != '\0'); outindex++) {
857
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
859
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
861
free_buf(text, buffer);
864
agrep_outpointer += outindex;
867
if (agrep_finalfp != NULL)
868
fprintf(agrep_finalfp, "\n");
870
if (agrep_outpointer+1>=agrep_outlen) {
872
free_buf(text, buffer);
875
else agrep_outbuffer[agrep_outpointer++] = '\n';
880
memcpy(buffer+end-D_length, tempbuf, D_length+1);
883
free_buf(text, buffer);
887
printout_end = i - D_length -1 ;
888
if((text != -1) && !(lasti >= Max_record + l - 1)) {
889
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
891
else if ((text == -1) && !(lasti >= l)) {
892
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
894
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
895
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
896
free_buf(text, buffer);
900
lasti = i - D_length ;
901
for(k=0; k<= D; k++) {
905
B[0] = (((A[0]>>1)&CMask) | r1) & D_Mask;
906
for(k=1; k<= D; k++) {
908
r2 = A[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
909
B[k] = (((A[k]>>1)&CMask) | r1 | r2) ;
911
if (DELIMITER) CurrentByteOffset += 1*D_length;
912
else CurrentByteOffset += 1*1;
920
ResidueSize = Max_record + l - lasti;
921
if(ResidueSize > Max_record) {
922
ResidueSize = Max_record;
925
strncpy(buffer+Max_record-ResidueSize, buffer+lasti, ResidueSize);
926
lasti = Max_record - ResidueSize;
927
if(lasti == 0) lasti = 1;
930
free_buf(text, buffer);
936
/* if (DELIMITER) tempbuf = (CHAR*)malloc(D_length + 1); */
937
buffer = (CHAR *)agrep_inbuffer;
940
/* buffer[end-1] = '\n';*/ /* at end of the text. */
941
/* buffer[0] = '\n';*/ /* in front of the text. */
944
for(k=0; k<D_length; k++) {
945
if(old_D_pat[k] != buffer[k]) break;
949
memcpy(tempbuf, buffer+end, D_length+1);
950
strncpy(buffer+end, old_D_pat, D_length);
951
buffer[end+D_length] = '\0';
952
end = end + D_length;
956
/* An exact copy of the above ASEARCH0_PROCESS: the while-loop below */
960
CurrentByteOffset ++;
963
A[0] = (((B[0] >> 1)) & CMask | r1 ) ;
964
for(k=1; k<=D; k++) {
966
r2 = B[k-1] | (((A[k-1]|B[k-1])>>1) & r_NO_ERR);
967
A[k] = ((B[k] >> 1) & CMask) | r2 | r1;
970
if (DELIMITER) CurrentByteOffset -= D_length;
971
else CurrentByteOffset -= 1;
974
if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
976
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
979
if (agrep_finalfp != NULL)
980
fprintf(agrep_finalfp, "%s", CurrentFileName);
983
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
984
(CurrentFileName[outindex] != '\0'); outindex++) {
985
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
987
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
989
free_buf(text, buffer);
992
agrep_outpointer += outindex;
995
char *s = aprint_file_time(CurrentFileTime);
996
if (agrep_finalfp != NULL)
997
fprintf(agrep_finalfp, "%s", s);
1000
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1001
(s[outindex] != '\0'); outindex++) {
1002
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
1004
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1006
free_buf(text, buffer);
1009
agrep_outpointer += outindex;
1012
if (agrep_finalfp != NULL)
1013
fprintf(agrep_finalfp, "\n");
1015
if (agrep_outpointer+1>=agrep_outlen) {
1017
free_buf(text, buffer);
1020
else agrep_outbuffer[agrep_outpointer++] = '\n';
1025
memcpy(buffer+end-D_length, tempbuf, D_length+1);
1028
free_buf(text, buffer);
1032
printout_end = i - D_length - 1;
1033
if((text != -1) && !(lasti >= Max_record + l - 1)) {
1034
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
1036
else if ((text == -1) && !(lasti >= l)) {
1037
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
1039
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
1040
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
1041
free_buf(text, buffer);
1042
return 0; /* done */
1045
lasti = i - D_length; /* point to starting position of D_pat */
1046
for(k=0; k<= D; k++) {
1049
r1 = B[0] & r_Init1;
1050
A[0] = (((B[0]>>1) & CMask) | r1) & D_Mask;
1051
for(k=1; k<= D; k++) {
1053
r2 = B[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
1054
A[k] = (((B[k]>>1)&CMask) | r1 | r2) ;
1056
if (DELIMITER) CurrentByteOffset += 1*D_length;
1057
else CurrentByteOffset += 1*1;
1060
CurrentByteOffset ++;
1062
r1 = r_Init1 & A[0];
1063
B[0] = ((A[0] >> 1 ) & CMask) | r1;
1064
for(k=1; k<=D; k++) {
1065
r1 = r_Init1 & A[k];
1066
r2 = A[k-1] | (((A[k-1]|B[k-1])>>1) & r_NO_ERR);
1067
B[k] = ((A[k] >> 1) & CMask) | r2 | r1;
1071
if (DELIMITER) CurrentByteOffset -= D_length;
1072
else CurrentByteOffset -= 1;
1074
if(((AND == 1) && ((r1 & endposition) == endposition)) || ((AND == 0) && (r1 & endposition)) ^ INVERSE )
1076
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
1079
if (agrep_finalfp != NULL)
1080
fprintf(agrep_finalfp, "%s", CurrentFileName);
1083
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1084
(CurrentFileName[outindex] != '\0'); outindex++) {
1085
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
1087
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1089
free_buf(text, buffer);
1092
agrep_outpointer += outindex;
1094
if (PRINTFILETIME) {
1095
char *s = aprint_file_time(CurrentFileTime);
1096
if (agrep_finalfp != NULL)
1097
fprintf(agrep_finalfp, "%s", s);
1100
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1101
(s[outindex] != '\0'); outindex++) {
1102
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
1104
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1106
free_buf(text, buffer);
1109
agrep_outpointer += outindex;
1112
if (agrep_finalfp != NULL)
1113
fprintf(agrep_finalfp, "\n");
1115
if (agrep_outpointer+1>=agrep_outlen) {
1117
free_buf(text, buffer);
1120
else agrep_outbuffer[agrep_outpointer++] = '\n';
1125
memcpy(buffer+end-D_length, tempbuf, D_length+1);
1128
free_buf(text, buffer);
1132
printout_end = i - D_length -1 ;
1133
if((text != -1) && !(lasti >= Max_record + l - 1)) {
1134
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
1136
else if ((text == -1) && !(lasti >= l)) {
1137
if (-1 == output(buffer, lasti, printout_end, j)) {free_buf(text, buffer); return -1;}
1139
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
1140
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
1141
free_buf(text, buffer);
1142
return 0; /* done */
1145
lasti = i - D_length ;
1146
for(k=0; k<= D; k++) {
1149
r1 = A[0] & r_Init1;
1150
B[0] = (((A[0]>>1)&CMask) | r1) & D_Mask;
1151
for(k=1; k<= D; k++) {
1152
r1 = r_Init1 & A[k];
1153
r2 = A[k-1] | (((A[k-1] | B[k-1])>>1)&r_NO_ERR);
1154
B[k] = (((A[k]>>1)&CMask) | r1 | r2) ;
1156
if (DELIMITER) CurrentByteOffset += 1*D_length;
1157
else CurrentByteOffset += 1*1;
1163
memcpy(buffer+end, tempbuf, D_length+1);
1169
#endif /*AGREP_POINTER*/