-
Notifications
You must be signed in to change notification settings - Fork 50
/
sgrep.c
2528 lines (2305 loc) · 74.6 KB
/
sgrep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal. All Rights Reserved. */
/* #define DEBUG2 */
/* #define DEBUG */
/*
[chg] 05.10.96 sgrep():
- when the last buffer is read:
no need to look back to the previous CR,
just process until (end) = (buf_end)
[chg] bm():
- when there is a hit at the last line of a file,
and the file has not a CR as the last character,
now the print buffer is terminated with an artifical
CR.
[chg] 04.10.96 bm(): major bugs in the algorithm removed [TG]
[chg] 22.09.96 UL850[].lower_1 -> UL850.lower [TG]
[chg] 21.08.96 header file ISO_CHAR [TG]
[chg] 13.08.96 edited by Tom Gries <gries@ibm.net>
*/
#include <stdio.h>
#include <ctype.h>
#include "agrep.h"
#include "codepage.h"
/* TG 29.04.04 */
#include <errno.h>
extern unsigned char LUT[256];
#undef MAXSYM
#define MAXSYM 256
#define MAXMEMBER 8192
#define CHARTYPE unsigned char
#undef MaxError /* don't use agrep.h definition */
#define MaxError 20
#define MAXPATT 256
#undef MAXLINE
#define MAXLINE 1024
#undef MAXNAME
#define MAXNAME 256
#undef MaxCan /* don't use agrep.h definition */
#define MaxCan 2048
#define BLOCKSIZE 16384
#define MAX_SHIFT_2 4096
#undef ON
#define ON 1
#undef OFF
#define OFF 0
#define LOG_ASCII 8
#define LOG_DNA 3
#define MAXMEMBER_1 65536
#define LONG_EXAC 20
#define LONG_APPX 24
#if ISO_CHAR_SET
#define W_DELIM 256
#else
#define W_DELIM 128
#endif
#ifndef _WIN32
#include <sys/time.h>
#else
#include <sys/timeb.h>
#endif
extern int tuncompressible();
extern int quick_tcompress();
extern int quick_tuncompress();
extern int DELIMITER, OUTTAIL;
extern int D_length, tc_D_length;
extern unsigned char D_pattern[MaxDelimit *2], tc_D_pattern[MaxDelimit *2];
extern int LIMITOUTPUT, LIMITPERFILE, INVERSE;
extern int CurrentByteOffset;
extern int BYTECOUNT;
extern int PRINTOFFSET;
extern int PRINTRECORD;
extern int CONSTANT, COUNT, FNAME, SILENT, FILENAMEONLY, prev_num_of_matched, num_of_matched;
extern int DNA ; /* DNA flag is set in checksg when pattern is DNA pattern
and p_size > 16 */
extern WORDBOUND, WHOLELINE, NOUPPER;
extern unsigned char CurrentFileName[], Progname[];
extern unsigned Mask[];
extern unsigned endposition;
extern int agrep_inlen;
extern CHARTYPE *agrep_inbuffer;
extern int agrep_initialfd;
extern FILE *agrep_finalfp;
extern int agrep_outpointer;
extern int agrep_outlen;
extern CHARTYPE * agrep_outbuffer;
extern int NEW_FILE, POST_FILTER;
extern int EXITONERROR;
extern int errno;
extern int TCOMPRESSED;
extern int EASYSEARCH;
extern char FREQ_FILE[MAX_LINE_LEN], HASH_FILE[MAX_LINE_LEN], STRING_FILE[MAX_LINE_LEN];
extern void alloc_buf(int fd, unsigned char **sbuf, int size);
extern void free_buf(int fd, char *sbuf);
extern unsigned char * backward_delimiter(unsigned char *end, unsigned char *begin, unsigned char *delim, int len, int outtail);
extern unsigned char * forward_delimiter(unsigned char *begin, unsigned char *end, unsigned char *delim, int len, int outtail);
#ifdef _WIN32
int fill_buf(); /* bitap.c */
int a_monkey(); /* sgrep.c */
int agrep(); /* sgrep.c */
int bm(); /* sgrep.c */
int blog(); /* sgrep.c */
int monkey(); /* sgrep.c */
int monkey4(); /* sgrep.c */
int s_output(); /* sgrep.c */
int verify(); /* sgrep.c */
#endif
#if MEASURE_TIMES
/* timing variables */
extern int OUTFILTER_ms;
extern int FILTERALGO_ms;
extern int INFILTER_ms;
#endif /*MEASURE_TIMES*/
unsigned char BSize; /* log_c m */
unsigned char char_map[MAXSYM];
/* data area */
int shift_1;
CHARTYPE SHIFT[MAXSYM];
CHARTYPE MEMBER[MAXMEMBER];
CHARTYPE pat[MAXPATT];
unsigned Hashmask;
char MEMBER_1[MAXMEMBER_1];
CHARTYPE TR[MAXSYM];
static void initmask();
static void am_preprocess();
static void m_preprocess();
static void prep();
static void prep4();
static void prep_bm();
/*
* General idea behind output processing with delimiters, inverse, compression, etc.
* CAUTION: In compressed files, we can search ONLY for simple patterns or their ;,.
* Attempts to search for complex patterns / with errors might lead to spurious matches.
* 1. Once we find the match, go back and forward to get the delimiters that surround
* the matched region.
* 2. If it is a compressed file, verify that the match is "real" (compressed files
* can have pseudo matches hence this filtering step is required).
* 3. Increment num_of_matched.
* 4. Process some output options which print stuff before the matched region is
* printed.
* 5. If there is compression, decomress and output the matched region. Otherwise
* just output it as is. Remember, from step (1) we know the matched region.
* 6. If inverse is set, then we must keep track of the end of the last matched region
* in the variable lastout. When there is a match, we must print everything from
* lastout to the beginning of the current matched region (curtextbegin) and then
* update lastout to point to the end of the current matched region (curtextend).
* ALSO: if we exit from the main loops, we must output everything from the end
* of the last matched region to the end of the input buffer.
* 7. Delimiter handling in complex patterns is different: there the search is done
* for a boolean and of the delimiter pattern and the actual pattern.
*/
/* skips over escaped characters */
unsigned char *
mystrchr(s, c)
unsigned char *s;
int c;
{
unsigned char *t = s;
while (*t) {
if (*t == '\\') t++;
else if (c == *t) return t;
t ++;
}
return NULL;
}
void
char_tr(pat, m)
unsigned char *pat;
int *m;
{
int i;
unsigned char temp[MAXPATT];
for(i=0; i<MAXSYM; i++) TR[i] = i;
/* if(NOUPPER) [TG] */ {
for(i=0; i<MAXSYM; i++)
#if (defined(__EMX__) && defined(ISO_CHAR_SET))
TR[i] = TR[LUT[i]];
#else
if (isupper(i)) TR[i] = TR[tolower(i)];
#endif
}
/*
if(WORDBOUND) {
for(i=0; i<MAXSYM; i++) {
if(!isalnum(i)) TR[i] = W_DELIM; removed by Udi.
we don't use the trick of making the boundary W_delim anymore.
It's too buggy otherwise and it's not necessary.
}
}
removed by bg 11/8/94
*/
if(WHOLELINE) {
memcpy(temp, pat, *m);
pat[0] = '\n';
memcpy(pat+1, temp, *m);
pat[*m+1] = '\n';
pat[*m+2] = 0;
*m = *m + 2;
}
}
int sgrep(in_pat, in_m, fd, D, samepattern)
CHARTYPE *in_pat;
int fd, in_m, D;
{
CHARTYPE patbuf[MAXLINE];
CHARTYPE *pat = patbuf;
int m = in_m;
CHARTYPE *text; /* input text stream */
int offset = 2*MAXLINE;
int buf_end, num_read, i, start, end, residue = 0;
int first_time = 1;
CHARTYPE *oldpat = pat;
int k, j, oldm = m;
static CHARTYPE newpat[MAXLINE]; /* holds compressed version */
static int newm;
#if MEASURE_TIMES
static struct timeval initt, finalt;
#endif
CHARTYPE *tempbuf;
int oldCurrentByteOffset;
strncpy(pat, in_pat, MAXLINE);
pat[MAXLINE-1] = '\0';
#define PROCESS_PATTERN \
if (!CONSTANT) {\
if( (pat[0] == '^') || (pat[0] == '$') ) pat[0] = '\n';\
if ((m>1) && (pat[m-2] != '\\') && ((pat[m-1] == '^') || (pat[m-1] == '$'))) pat[m-1] = '\n';\
}\
/* whether constant or not, interpret the escape character */\
for (k=0; k<m; k++) {\
if (pat[k] == '\\') {\
for (j=k; j<m; j++)\
pat[j] = pat[j+1]; /* including '\0' */\
m--;\
}\
}\
char_tr(pat, &m); /* will change pat, and m if WHOLELINE is ON */\
if(m >= MAXPATT) {\
fprintf(stderr, "%s: pattern too long (has > %d chars)\n", Progname, MAXPATT);\
if (!EXITONERROR) {\
errno = AGREP_ERROR;\
return -1;\
}\
else exit(2);\
}\
if(D == 0) {\
if(m > LONG_EXAC) m_preprocess(pat);\
else prep_bm(pat, m);\
}\
else if (DNA) prep4(pat, m);\
else if(m >= LONG_APPX) am_preprocess(pat);\
else {\
prep(pat, m, D);\
initmask(pat, Mask, m, 0, &endposition);\
}
#if AGREP_POINTER
if (fd != -1) {
#endif /*AGREP_POINTER*/
alloc_buf(fd, &text, 2*BLOCKSIZE+2*MAXLINE+MAXPATT);
text[offset-1] = '\n'; /* initial case */
for(i=0; i < MAXLINE; i++) text[i] = 0; /* security zone */
start = offset;
if(WHOLELINE) {
start--;
CurrentByteOffset --;
}
while( (num_read = fill_buf(fd, text+offset, 2*BLOCKSIZE)) > 0)
{
buf_end = end = offset + num_read -1 ;
oldCurrentByteOffset = CurrentByteOffset;
if (first_time) {
if ((TCOMPRESSED == ON) && tuncompressible(text+offset, num_read)) {
EASYSEARCH = text[offset+SIGNATURE_LEN-1];
start += SIGNATURE_LEN;
CurrentByteOffset += SIGNATURE_LEN;
if (!EASYSEARCH) {
fprintf(stderr, "not compressed for easy-search: can miss some matches in: %s\n", CurrentFileName);
}
#if MEASURE_TIMES
gettimeofday(&initt, NULL);
#endif /*MEASURE_TIMES*/
if (samepattern || ((newm = quick_tcompress(FREQ_FILE, HASH_FILE, pat, m, newpat, MAXLINE-8, EASYSEARCH)) > 0)) {
oldm = m;
oldpat = pat;
m = newm;
pat = newpat;
}
#if MEASURE_TIMES
gettimeofday(&finalt, NULL);
INFILTER_ms += (finalt.tv_sec*1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
#endif /*MEASURE_TIMES*/
}
else TCOMPRESSED = OFF;
PROCESS_PATTERN /* must be AFTER we know that it is a compressed pattern... */
/* to make sure the skip loop in bm() won't go out of bound in later iterations:
This was the original code.
It will be inefficient to place a copy of the pattern at the end
of the buffer. Better put that stop pattern to the end of the
_actual_ read block (end) [TG] 04.10.96 */
/* for(i=1; i<=m; i++) text[2*BLOCKSIZE+offset+i] = pat[m-1]; [del] [TG] */
/* Emergency Stop: put one copy of pattern to the end of the buffer
to make sure that the skip loop in bm()
won't go out of bound in later iterations */
/* save portion being overwritten.
copied from below (memagrep()), but not need here: */
/* memcpy(tempbuf, text+end+1, m); */
for(i=1; i<=m; i++) text[end+i] = pat[m-1]; /* [new] [TG] */
first_time = 0;
}
if (!DELIMITER) {
/* [TG] */ if (num_read == 2*BLOCKSIZE) {
while ((text[end] != '\n') && (end > offset)) end--;
}
/* else end = buf_end; no need to look back [TG] */
text[start-1] = '\n';
}
else {
unsigned char *newbuf = text + end + 1;
newbuf = backward_delimiter(newbuf, text+offset, D_pattern, D_length, OUTTAIL); /* see agrep.c/'d' */
if (newbuf < text+offset+D_length) newbuf = text + end + 1;
end = newbuf - text - 1;
memcpy(text+start-D_length, D_pattern, D_length);
}
residue = buf_end - end + 1 ;
/* SGREP_PROCESS */
/* No harm in sending a few extra parameters even if they are unused: they are not accessed in monkey*()s */
if(D==0) {
if(m > LONG_EXAC) {
if (-1 == monkey(pat, m, text+start, text+end, oldpat, oldm)) {
free_buf(fd, text);
return -1;
}
}
else {
if (-1 == bm(pat, m, text+start, text+end, oldpat, oldm)) {
free_buf(fd, text);
return -1;
}
}
}
else {
if(DNA) {
if (-1 == monkey4( pat, m, text+start, text+end, D , oldpat, oldm )) {
free_buf(fd, text);
return -1;
}
}
else {
if(m >= LONG_APPX) {
if (-1 == a_monkey(pat, m, text+start, text+end, D, oldpat, oldm)) {
free_buf(fd, text);
return -1;
}
}
else {
if (-1 == agrep(pat, m, text+start, text+end, D, oldpat, oldm)) {
free_buf(fd, text);
return -1;
}
}
}
}
if(FILENAMEONLY && (num_of_matched - prev_num_of_matched) && (NEW_FILE || !POST_FILTER)) {
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s\n", CurrentFileName);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(fd, text);
return -1;
}
else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
agrep_outpointer += outindex;
}
free_buf(fd, text);
NEW_FILE = OFF;
return 0;
}
CurrentByteOffset = oldCurrentByteOffset + end - start + 1; /* for a new iteration: avoid complicated calculations below */
start = offset - residue ;
if(start < MAXLINE) {
start = MAXLINE;
}
strncpy(text+start, text+end, residue);
start++;
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
free_buf(fd, text);
return 0; /* done */
}
} /* end of while(num_read = ...) */
if (!DELIMITER) {
text[start-1] = '\n';
text[start+residue] = '\n';
}
else {
if (start > D_length) memcpy(text+start-D_length, D_pattern, D_length);
memcpy(text+start+residue, D_pattern, D_length);
}
end = start + residue - 2;
if(residue > 1) {
/* SGREP_PROCESS */
/* No harm in sending a few extra parameters even if they are unused: they are not accessed in monkey*()s */
if(D==0) {
if(m > LONG_EXAC) {
if (-1 == monkey(pat, m, text+start, text+end, oldpat, oldm)) {
free_buf(fd, text);
return -1;
}
}
else {
if (-1 == bm(pat, m, text+start, text+end, oldpat, oldm)) {
free_buf(fd, text);
return -1;
}
}
}
else {
if(DNA) {
if (-1 == monkey4( pat, m, text+start, text+end, D , oldpat, oldm )) {
free_buf(fd, text);
return -1;
}
}
else {
if(m >= LONG_APPX) {
if (-1 == a_monkey(pat, m, text+start, text+end, D, oldpat, oldm)) {
free_buf(fd, text);
return -1;
}
}
else {
if (-1 == agrep(pat, m, text+start, text+end, D, oldpat, oldm)) {
free_buf(fd, text);
return -1;
}
}
}
}
if(FILENAMEONLY && (num_of_matched - prev_num_of_matched) && (NEW_FILE || !POST_FILTER)) {
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s\n", CurrentFileName);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(fd, text);
return -1;
}
else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
agrep_outpointer += outindex;
}
free_buf(fd, text);
NEW_FILE = OFF;
return 0;
}
}
free_buf(fd, text);
return 0;
#if AGREP_POINTER
}
else { /* as if only one iteration of the while-loop and offset = 0 */
tempbuf = (CHARTYPE*)malloc(m);
text = (CHARTYPE *)agrep_inbuffer;
num_read = agrep_inlen;
start = 0;
buf_end = end = num_read - 1;
#if 0
if (WHOLELINE) {
start --;
CurrentByteOffset --;
}
#endif
if ((TCOMPRESSED == ON) && tuncompressible(text+1, num_read)) {
EASYSEARCH = text[offset+SIGNATURE_LEN-1];
start += SIGNATURE_LEN;
CurrentByteOffset += SIGNATURE_LEN;
if (!EASYSEARCH) {
fprintf(stderr, "not compressed for easy-search: can miss some matches in: %s\n", CurrentFileName);
}
#if MEASURE_TIMES
gettimeofday(&initt, NULL);
#endif /*MEASURE_TIMES*/
if (samepattern || ((newm = quick_tcompress(FREQ_FILE, HASH_FILE, pat, m, newpat, MAXLINE-8, EASYSEARCH)) > 0)) {
oldm = m;
oldpat = pat;
m = newm;
pat = newpat;
}
#if MEASURE_TIMES
gettimeofday(&finalt, NULL);
INFILTER_ms += (finalt.tv_sec*1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
#endif /*MEASURE_TIMES*/
}
else TCOMPRESSED = OFF;
PROCESS_PATTERN /* must be after we know whether it is compressed or not */
/* Emergency Stop: put one copy of pattern to the end of the buffer
to make sure that the skip loop in bm()
won't go out of bound in later iterations */
memcpy(tempbuf, text+end+1, m); /* save portion being overwritten */
for(i=1; i<=m; i++) text[end+i] = pat[m-1];
if (!DELIMITER)
while(text[end] != '\n' && end > 1) end--;
else {
unsigned char *newbuf = text + end + 1;
newbuf = backward_delimiter(newbuf, text, D_pattern, D_length, OUTTAIL); /* see agrep.c/'d' */
if (newbuf < text+offset+D_length) newbuf = text + end + 1;
end = newbuf - text - 1;
}
/* text[0] = text[end] = r_newline; : the user must ensure that the delimiter is there at text[0] and occurs somewhere before text[end ] */
/* An exact copy of the above SGREP_PROCESS */
/* No harm in sending a few extra parameters even if they are unused: they are not accessed in monkey*()s */
if(D==0) {
if(m > LONG_EXAC) {
if (-1 == monkey(pat, m, text+start, text+end, oldpat, oldm)) {
free_buf(fd, text);
memcpy(text+end+1, tempbuf, m); /* restore */
free(tempbuf);
return -1;
}
}
else {
if (-1 == bm(pat, m, text+start, text+end, oldpat, oldm)) {
free_buf(fd, text);
memcpy(text+end+1, tempbuf, m); /* restore */
free(tempbuf);
return -1;
}
}
}
else {
if(DNA) {
if (-1 == monkey4( pat, m, text+start, text+end, D , oldpat, oldm )) {
free_buf(fd, text);
memcpy(text+end+1, tempbuf, m); /* restore */
free(tempbuf);
return -1;
}
}
else {
if(m >= LONG_APPX) {
if (-1 == a_monkey(pat, m, text+start, text+end, D, oldpat, oldm)) {
free_buf(fd, text);
memcpy(text+end+1, tempbuf, m); /* restore */
free(tempbuf);
return -1;
}
}
else {
if (-1 == agrep(pat, m, text+start, text+end, D, oldpat, oldm)) {
free_buf(fd, text);
memcpy(text+end+1, tempbuf, m); /* restore */
free(tempbuf);
return -1;
}
}
}
}
if(FILENAMEONLY && (num_of_matched - prev_num_of_matched) && (NEW_FILE || !POST_FILTER)) { /* externally set */
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s\n", CurrentFileName);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(fd, text);
memcpy(text+end+1, tempbuf, m); /* restore */
free(tempbuf);
return -1;
}
else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
agrep_outpointer += outindex;
}
free_buf(fd, text);
NEW_FILE = OFF;
}
memcpy(text+end+1, tempbuf, m); /* restore */
free(tempbuf);
return 0;
}
#endif /*AGREP_POINTER*/
} /* end sgrep */
/* SUN:
BOYER-MOORE.
Our implementation of bm assumes
that the content of text[n]...text[n+m-1] is pat[m-1] (emergency stop)
such that the skip loop is guaranteed to terminated.
*/
int bm(pat, m, text, textend, oldpat, oldm)
CHARTYPE *text, *textend, *pat, *oldpat;
int m, oldm;
{
int PRINTED = 0;
register int shift;
register int m1, j, d1;
CHARTYPE *textbegin = text;
int newlen;
CHARTYPE *textstart;
CHARTYPE *curtextbegin;
CHARTYPE *curtextend;
#if MEASURE_TIMES
struct timeval initt, finalt;
#endif
CHARTYPE *lastout = text;
d1 = shift_1; /* at least 1 */
m1 = m - 1;
shift = 0; /* to start with the skip loop: assume,
the first character is a match. */
#ifdef DEBUG2
printf("***BM-START*** textend=%d=%c=\n",textend,*textend);
#endif
/* The original loop was: while (text <= textend) [TG] 04.10.96 */
while (text < textend) {
textstart = text;
#ifdef DEBUG2
printf("shift=%d text=%d=%c\n",shift,text,*text);
#endif
/* the skip-loop: skip until a match is found (shift=0) */
while(shift) {
shift = SHIFT[*(text += shift)];
#ifdef DEBUG2
printf("shift=%d text=%d=%c\n",shift,text,*text);
#endif
}
CurrentByteOffset += text - textstart;
j = 0;
while(TR[pat[m1 - j]] == TR[*(text - j)]) {
if(++j == m) break; /* if statement can be saved,
but for safety ... */
}
if (j == m ) {
if(text > textend) return 0;
if(WORDBOUND) {
if(isalnum(*(text+1))) { shift=1 /*TG*/ ; goto CONT; } /* as if there was no match */
if(isalnum(*(text-m))) { shift=1 /*TG*/ ; goto CONT; } /* as if there was no match */
/* changed by Udi 11/7/94 to avoid having to set TR[] to W_delim */
}
if (TCOMPRESSED == ON) {
/* Don't update CurrentByteOffset here: only before outputting properly */
if (!DELIMITER) {
curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
if (*curtextbegin == '\n') curtextbegin ++;
curtextend = text+1; while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
if (*curtextend == '\n') curtextend ++;
/*** [TG] *THE CODE BELOW MUST BE COPIED HERE ***/
}
else {
curtextbegin = backward_delimiter(text, textbegin, tc_D_pattern, tc_D_length, OUTTAIL);
curtextend = forward_delimiter(text+1, textend, tc_D_pattern, tc_D_length, OUTTAIL);
}
}
else {
/* Don't update CurrentByteOffset here: only before outputting properly */
if (!DELIMITER) {
curtextbegin = text; while((curtextbegin > textbegin) && (*(--curtextbegin) != '\n'));
if (*curtextbegin == '\n') curtextbegin ++;
curtextend = text+1;
while((curtextend < textend) && (*curtextend != '\n')) curtextend ++;
if (*curtextend == '\n') curtextend ++;
/* adjust for files without CR as last character;
this part is only needed for hits at the very last line. [TG] */
if (curtextend >= textend) {
curtextend=textend+1;
if (*(curtextend-1) != '\n') *curtextend++='\n';
}
}
else {
curtextbegin = backward_delimiter(text, textbegin, D_pattern, D_length, OUTTAIL);
curtextend = forward_delimiter(text+1, textend, D_pattern, D_length, OUTTAIL);
}
}
if (TCOMPRESSED == ON) {
#if MEASURE_TIMES
gettimeofday(&initt, NULL);
#endif /*MEASURE_TIMES*/
if (-1 == exists_tcompressed_word(pat, m, curtextbegin, text - curtextbegin + m, EASYSEARCH)) {
shift = 1; /* TG */
goto CONT; /* as if there was no match */
}
#if MEASURE_TIMES
gettimeofday(&finalt, NULL);
FILTERALGO_ms += (finalt.tv_sec *1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
#endif /*MEASURE_TIMES*/
}
textbegin = curtextend; /* (curtextend - 1 > textbegin ? curtextend - 1 : curtextend); */
num_of_matched++;
if(FILENAMEONLY) return 0;
if(!COUNT) {
if (!INVERSE) {
if(FNAME && (NEW_FILE || !POST_FILTER)) {
char nextchar = (POST_FILTER == ON)?'\n':' ';
char *prevstring = (POST_FILTER == ON)?"\n":"";
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s%s:%c", prevstring, CurrentFileName, nextchar);
else {
int outindex;
if (prevstring[0] != '\0') {
if(agrep_outpointer + 1 >= agrep_outlen) {
OUTPUT_OVERFLOW;
return -1;
}
else agrep_outbuffer[agrep_outpointer ++] = prevstring[0];
}
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+2>=agrep_outlen)) {
OUTPUT_OVERFLOW;
return -1;
}
else {
agrep_outbuffer[agrep_outpointer+outindex++] = ':';
agrep_outbuffer[agrep_outpointer+outindex++] = nextchar;
}
agrep_outpointer += outindex;
}
NEW_FILE = OFF;
PRINTED = 1;
}
if(BYTECOUNT) {
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%d= ", CurrentByteOffset);
else {
char s[32];
int outindex;
sprintf(s, "%d=", CurrentByteOffset);
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(s[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
}
if (s[outindex] != '\0') {
OUTPUT_OVERFLOW;
return -1;
}
agrep_outpointer += outindex;
}
PRINTED = 1;
}
if (PRINTOFFSET) {
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "@%d{%d} ", CurrentByteOffset - (text -curtextbegin), curtextend-curtextbegin);
else {
char s[32];
int outindex;
sprintf(s, "@%d{%d} ", CurrentByteOffset - (text -curtextbegin), curtextend-curtextbegin);
for (outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(s[outindex] != '\0'); outindex ++) {
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
}
if (s[outindex] != '\0') {
OUTPUT_OVERFLOW;
return -1;
}
agrep_outpointer += outindex;
}
PRINTED = 1;
}
CurrentByteOffset += textbegin - text;
text = textbegin;
if (PRINTRECORD) {
if (TCOMPRESSED == ON) {
#if MEASURE_TIMES
gettimeofday(&initt, NULL);
#endif /*MEASURE_TIMES*/
if (agrep_finalfp != NULL)
newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_finalfp, -1, EASYSEARCH);
else {
if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, curtextbegin, curtextend-curtextbegin, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
if (agrep_outpointer + newlen + 1 >= agrep_outlen) {
OUTPUT_OVERFLOW;
return -1;
}
agrep_outpointer += newlen;
}
}
#if MEASURE_TIMES
gettimeofday(&finalt, NULL);
OUTFILTER_ms += (finalt.tv_sec*1000 + finalt.tv_usec/1000) - (initt.tv_sec*1000 + initt.tv_usec/1000);
#endif /*MEASURE_TIMES*/
}
else {
if (agrep_finalfp != NULL) {
fwrite(curtextbegin, 1, curtextend - curtextbegin, agrep_finalfp);
}
else {
if (agrep_outpointer + curtextend - curtextbegin >= agrep_outlen) {
OUTPUT_OVERFLOW;
return -1;
}
memcpy(agrep_outbuffer+agrep_outpointer, curtextbegin, curtextend-curtextbegin);
agrep_outpointer += curtextend - curtextbegin;
}
}
}
else if (PRINTED) {
if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);
else agrep_outbuffer[agrep_outpointer ++] = '\n';
PRINTED = 0;
}
}
else { /* INVERSE */
if (TCOMPRESSED == ON) { /* INVERSE: Don't care about filtering time */
if (agrep_finalfp != NULL)
newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_finalfp, -1, EASYSEARCH);
else {
if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, curtextbegin - lastout, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
if (newlen + agrep_outpointer >= agrep_outlen) {
OUTPUT_OVERFLOW;
return -1;
}
agrep_outpointer += newlen;
}
}
lastout=textbegin;
CurrentByteOffset += textbegin - text;
text = textbegin;
}
else { /* NOT TCOMPRESSED */
if (agrep_finalfp != NULL)
fwrite(lastout, 1, curtextbegin-lastout, agrep_finalfp);
else {
if (curtextbegin - lastout + agrep_outpointer >= agrep_outlen) {
OUTPUT_OVERFLOW;
return -1;
}
memcpy(agrep_outbuffer+agrep_outpointer, lastout, curtextbegin-lastout);
agrep_outpointer += (curtextbegin - lastout);
}
lastout=textbegin;
CurrentByteOffset += textbegin - text;
text = textbegin;
} /* TCOMPRESSED */
} /* INVERSE */
}
else { /* COUNT */
CurrentByteOffset += textbegin - text;
text = textbegin;
}
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) return 0; /* done */
/* CONT: was here TG */
/* shift = 1; [del] [TG] */
shift = SHIFT[*text]; /* [new] [TG] */
CONT: /* now here to restart the skip-loop */
#ifdef _WIN32
;
#endif
;
}
else shift = d1;
}
if (INVERSE && !COUNT && (lastout <= textend)) {
if (TCOMPRESSED == ON) { /* INVERSE: Don't care about filtering time */
if (agrep_finalfp != NULL)
newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_finalfp, -1, EASYSEARCH);
else {
if ((newlen = quick_tuncompress(FREQ_FILE, STRING_FILE, lastout, textend - lastout + 1, agrep_outbuffer, agrep_outlen - agrep_outpointer, EASYSEARCH)) > 0) {
if (newlen + agrep_outpointer >= agrep_outlen) {
OUTPUT_OVERFLOW;
return -1;
}
agrep_outpointer += newlen;
}
}
}