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. */
6
unsigned Init1, NO_ERR_MASK, Init[MaxError];
8
CHAR buffer[BlockSize+Maxline+1];
9
unsigned Next[MaxNext], Next1[MaxNext];
10
unsigned wildmask, endposition, D_endpos;
11
int REGEX, RE_ERR, FNAME, WHOLELINE, SIMPLEPATTERN;
12
int COUNT, HEAD, TAIL, LINENUM, INVERSE, I, S, DD, AND, SGREP, JUMP;
13
int Num_Pat, PSIZE, num_of_matched, SILENT, NOPROMPT, BESTMATCH, NOUPPER;
14
int NOMATCH, TRUNCATE, FIRST_IN_RE, FIRSTOUTPUT;
15
int WORDBOUND, DELIMITER, D_length;
16
int EATFIRST, OUTTAIL;
22
int total_line = 0; /* used in mgrep */
24
CHAR **Textfiles; /* array of filenames to be searched */
25
CHAR old_D_pat[MaxDelimit] = "\n"; /* to hold original D_pattern */
26
CHAR CurrentFileName[MAXNAME];
27
CHAR Progname[MAXNAME];
28
CHAR D_pattern[MaxDelimit] = "\n; "; /* string which delimits records --
29
defaults to newline */
30
int NOFILENAME = 0, /* Boolean flag, set for -h option */
31
FILENAMEONLY = 0,/* Boolean flag, set for -l option */
32
Numfiles = 0; /* indicates how many files in Textfiles */
34
int table[WORD][WORD];
40
JUMP = REGEX = FNAME = BESTMATCH = NOPROMPT = NOUPPER = 0;
41
COUNT = LINENUM = WHOLELINE = SGREP = 0;
42
EATFIRST = INVERSE = AND = TRUNCATE = OUTTAIL = 0;
43
FIRST_IN_RE = NOMATCH = FIRSTOUTPUT = ON;
47
SILENT = Num_Pat = PSIZE = SIMPLEPATTERN = num_of_matched = 0 ;
48
WORDBOUND = DELIMITER = RE_ERR = 0;
50
for (i = WORD - 1; i > 0 ; i--) Bit[i] = Bit[i+1] << 1;
51
for (i=0; i< MAXSYM; i++) Mask[i] = 0;
54
compute_next(M, Next, Next1)
55
int M; unsigned *Next, *Next1;
57
int i, j=0, n, k, temp;
63
temp = Bit[base]; Bit[base] = 0;
64
for (i=0; i<WORD; i++) V[i] = 0;
68
while (table[i][j] > 0 && j < 10) {
69
V[i] = V[i] | Bit[base + table[i][j++]];
82
if(n & Bit[WORD]) Next[i] = Next[i] | V[j];
88
if(M > MAXREG) fprintf(stderr, "%s: regular expression too long\n", Progname);
96
Next[i]= (Bit[base]>>1);
97
for(j=MM; j>mid ; j--)
99
if(n & Bit[WORD]) Next[i] = Next[i] | V[j-mid];
104
for(j = 0; j<mid; j++)
106
if(n & Bit[WORD]) Next1[i-k] = Next1[i-k] | V[MM-j];
117
for (i=0; i<m; i++) ex= ex*2;
124
register unsigned i, c, r0, r1, r2, r3, CMask, Newline, Init0, r_NO_ERR;
125
register unsigned end;
126
register unsigned hh, LL=0, k; /* Lower part */
127
int FIRST_TIME=ON, num_read , j=0, base;
128
unsigned A[MaxRerror+1], B[MaxRerror+1];
129
unsigned Next[MaxNext], Next1[MaxNext];
130
CHAR buffer[BlockSize+Maxline+1];
133
r_NO_ERR = NO_ERR_MASK;
135
fprintf(stderr, "%s: regular expression too long\n", Progname);
140
for(i=WORD, j=0; j < hh ; i--, j++) LL = LL | Bit[i];
141
if(FIRST_IN_RE) compute_next(M, Next, Next1);
142
/*SUN: try: change to memory allocation */
146
if(HEAD) Init[0] = Init[0] | Bit[base+1];
147
for(i=1; i<= D; i++) Init[i] = Init[i-1] | Next[Init[i-1]>>hh] | Next1[Init[i-1]&LL];
151
for(k=0; k<= D; k++) { A[k] = B[k] = Init[k]; }
154
while ((num_read = read(Text, buffer + Maxline, BlockSize)) > 0)
156
i=Maxline; end = num_read + Maxline;
157
if((num_read < BlockSize) && buffer[end-1] != '\n') buffer[end] = '\n';
158
if(FIRST_LOOP) { /* if first time in the loop add a newline */
159
buffer[i-1] = '\n'; /* in front the text. */
170
r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
177
r1 = Init1 & r3; /* match against endofline */
178
r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
179
if(TAIL) r2 = (Next[r2>>hh] | Next1[r2&LL]) | r2; /* epsilon move */
180
if(( r2 & 1 ) ^ INVERSE) {
183
printf("%s\n", CurrentFileName);
186
r_output(buffer, i-1, end, j);
189
r2 = (Next[r3>>hh] | Next1[r3&LL]) & CMask | Init0;
190
/* match begin of line */
197
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
199
else r3 = r2 & Init1;
200
} /* if(NOT Newline) */
202
r1 = Init1 & r2; /* match against endofline */
203
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
204
if(TAIL) r3 = ( Next[r3>>hh] | Next1[r3&LL] ) | r3;
206
if(( r3 & 1 ) ^ INVERSE) {
209
printf("%s\n", CurrentFileName);
212
r_output(buffer, i-1, end, j);
215
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | Init0;
216
/* match begin of line */
218
} /* while i < end ... */
219
strncpy(buffer, buffer+num_read, Maxline);
220
} /* end while read()... */
222
} /* end if (D == 0) */
223
while ((num_read = read(Text, buffer + Maxline, BlockSize)) > 0)
225
i=Maxline; end = Maxline + num_read;
226
if((num_read < BlockSize) && buffer[end-1] != '\n') buffer[end] = '\n';
227
if(FIRST_TIME) { /* if first time in the loop add a newline */
228
buffer[i-1] = '\n'; /* in front the text. */
241
A[0] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
244
r0 = r2 | A[0]; /* A[0] | B[0] */
245
A[1] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | (( r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
246
if(D == 1) goto Nextchar;
250
A[2] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
251
if(D == 2) goto Nextchar;
255
A[3] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
256
if(D == 3) goto Nextchar;
260
A[4] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
261
if(D == 4) goto Nextchar;
269
A[1] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
270
if(D == 1) goto Nextchar;
274
A[2] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
275
if(D == 2) goto Nextchar;
279
A[3] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
280
if(D == 3) goto Nextchar;
284
A[4] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
285
if(D == 4) goto Nextchar;
289
r1 = Init1 & B[D]; /* match against endofline */
290
A[D] = ((Next[B[D]>>hh] | Next1[B[D]&LL]) & CMask) | r1;
291
if(TAIL) A[D] = ( Next[A[D]>>hh] | Next1[A[D]&LL] ) | A[D];
293
if(( A[D] & 1 ) ^ INVERSE) {
296
printf("%s\n", CurrentFileName);
299
r_output(buffer, i, end, j);
301
for(k=0; k<=D; k++) B[k] = Init[0];
303
A[0] = (( Next[B[0]>>hh] | Next1[B[0]&LL]) & CMask) | r1;
304
for(k=1; k<=D; k++) {
307
r2 = A[k-1] | B[k-1];
308
A[k] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((B[k-1] | Next[r2>>hh] | Next1[r2&LL]) & r_NO_ERR) | r1;
319
B[0] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
323
B[1] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((r2 | Next[r0>>hh] | Next1[r0&LL]) & r_NO_ERR) | r1 ;
324
if(D == 1) goto Nextchar1;
328
B[2] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
329
if(D == 2) goto Nextchar1;
333
B[3] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
334
if(D == 3) goto Nextchar1;
338
B[4] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
339
if(D == 4) goto Nextchar1;
347
B[1] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
348
if(D == 1) goto Nextchar1;
352
B[2] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
353
if(D == 2) goto Nextchar1;
357
B[3] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
358
if(D == 3) goto Nextchar1;
362
B[4] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
363
if(D == 4) goto Nextchar1;
365
} /* if(NOT Newline) */
367
r1 = Init1 & A[D]; /* match against endofline */
368
B[D] = ((Next[A[D]>>hh] | Next1[A[D]&LL]) & CMask) | r1;
369
if(TAIL) B[D] = ( Next[B[D]>>hh] | Next1[B[D]&LL] ) | B[D];
371
if(( B[D] & 1 ) ^ INVERSE) {
374
printf("%s\n", CurrentFileName);
377
r_output(buffer, i, end, j);
379
for(k=0; k<=D; k++) A[k] = Init0;
381
B[0] = ((Next[A[0]>>hh] | Next1[A[0]&LL]) & CMask) | r1;
382
for(k=1; k<=D; k++) {
385
r2 = A[k-1] | B[k-1];
386
B[k] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((A[k-1] | Next[r2>>hh] | Next1[r2&LL]) & r_NO_ERR) | r1;
391
strncpy(buffer, buffer+num_read, Maxline);
399
register unsigned i, c, r1, r2, r3, CMask, k, Newline, Init0, Init1, end;
400
register unsigned r_even, r_odd, r_NO_ERR ;
401
unsigned RMask[MAXSYM];
402
unsigned A[MaxRerror+1], B[MaxRerror+1];
403
int num_read, j=0, bp, lasti, base, ResidueSize;
404
int FIRST_TIME; /* Flag */
408
compute_next(M, Next, Next1);
410
for(i=0; i< MAXSYM; i++) RMask[i] = Mask[i];
411
r_NO_ERR = NO_ERR_MASK;
414
Init0 = Init[0] = Bit[base];
415
if(HEAD) Init0 = Init[0] = Init0 | Bit[base+1] ;
416
for(i=1; i<= D; i++) Init[i] = Init[i-1] | Next[Init[i-1]]; /* can be out? */
419
for(k=0; k<= D; k++) { A[k] = B[k] = Init[0]; } /* can be out? */
423
while ((num_read = read(Text, buffer + Maxline, BlockSize)) > 0)
425
i=Maxline; end = Maxline + num_read ;
426
if((num_read < BlockSize)&&buffer[end-1] != '\n') buffer[end] = '\n';
439
r2 = (Next[r3] & CMask) | r1;
442
r1 = Init1 & r3; /* match against '\n' */
443
r2 = Next[r3] & CMask | r1;
445
if(TAIL) r2 = Next[r2] | r2 ; /* epsilon move */
446
if(( r2 & 1) ^ INVERSE) {
449
printf("%s\n", CurrentFileName);
452
r_output(buffer, i-1, end, j);
456
r2 = (Next[r3] & CMask) | Init0;
463
r3 = (Next[r2] & CMask) | r1;
466
r1 = Init1 & r2; /* match against endofline */
467
r3 = Next[r2] & CMask | r1;
468
if(TAIL) r3 = Next[r3] | r3;
469
if(( r3 & 1) ^ INVERSE) {
472
printf("%s\n", CurrentFileName);
475
r_output(buffer, i-1, end, j);
479
r3 = (Next[r2] & CMask) | Init0; /* match the newline */
482
ResidueSize = Maxline + num_read - lasti;
483
if(ResidueSize > Maxline) {
484
ResidueSize = Maxline; }
485
strncpy(buffer+Maxline-ResidueSize, buffer+lasti, ResidueSize);
486
lasti = Maxline - ResidueSize;
490
while ((num_read = read(Text, buffer + Maxline, BlockSize)) > 0)
492
i=Maxline; end = Maxline+num_read;
493
if((num_read < BlockSize) && buffer[end-1] != '\n') buffer[end] = '\n';
506
A[0] = (Next[r_even] & CMask) | r1;
509
r2 = (r_even | Next[r_even|A[0]]) &r_NO_ERR;
510
A[1] = (Next[r_odd] & CMask) | r2 | r1 ;
511
if(D == 1) goto Nextchar;
514
r2 = (r_odd | Next[r_odd|A[1]]) &r_NO_ERR;
515
A[2] = (Next[r_even] & CMask) | r2 | r1 ;
516
if(D == 2) goto Nextchar;
519
r2 = (r_even | Next[r_even|A[2]]) &r_NO_ERR;
520
A[3] = (Next[r_odd] & CMask) | r2 | r1 ;
521
if(D == 3) goto Nextchar;
524
r2 = (r_odd | Next[r_odd|A[3]]) &r_NO_ERR;
525
A[4] = (Next[r_even] & CMask) | r2 | r1 ;
527
} /* if NOT Newline */
529
r1 = Init1 & B[D]; /* match endofline */
530
A[D] = (Next[B[D]] & CMask) | r1;
531
if(TAIL) A[D] = Next[A[D]] | A[D];
532
if((A[D] & 1) ^ INVERSE ) {
535
printf("%s\n", CurrentFileName);
538
r_output(buffer, i-1, end, j);
540
for(k=0; k<= D; k++) { A[k] = B[k] = Init[k]; }
542
A[0] = (Next[B[0]] & CMask) | r1;
543
for(k=1; k<= D; k++) {
545
r2 = (B[k-1] | Next[A[k-1]|B[k-1]]) &r_NO_ERR;
546
A[k] = (Next[B[k]] & CMask) | r1 | r2;
555
B[0] = (Next[A[0]] & CMask) | r1;
557
B[1] = (Next[A[1]] & CMask) | ((A[0] | Next[A[0] | B[0]]) & r_NO_ERR) | r1 ;
558
if(D == 1) goto Nextchar1;
560
B[2] = (Next[A[2]] & CMask) | ((A[1] | Next[A[1] | B[1]]) &r_NO_ERR) | r1 ;
561
if(D == 2) goto Nextchar1;
563
B[3] = (Next[A[3]] & CMask) | ((A[2] | Next[A[2] | B[2]])&r_NO_ERR) | r1 ;
564
if(D == 3) goto Nextchar1;
566
B[4] = (Next[A[4]] & CMask) | ((A[3] | Next[A[3] | B[3]])&r_NO_ERR) | r1 ;
568
} /* if(NOT Newline) */
570
r1 = Init1 & A[D]; /* match endofline */
571
B[D] = (Next[A[D]] & CMask) | r1;
572
if(TAIL) B[D] = Next[B[D]] | B[D];
573
if((B[D] & 1) ^ INVERSE ) {
576
printf("%s\n", CurrentFileName);
579
r_output(buffer, i, end, j);
581
for(k=0; k<= D; k++) { A[k] = B[k] = Init[k]; }
583
B[0] = (Next[A[0]] & CMask) | r1;
584
for(k=1; k<= D; k++) {
586
r2 = (A[k-1] | Next[A[k-1]|B[k-1]])&r_NO_ERR;
587
B[k] = (Next[A[k]] & CMask) | r1 | r2;
591
} /* while i < end */
592
strncpy(buffer, buffer+num_read, Maxline);
598
r_output (buffer, i, end, j)
606
if(FNAME) printf("%s: ", CurrentFileName);
608
while ((buffer[bp] != '\n') && (bp > 0)) bp--;
609
if(LINENUM) printf("%d: ", j);
610
if(buffer[bp] != '\n') bp = Maxline-1;
612
while (bp <= i ) putchar(buffer[bp++]);
5
#include <locale.h> /* support for 8bit character set: ew@senate.be */
9
extern int INFILTER_ms, OUTFILTER_ms, FILTERALGO_ms;
10
#endif /*MEASURE_TIMES*/
12
extern char Pattern[MAXPAT];
13
extern int EXITONERROR;
15
#include "dummysyscalls.c"
616
int argc; char *argv[];
618
int N, M, D=0, fp, fd, i, j;
621
unsigned char Pattern[MAXPAT], OldPattern[MAXPAT], temp[MAXPAT];
624
strcpy(Progname, argv[0]);
625
if (argc < 2) usage();
627
while(--argc > 0 && (*++argv)[0] == '-') {
630
case 'c' : COUNT = ON; /* output the # of matched */
632
case 's' : SILENT = ON; /* silent mode */
634
case 'p' : I = 0; /* insertion cost is 0 */
636
case 'x' : WHOLELINE = ON; /* match the whole line */
638
fprintf(stderr, "%s: illegal option combination\n", Progname);
643
case 'd' : DELIMITER = ON; /* user defines delimiter */
644
if(argc <= 1) usage();
645
if (argv[0][2] == '\0') {/* space after -d option */
647
if ((D_length = strlen(argv[0])) > MaxDelimit) {
648
fprintf(stderr, "%s: delimiter pattern too long\n", Progname);
652
strcpy(D_pattern+1, argv[0]);
655
if ((D_length = strlen(argv[0]+2)) > MaxDelimit) {
656
fprintf(stderr, "%s: delimiter pattern too long\n", Progname);
660
strcpy(D_pattern+1, argv[0]+2);
662
strcat(D_pattern, ">; ");
663
D_length++; /* to count ';' as one */
667
fprintf(stderr, "%s: the pattern should immediately follow the -e option\n", Progname);
670
if((++argv)[0][0] == '-') {
672
strcat(Pattern, (argv)[0]);
674
else strcat(Pattern, argv[0]);
676
case 'f' : PAT_FILE = ON;
679
if((fp = open(argv[0], 0)) < 0) {
680
fprintf(stderr, "%s: Can't open pattern file %s\n", Progname, argv[0]);
684
case 'h' : NOFILENAME = ON;
686
case 'i' : NOUPPER = ON;
690
fprintf(stderr, "%s: the pattern should immediately follow the -k option\n", Progname);
695
strcat(Pattern, argv[0]);
696
if(argc > 1 && argv[1][0] == '-') {
697
fprintf(stderr, "%s: -k should be the last option in the command\n", Progname);
701
case 'l' : FILENAMEONLY = ON;
703
case 'n' : LINENUM = ON; /* output prefixed by line no*/
705
case 'v' : INVERSE = ON; /* output no-matched lines */
707
case 't' : OUTTAIL = ON; /* output from tail of delimiter */
709
case 'B' : BESTMATCH = ON;
711
case 'w' : WORDBOUND = ON;/* match to words */
713
fprintf(stderr, "%s: illegal option combination\n", Progname);
717
case 'y' : NOPROMPT = ON;
719
case 'I' : I = atoi(argv[0]+2); /* Insertion Cost */
722
case 'S' : S = atoi(argv[0]+2); /* Substitution Cost */
725
case 'D' : DD = atoi(argv[0]+2); /* Deletion Cost */
728
case 'G' : FILEOUT = ON;
731
default : if (isdigit(c)) {
735
fprintf(stderr,"%s: the maximum number of errors is %d \n", Progname, MaxError);
739
fprintf(stderr, "%s: illegal option -%c\n",Progname, c);
743
} /* while (--argc > 0 && (*++argv)[0] == '-') */
745
if (FILENAMEONLY && NOFILENAME) {
746
fprintf(stderr, "%s: -h and -l options are mutually exclusive\n",Progname);
748
if (COUNT && (FILENAMEONLY || NOFILENAME)) {
750
if(!FILEOUT) NOFILENAME = OFF;
752
if (!(PAT_FILE) && Pattern[0] == '\0') { /* Pattern not set with -e option */
753
if (argc == 0) usage();
754
strcpy(Pattern, *argv);
759
fd = 3; /* make sure it's not 0 */
760
if (argc == 0) /* check Pattern against stdin */
763
if (!(Textfiles = (CHAR **)malloc(argc * sizeof(CHAR *) ))) {
764
fprintf(stderr, "%s: malloc failure (you probably don't have enough memory)\n", Progname);
767
while (argc--) { /* one or more filenames on command line -- put
768
the valid filenames in a array of strings */
770
if ((filetype = check_file(*argv)) != ISASCIIFILE) {
771
if(filetype == NOSUCHFILE) fprintf(stderr,"%s: %s: no such file or directory\n",Progname,*argv);
775
if ((filetype = check_file(*argv)) == NOSUCHFILE) {
776
if(filetype == NOSUCHFILE) fprintf(stderr,"%s: %s: no such file or directory\n",Progname,*argv);
778
} else { /* file is ascii*/
779
if (!(Textfiles[Numfiles] = (CHAR *)malloc((strlen(*argv)+1)))) {
780
fprintf(stderr, "%s: malloc failure (you probably don't have enough memory)\n", Progname);
783
strcpy(Textfiles[Numfiles++], *argv++);
785
} /* while (argc--) */
787
checksg(Pattern, D); /* check if the pattern is simple */
788
strcpy(OldPattern, Pattern);
790
preprocess(D_pattern, Pattern);
791
strcpy(old_D_pat, D_pattern);
792
M = maskgen(Pattern, D);
794
else M = strlen(OldPattern);
795
if (PAT_FILE) prepf(fp);
796
if (Numfiles > 1) FNAME = ON;
797
if (NOFILENAME) FNAME = 0;
799
compat(); /* check compatibility between options */
802
fprintf(stderr, "%s: -l option is not compatible with standard input\n", Progname);
805
if(PAT_FILE) mgrep(fd);
807
if(SGREP) sgrep(OldPattern, strlen(OldPattern), fd, D);
808
else bitap(old_D_pat, Pattern, fd, M, D);
811
if(INVERSE && PAT_FILE) printf("%d\n", total_line-num_of_matched);
812
else printf("%d\n", num_of_matched);
816
for (i = 0; i < Numfiles; i++, close(fd), num_of_matched = 0) {
817
strcpy(CurrentFileName, Textfiles[i]);
818
if ((fd = open(Textfiles[i], 0)) <= 0) {
819
fprintf(stderr, "%s: can't open file %s\n",Progname, Textfiles[i]);
822
if(PAT_FILE) mgrep(fd);
824
if(SGREP) sgrep(OldPattern, strlen(OldPattern), fd, D);
825
else bitap(old_D_pat, Pattern, fd, M, D);
827
if (num_of_matched) NOMATCH = OFF;
828
if (COUNT && !FILEOUT) {
829
if(INVERSE && PAT_FILE) {
830
if(FNAME) printf("%s: %d\n", CurrentFileName, total_line - num_of_matched);
831
else printf("%d\n", total_line - num_of_matched);
834
if(FNAME) printf("%s: %d\n", CurrentFileName, num_of_matched);
835
else printf("%d\n", num_of_matched);
838
if(FILEOUT && num_of_matched) {
839
file_out(CurrentFileName);
842
} /* for i < Numfiles */
843
if(NOMATCH && BESTMATCH) {
844
if(WORDBOUND || WHOLELINE || LINENUM || INVERSE) {
846
preprocess(D_pattern, Pattern);
847
strcpy(old_D_pat, D_pattern);
848
M = maskgen(Pattern, D);
851
while(D<M && D<=MaxError && num_of_matched == 0) {
852
for (i = 0; i < Numfiles; i++, close(fd)) {
853
strcpy(CurrentFileName, Textfiles[i]);
854
if ((fd = open(Textfiles[i], 0)) > 0) {
855
if(PAT_FILE) mgrep(fd);
857
if(SGREP) sgrep(OldPattern,strlen(OldPattern),fd,D);
858
else bitap(old_D_pat,Pattern,fd,M,D);
861
} /* for i < Numfiles */
864
if(num_of_matched > 0) {
866
if(NOPROMPT) goto GO_AHEAD;
867
if(D==1) fprintf(stderr, "best match has 1 error, ");
868
else fprintf(stderr, "best match has %d errors, ", D);
870
if(num_of_matched == 1) fprintf(stderr,"there is 1 match, output it? (y/n)");
871
else fprintf(stderr,"there are %d matches, output them? (y/n)", num_of_matched);
873
if(c != 'y') goto CONT;
875
for (i = 0; i < Numfiles; i++, close(fd)) {
876
strcpy(CurrentFileName, Textfiles[i]);
877
if ((fd = open(Textfiles[i], 0)) > 0) {
878
if(PAT_FILE) mgrep(fd);
880
if(SGREP) sgrep(OldPattern,strlen(OldPattern),fd,D);
881
else bitap(old_D_pat,Pattern,fd,M,D);
884
} /* for i < Numfiles */
894
if(num_of_matched) NOMATCH = OFF;
897
} /* end of main() */
910
for(i=0; i< len; i++) putchar(':');
912
printf("%s\n", CurrentFileName);
914
for(i=0; i< len; i++) putchar(':');
919
while((num_read = read(fd, buf, 4096)) > 0)
920
write(1, buf, num_read);
926
fprintf(stderr, "usage: %s [-#cdehiklnpstvwxBDGIS] [-f patternfile] pattern [files]\n", Progname);
928
fprintf(stderr, "summary of frequently used options:\n");
929
fprintf(stderr, "-#: find matches with at most # errors\n");
930
fprintf(stderr, "-c: output the number of matched records\n");
931
fprintf(stderr, "-d: define record delimiter\n");
932
fprintf(stderr, "-h: do not output file names\n");
933
fprintf(stderr, "-i: case-insensitive search, e.g., 'a' = 'A'\n");
934
fprintf(stderr, "-l: output the names of files that contain a match\n");
935
fprintf(stderr, "-n: output record prefixed by record number\n");
936
fprintf(stderr, "-v: output those records containing no matches\n");
937
fprintf(stderr, "-w: pattern has to match as a word, e.g., 'win' will not match 'wind'\n");
938
fprintf(stderr, "-B: best match mode. find the closest matches to the pattern\n");
939
fprintf(stderr, "-G: output the files that contain a match\n");
946
CHAR *Pattern; int D;
951
if(!(PAT_FILE) && m <= D) {
952
fprintf(stderr, "%s: size of pattern must be greater than number of errors\n", Progname);
956
for (i=0; i < m; i++)
960
case ';' : SIMPLEPATTERN = OFF; break;
961
case ',' : SIMPLEPATTERN = OFF; break;
962
case '.' : SIMPLEPATTERN = OFF; break;
963
case '*' : SIMPLEPATTERN = OFF; break;
964
case '-' : SIMPLEPATTERN = OFF; break;
965
case '[' : SIMPLEPATTERN = OFF; break;
966
case ']' : SIMPLEPATTERN = OFF; break;
967
case '(' : SIMPLEPATTERN = OFF; break;
968
case ')' : SIMPLEPATTERN = OFF; break;
969
case '<' : SIMPLEPATTERN = OFF; break;
970
case '>' : SIMPLEPATTERN = OFF; break;
971
case '^' : if(D > 0) SIMPLEPATTERN = OFF;
973
case '$' : if(D > 0) SIMPLEPATTERN = OFF;
975
case '|' : SIMPLEPATTERN = OFF; break;
976
case '#' : SIMPLEPATTERN = OFF; break;
977
case '\\' : SIMPLEPATTERN = OFF; break;
981
if (CONSTANT) SIMPLEPATTERN = ON;
982
if (SIMPLEPATTERN == OFF) return;
983
if (NOUPPER && D) return;
984
if (JUMP == ON) return;
987
if (DELIMITER) return;
989
if (WORDBOUND && D > 0) return;
990
if (WHOLELINE && D > 0) return;
991
if (SILENT) return; /* REMINDER: to be removed */
993
if(m >= 16) DNA = ON;
996
if(c == 'a' || c == 'c' || c == 't' || c == 'g' ) ;
1002
output (buffer, i1, i2, j)
1003
register CHAR *buffer; int i1, i2, j;
1005
register CHAR *bp, *outend;
1014
if(DELIMITER) j = j+1;
1016
if (buffer[i1] == '\n') {
1023
fprintf(stderr, "WARNING!!! some lines have been truncated in output record #%d\n", num_of_matched-1);
1025
while(buffer[i1] == '\n' && i1 <= i2) {
1029
if(FNAME == ON) printf("%s: ", CurrentFileName);
1030
if(LINENUM) printf("%d: ", j-1);
1032
outend = buffer + i2;
1033
while(bp <= outend) putchar(*bp++);
25
setlocale(LC_ALL,""); /* support for 8bit character set: ew@senate.be, Henrik.Martin@eua.ericsson.se, "O.Bartunov" <megera@sai.msu.su>, S.Nazin (leng@sai.msu.su) */
28
EXITONERROR = 1; /* the only place where it is set to 1 */
29
ret = fileagrep(argc, argv, 0, stdout);
32
fprintf(stderr, "ret = %d infilter = %d ms\toutfilter = %d ms\tfilteralgo = %d ms\n", ret, INFILTER_ms, OUTFILTER_ms, FILTERALGO_ms);
33
#endif /*MEASURE_TIMES*/