3
* bgopal: (1993-4) added a library interface and removed some bugs: also
4
* selectively modified many routines to work with our text-compression algo.
13
extern char **environ;
15
int pattern_index; /* index in argv where the pattern is */
17
int glimpse_isserver=0; /* so that there is no user interaction */
18
int glimpse_call = 0; /* So that usage message is not printed twice */
19
int glimpse_clientdied=0;/* to quit search if glimpseserver's client dies */
20
int agrep_initialfd; /* Where does input come from? File/Memory? */
25
FILE *agrep_finalfp; /* Where does output go to? File/Memory? */
26
CHAR *agrep_outbuffer;
30
int execfd; /* used by exec called within agrep_search, set in agrep_init */
31
int multifd = -1; /* fd for multipattern search used in ^^ , set in ^^^^^^^^ */
32
extern char *pat_spool;
34
extern char *tc_pat_spool;
35
#endif /* DOTCOMPRESSED */
36
char *multibuf=NULL; /* buffer to put the multiple patterns in */
37
int multilen = 0; /* length of the multibuf: not the #of multi-patterns! */
39
extern int pos_cnt; /* to re-initialize it to 0 for reg-exp search */
40
unsigned Mask[MAXSYM];
41
unsigned Init1, NO_ERR_MASK, Init[MaxError];
43
CHAR buffer[BlockSize+Maxline+1]; /* should not be used anywhere: 10/18/93 */
44
unsigned Next[MaxNext], Next1[MaxNext];
45
unsigned wildmask, endposition, D_endpos;
46
int LIMITOUTPUT; /* maximum number of matches we are going to allow */
47
int LIMITPERFILE; /* maximum number of matches per file we are going to allow */
48
int LIMITTOTALFILE; /* maximum number of files we are going to allow */
49
int EXITONERROR; /* return -1 or exit on error? */
50
int REGEX, FASTREGEX, RE_ERR, FNAME, WHOLELINE, SIMPLEPATTERN;
51
int COUNT, HEAD, TAIL, LINENUM, INVERSE, I, S, DD, AND, SGREP, JUMP;
53
int Num_Pat, PSIZE, prev_num_of_matched, num_of_matched, files_matched, SILENT, NOPROMPT, BESTMATCH, NOUPPER;
54
int NOMATCH, TRUNCATE, FIRST_IN_RE, FIRSTOUTPUT;
55
int WORDBOUND, DELIMITER, D_length, tc_D_length, original_D_length;
56
int EATFIRST, OUTTAIL;
60
int PRINTNONEXISTENTFILE;
64
int PAT_FILE; /* multiple patterns from a given file */
65
char PAT_FILE_NAME[MAX_LINE_LEN];
66
int PAT_BUFFER; /* multiple patterns from a given buffer */
69
int total_line; /* used in mgrep */
73
int EASYSEARCH; /* 1 used only for compressed files: LITTLE/BIG */
74
int ALWAYSFILENAME = OFF;
75
int POST_FILTER = OFF;
76
int NEW_FILE = OFF; /* only when post-filter is used */
77
int PRINTFILENUMBER = OFF;
78
int PRINTFILETIME = OFF;
79
int PRINTPATTERN = OFF;
80
int MULTI_OUTPUT = OFF; /* should mgrep print the matched line multiple times for each matched pattern or just once? */
81
/* invisible to the user, used only by glimpse: cannot use -l since it is incompatible with stdin and -A is used for the index search (done next) */
83
/* Stuff to handle complicated boolean patterns */
84
int AComplexBoolean = 0;
85
ParseTree *AParse = NULL;
86
int anum_terminals = 0;
87
ParseTree aterminals[MAXNUM_PAT];
88
char amatched_terminals[MAXNUM_PAT];
89
char aduplicates[MAXNUM_PAT][MAXNUM_PAT]; /* tells what other patterns are exactly equal to the i-th one */
90
char tc_aduplicates[MAXNUM_PAT][MAXNUM_PAT]; /* tells what other patterns are exactly equal to the i-th one */
93
/* timing variables */
97
#endif /*MEASURE_TIMES*/
99
CHAR **Textfiles = NULL; /* array of filenames to be searched */
100
int Numfiles = 0; /* indicates how many files in Textfiles */
101
int copied_from_argv = 0; /* were filenames copied from argv (should I free 'em)? */
102
CHAR old_D_pat[MaxDelimit * 2] = "\n"; /* to hold original D_pattern */
103
CHAR original_old_D_pat[MaxDelimit * 2] = "\n";
104
CHAR Pattern[MAXPAT], OldPattern[MAXPAT];
105
CHAR CurrentFileName[MAX_LINE_LEN];
106
long CurrentFileTime;
107
int SetCurrentFileName = 0; /* dirty glimpse trick to make filters work: output seems to come from another file */
108
int SetCurrentFileTime = 0; /* dirty glimpse trick to avoid doing a stat to find the time */
109
int CurrentByteOffset;
110
int SetCurrentByteOffset = 0;
111
CHAR Progname[MAXNAME];
112
CHAR D_pattern[MaxDelimit * 2] = "\n; "; /* string which delimits records -- defaults to newline */
113
CHAR tc_D_pattern[MaxDelimit * 2] = "\n";
114
CHAR original_D_pattern[MaxDelimit * 2] = "\n; ";
115
char COMP_DIR[MAX_LINE_LEN];
116
char FREQ_FILE[MAX_LINE_LEN], HASH_FILE[MAX_LINE_LEN], STRING_FILE[MAX_LINE_LEN]; /* interfacing with tcompress */
118
int NOFILENAME, /* Boolean flag, set for -h option */
119
FILENAMEONLY;/* Boolean flag, set for -l option */
121
int table[WORD][WORD];
122
CHAR *agrep_saved_pattern = NULL; /* to prevent multiple prepfs for each boolean search: crd@hplb.hpl.hp.com */
125
aget_file_time(stbuf, name)
131
if (stbuf != NULL) ret = stbuf->st_mtime;
133
if (my_stat(name, &mystbuf) == -1) ret = 0;
134
else ret = mystbuf.st_mtime;
140
aprint_file_time(thetime)
144
char s[256], s1[16], s2[16], s3[16], s4[16], s5[16];
145
static char buffer[256];
147
strcpy(s, ctime(&thetime)); /* of the form: Sun Sep 16 01:03:52 1973\n\0 */
148
s[strlen(s) - 1] = '\0';
149
sscanf(s, "%s%s%s%s%s", s1, s2, s3, s4, s5);
150
sprintf(buffer, ": %s %s %s", s2, s3, s5); /* ditch Sun 01:03:52 */
152
static char buffer[256];
155
strftime(&buffer[2], 256, "%h %e %Y", gmtime(&thetime));
160
/* Called when multipattern search and pattern has not changed */
162
reinit_value_partial()
164
num_of_matched = prev_num_of_matched = 0;
169
/* This must be called before every agrep_search to reset agrep globals */
175
/* Added on 7th Oct 194 */
177
if (AComplexBoolean) destroy_tree(AParse);
181
if (multibuf != NULL) free(multibuf); /* this was allocated for arbit booleans, not multipattern search */
184
/* Cannot free multifd here since that is always allocated for multipattern search */
186
for (i=0; i<anum_terminals; i++) {
187
free(aterminals[i].data.leaf.value);
188
memset(&aterminals[i], '\0', sizeof(ParseTree));
191
for (i=0; i<MAXNUM_PAT; i++) memset(aduplicates[i], '\0', MAXNUM_PAT);
192
for (i=0; i<MAXNUM_PAT; i++) memset(tc_aduplicates[i], '\0', MAXNUM_PAT);
195
for (i = WORD - 1; i > 0 ; i--) Bit[i] = Bit[i+1] << 1;
196
for (i=0; i< MAXSYM; i++) Mask[i] = 0;
198
/* bg: new things added on Mar 13 94 */
201
memset(Init, '\0', MaxError * sizeof(unsigned));
202
memset(Next, '\0', MaxNext * sizeof(unsigned));
203
memset(Next1, '\0', MaxNext * sizeof(unsigned));
204
wildmask = endposition = D_endpos = 0;
205
for (i=0; i<WORD; i++)
206
for (j=0; j<WORD; j++)
209
strcpy(D_pattern, original_D_pattern);
210
D_length = original_D_length;
211
strcpy(old_D_pat, original_old_D_pat);
213
/* Changed on Dec 26th: bg */
214
FASTREGEX = REGEX = 0;
215
HEAD = TAIL = ON; /* were off initially */
219
pos_cnt = 0; /* added 31 Jan 95 */
221
reinit_value_partial();
224
/* This must be called before every agrep_init to reset agrep options */
228
SetCurrentFileName = 0; /* 16/9/94 */
229
SetCurrentFileTime = 0;
230
SetCurrentByteOffset = 0; /* 23/9/94 */
232
/* courtesy: crd@hplb.hpl.hp.com */
233
if (agrep_saved_pattern) {
234
free(agrep_saved_pattern);
235
agrep_saved_pattern= NULL;
237
/* bg: new stuff on 17/Feb/94 */
238
if (multifd != -1) close(multifd);
240
if (multibuf != NULL) free(multibuf);
243
if (pat_spool != NULL) free(pat_spool);
246
if (tc_pat_spool != NULL) free(tc_pat_spool);
248
#endif /* DOTCOMPRESSED */
249
LIMITOUTPUT = 0; /* means infinity = current semantics */
250
LIMITPERFILE = 0; /* means infinity = current semantics */
251
LIMITTOTALFILE = 0; /* means infinity = current semantics */
253
DNA = APPROX = PAT_FILE = PAT_BUFFER = CONSTANT = total_line = D = TCOMPRESSED = 0;
254
PAT_FILE_NAME[0] = '\0';
255
EXITONERROR = NOFILENAME = FILENAMEONLY = FILEOUT = ALWAYSFILENAME = NEW_FILE = POST_FILTER = 0;
257
original_old_D_pat[0] = old_D_pat[0] = '\n';
258
original_old_D_pat[1] = old_D_pat[1] = '\0';
259
original_D_pattern[0] = D_pattern[0] = '\n';
260
original_D_pattern[1] = D_pattern[1] = ';';
261
original_D_pattern[2] = D_pattern[2] = ' ';
262
original_D_pattern[3] = D_pattern[3] = '\0';
264
strcpy(tc_D_pattern, "\n");
267
/* the functions agrep_init and agrep_search take care of Textfiles and Numfiles */
269
agrep_outpointer = 0;
272
OUTFILTER_ms = FILTERALGO_ms = INFILTER_ms = 0;
273
#endif /*MEASURE_TIMES*/
279
JUMP = FNAME = BESTMATCH = NOPROMPT = NOUPPER = 0;
281
COUNT = LINENUM = WHOLELINE = SGREP = 0;
283
EATFIRST = INVERSE = TRUNCATE = OUTTAIL = 0;
284
NOMATCH = FIRSTOUTPUT = ON; /* were off initally */
285
I = DD = S = 1; /* were off initially */
286
original_D_length = D_length = 2; /* was 0 initially */
287
SILENT = Num_Pat = PSIZE = SIMPLEPATTERN = prev_num_of_matched = num_of_matched = files_matched = 0;
288
WORDBOUND = DELIMITER = 0;
293
STRING_FILE[0] = '\0';
297
PRINTNONEXISTENTFILE = OFF;
299
glimpse_clientdied = 0; /* added 15th Feb 95 */
301
/* Pattern, OldPattern, execfd, Numfiles are set in agrep_init: so no need to initialize */
306
compute_next(M, Next, Next1)
308
unsigned *Next, *Next1;
310
int i, j=0, n, k, temp;
318
for (i=0; i<WORD; i++) V[i] = 0;
322
while (table[i][j] > 0 && j < 10) {
323
V[i] = V[i] | Bit[base + table[i][j++]];
337
if(n & Bit[WORD]) Next[i] = Next[i] | V[j];
343
if(M > MAXREG) fprintf(stderr, "%s: regular expression too long\n", Progname);
352
Next[i]= (Bit[base]>>1);
353
for(j=MM; j>mid ; j--)
355
if(n & Bit[WORD]) Next[i] = Next[i] | V[j-mid];
360
for(j = 0; j<mid; j++)
362
if(n & Bit[WORD]) Next1[i-k] = Next1[i-k] | V[MM-j];
375
for (i=0; i<m; i++) ex <<= 1; /* was ex *= 2 */
383
register unsigned i, c, r0, r1, r2, r3, CMask, Newline, Init0, r_NO_ERR;
384
register unsigned end;
385
register unsigned hh, LL=0, k; /* Lower part */
386
int FIRST_TIME=ON, num_read , j=0, base;
387
unsigned A[MaxRerror+1], B[MaxRerror+1];
388
unsigned Next[MaxNext], Next1[MaxNext];
392
r_NO_ERR = NO_ERR_MASK;
394
fprintf(stderr, "%s: regular expression too long\n", Progname);
403
for(i=WORD, j=0; j < hh ; i--, j++) LL = LL | Bit[i];
404
if(FIRST_IN_RE) compute_next(M, Next, Next1);
405
/*SUN: try: change to memory allocation */
409
if(HEAD) Init[0] = Init[0] | Bit[base+1];
410
for(i=1; i<= D; i++) Init[i] = Init[i-1] | Next[Init[i-1]>>hh] | Next1[Init[i-1]&LL];
414
for(k=0; k<= D; k++) {
415
A[k] = B[k] = Init[k];
423
#endif /*AGREP_POINTER*/
424
alloc_buf(Text, &buffer, BlockSize+Maxline+1);
425
while ((num_read = fill_buf(Text, buffer + Maxline, BlockSize)) > 0)
428
end = num_read + Maxline;
430
/* pab: Don't do this here; it's done in bitap.fill_buf,
431
* where we can handle eof on a block boundary right */
432
if((num_read < BlockSize) && buffer[end-1] != '\n') buffer[end++] = '\n';
434
if(FIRST_LOOP) { /* if first time in the loop add a newline */
435
buffer[i-1] = '\n'; /* in front the text. */
437
CurrentByteOffset --;
441
/* RE1_PROCESS_WHEN_DZERO: the while-loop below */
445
CurrentByteOffset ++;
451
r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
459
if (DELIMITER) CurrentByteOffset -= D_length;
460
else CurrentByteOffset -= 1;
461
r1 = Init1 & r3; /* match against endofline */
462
r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
463
if(TAIL) r2 = (Next[r2>>hh] | Next1[r2&LL]) | r2; /* epsilon move */
464
if(( r2 & 1 ) ^ INVERSE) {
465
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
468
if (agrep_finalfp != NULL)
469
fprintf(agrep_finalfp, "%s", CurrentFileName);
472
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
473
(CurrentFileName[outindex] != '\0'); outindex++) {
474
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
476
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
478
free_buf(Text, buffer);
481
agrep_outpointer += outindex;
484
char *s = aprint_file_time(CurrentFileTime);
485
if (agrep_finalfp != NULL)
486
fprintf(agrep_finalfp, "%s", s);
489
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
490
(s[outindex] != '\0'); outindex++) {
491
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
493
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
495
free_buf(Text, buffer);
498
agrep_outpointer += outindex;
501
if (agrep_finalfp != NULL)
502
fprintf(agrep_finalfp, "\n");
504
if (agrep_outpointer+1>=agrep_outlen) {
506
free_buf(Text, buffer);
509
else agrep_outbuffer[agrep_outpointer++] = '\n';
512
free_buf(Text, buffer);
516
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
517
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
518
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
519
free_buf(Text, buffer);
524
r2 = (Next[r3>>hh] | Next1[r3&LL]) & CMask | Init0;
525
/* match begin of line */
526
if (DELIMITER) CurrentByteOffset += 1*D_length;
527
else CurrentByteOffset += 1*1;
530
CurrentByteOffset ++;
536
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
538
else r3 = r2 & Init1;
539
} /* if(NOT Newline) */
542
if (DELIMITER) CurrentByteOffset -= D_length;
543
else CurrentByteOffset -= 1;
544
r1 = Init1 & r2; /* match against endofline */
545
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
546
if(TAIL) r3 = ( Next[r3>>hh] | Next1[r3&LL] ) | r3;
548
if(( r3 & 1 ) ^ INVERSE) {
549
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
552
if (agrep_finalfp != NULL)
553
fprintf(agrep_finalfp, "%s", CurrentFileName);
556
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
557
(CurrentFileName[outindex] != '\0'); outindex++) {
558
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
560
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
562
free_buf(Text, buffer);
565
agrep_outpointer += outindex;
568
char *s = aprint_file_time(CurrentFileTime);
569
if (agrep_finalfp != NULL)
570
fprintf(agrep_finalfp, "%s", s);
573
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
574
(s[outindex] != '\0'); outindex++) {
575
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
577
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
579
free_buf(Text, buffer);
582
agrep_outpointer += outindex;
585
if (agrep_finalfp != NULL)
586
fprintf(agrep_finalfp, "\n");
588
if (agrep_outpointer+1>=agrep_outlen) {
590
free_buf(Text, buffer);
593
else agrep_outbuffer[agrep_outpointer++] = '\n';
596
free_buf(Text, buffer);
600
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
601
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
602
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
603
free_buf(Text, buffer);
608
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | Init0;
609
/* match begin of line */
610
if (DELIMITER) CurrentByteOffset += 1*D_length;
611
else CurrentByteOffset += 1*1;
613
} /* while i < end ... */
615
strncpy(buffer, buffer+num_read, Maxline);
616
} /* end while fill_buf()... */
618
free_buf(Text, buffer);
622
else { /* within the memory buffer: assume it starts with a newline at position 0, the actual pattern follows that, and it ends with a '\n' */
623
num_read = agrep_inlen;
624
buffer = (CHAR *)agrep_inbuffer;
626
/* buffer[end-1] = '\n';*/ /* at end of the text. */
627
/* buffer[0] = '\n';*/ /* in front of the text. */
630
/* An exact copy of the above RE1_PROCESS_WHEN_DZERO: the while-loop below */
634
CurrentByteOffset ++;
640
r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
648
if (DELIMITER) CurrentByteOffset -= D_length;
649
else CurrentByteOffset -= 1;
650
r1 = Init1 & r3; /* match against endofline */
651
r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
652
if(TAIL) r2 = (Next[r2>>hh] | Next1[r2&LL]) | r2; /* epsilon move */
653
if(( r2 & 1 ) ^ INVERSE) {
654
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
657
if (agrep_finalfp != NULL)
658
fprintf(agrep_finalfp, "%s", CurrentFileName);
661
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
662
(CurrentFileName[outindex] != '\0'); outindex++) {
663
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
665
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
667
free_buf(Text, buffer);
670
agrep_outpointer += outindex;
673
char *s = aprint_file_time(CurrentFileTime);
674
if (agrep_finalfp != NULL)
675
fprintf(agrep_finalfp, "%s", s);
678
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
679
(s[outindex] != '\0'); outindex++) {
680
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
682
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
684
free_buf(Text, buffer);
687
agrep_outpointer += outindex;
690
if (agrep_finalfp != NULL)
691
fprintf(agrep_finalfp, "\n");
693
if (agrep_outpointer+1>=agrep_outlen) {
695
free_buf(Text, buffer);
698
else agrep_outbuffer[agrep_outpointer++] = '\n';
701
free_buf(Text, buffer);
705
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
706
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
707
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
708
free_buf(Text, buffer);
713
r2 = (Next[r3>>hh] | Next1[r3&LL]) & CMask | Init0;
714
/* match begin of line */
715
if (DELIMITER) CurrentByteOffset += 1*D_length;
716
else CurrentByteOffset += 1*1;
719
CurrentByteOffset ++;
725
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
727
else r3 = r2 & Init1;
728
} /* if(NOT Newline) */
731
if (DELIMITER) CurrentByteOffset -= D_length;
732
else CurrentByteOffset -= 1;
733
r1 = Init1 & r2; /* match against endofline */
734
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
735
if(TAIL) r3 = ( Next[r3>>hh] | Next1[r3&LL] ) | r3;
737
if(( r3 & 1 ) ^ INVERSE) {
738
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
741
if (agrep_finalfp != NULL)
742
fprintf(agrep_finalfp, "%s", CurrentFileName);
745
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
746
(CurrentFileName[outindex] != '\0'); outindex++) {
747
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
749
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
751
free_buf(Text, buffer);
754
agrep_outpointer += outindex;
757
char *s = aprint_file_time(CurrentFileTime);
758
if (agrep_finalfp != NULL)
759
fprintf(agrep_finalfp, "%s", s);
762
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
763
(s[outindex] != '\0'); outindex++) {
764
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
766
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
768
free_buf(Text, buffer);
771
agrep_outpointer += outindex;
774
if (agrep_finalfp != NULL)
775
fprintf(agrep_finalfp, "\n");
777
if (agrep_outpointer+1>=agrep_outlen) {
779
free_buf(Text, buffer);
782
else agrep_outbuffer[agrep_outpointer++] = '\n';
785
free_buf(Text, buffer);
789
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
790
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
791
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
792
free_buf(Text, buffer);
797
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | Init0;
798
/* match begin of line */
799
if (DELIMITER) CurrentByteOffset += 1*D_length;
800
else CurrentByteOffset += 1*1;
802
} /* while i < end ... */
806
#endif /*AGREP_POINTER*/
807
} /* end if (D == 0) */
812
#endif /*AGREP_POINTER*/
813
while ((num_read = fill_buf(Text, buffer + Maxline, BlockSize)) > 0)
816
end = Maxline + num_read;
818
/* pab: Don't do this here; it's done in bitap.fill_buf,
819
* where we can handle eof on a block boundary right */
820
if((num_read < BlockSize) && buffer[end-1] != '\n') buffer[end++] = '\n';
822
if(FIRST_TIME) { /* if first time in the loop add a newline */
823
buffer[i-1] = '\n'; /* in front the text. */
825
CurrentByteOffset --;
829
/* RE1_PROCESS_WHEN_DNOTZERO: the while loop below */
839
A[0] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
842
r0 = r2 | A[0]; /* A[0] | B[0] */
843
A[1] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | (( r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
844
if(D == 1) goto Nextcharfile;
848
A[2] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
849
if(D == 2) goto Nextcharfile;
853
A[3] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
854
if(D == 3) goto Nextcharfile;
858
A[4] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
859
if(D == 4) goto Nextcharfile;
867
A[1] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
868
if(D == 1) goto Nextcharfile;
872
A[2] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
873
if(D == 2) goto Nextcharfile;
877
A[3] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
878
if(D == 3) goto Nextcharfile;
882
A[4] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
883
if(D == 4) goto Nextcharfile;
888
if (DELIMITER) CurrentByteOffset -= D_length;
889
else CurrentByteOffset -= 1;
890
r1 = Init1 & B[D]; /* match against endofline */
891
A[D] = ((Next[B[D]>>hh] | Next1[B[D]&LL]) & CMask) | r1;
892
if(TAIL) A[D] = ( Next[A[D]>>hh] | Next1[A[D]&LL] ) | A[D];
894
if(( A[D] & 1 ) ^ INVERSE) {
895
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
898
if (agrep_finalfp != NULL)
899
fprintf(agrep_finalfp, "%s", CurrentFileName);
902
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
903
(CurrentFileName[outindex] != '\0'); outindex++) {
904
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
906
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
908
free_buf(Text, buffer);
911
agrep_outpointer += outindex;
914
char *s = aprint_file_time(CurrentFileTime);
915
if (agrep_finalfp != NULL)
916
fprintf(agrep_finalfp, "%s", s);
919
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
920
(s[outindex] != '\0'); outindex++) {
921
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
923
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
925
free_buf(Text, buffer);
928
agrep_outpointer += outindex;
931
if (agrep_finalfp != NULL)
932
fprintf(agrep_finalfp, "\n");
934
if (agrep_outpointer+1>=agrep_outlen) {
936
free_buf(Text, buffer);
939
else agrep_outbuffer[agrep_outpointer++] = '\n';
942
free_buf(Text, buffer);
946
if (-1 == r_output(buffer, i, end, j)) {free_buf(Text, buffer); return -1;}
947
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
948
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
949
free_buf(Text, buffer);
953
for(k=0; k<=D; k++) B[k] = Init[0];
955
A[0] = (( Next[B[0]>>hh] | Next1[B[0]&LL]) & CMask) | r1;
956
for(k=1; k<=D; k++) {
959
r2 = A[k-1] | B[k-1];
960
A[k] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((B[k-1] | Next[r2>>hh] | Next1[r2&LL]) & r_NO_ERR) | r1;
962
if (DELIMITER) CurrentByteOffset += 1*D_length;
963
else CurrentByteOffset += 1*1;
967
CurrentByteOffset ++;
975
B[0] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
979
B[1] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((r2 | Next[r0>>hh] | Next1[r0&LL]) & r_NO_ERR) | r1 ;
980
if(D == 1) goto Nextchar1file;
984
B[2] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
985
if(D == 2) goto Nextchar1file;
989
B[3] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
990
if(D == 3) goto Nextchar1file;
994
B[4] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
995
if(D == 4) goto Nextchar1file;
1003
B[1] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1004
if(D == 1) goto Nextchar1file;
1008
B[2] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1009
if(D == 2) goto Nextchar1file;
1013
B[3] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1014
if(D == 3) goto Nextchar1file;
1018
B[4] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1019
if(D == 4) goto Nextchar1file;
1021
} /* if(NOT Newline) */
1024
if (DELIMITER) CurrentByteOffset -= D_length;
1025
else CurrentByteOffset -= 1;
1026
r1 = Init1 & A[D]; /* match against endofline */
1027
B[D] = ((Next[A[D]>>hh] | Next1[A[D]&LL]) & CMask) | r1;
1028
if(TAIL) B[D] = ( Next[B[D]>>hh] | Next1[B[D]&LL] ) | B[D];
1030
if(( B[D] & 1 ) ^ INVERSE) {
1031
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
1034
if (agrep_finalfp != NULL)
1035
fprintf(agrep_finalfp, "%s", CurrentFileName);
1038
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1039
(CurrentFileName[outindex] != '\0'); outindex++) {
1040
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
1042
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1044
free_buf(Text, buffer);
1047
agrep_outpointer += outindex;
1049
if (PRINTFILETIME) {
1050
char *s = aprint_file_time(CurrentFileTime);
1051
if (agrep_finalfp != NULL)
1052
fprintf(agrep_finalfp, "%s", s);
1055
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1056
(s[outindex] != '\0'); outindex++) {
1057
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
1059
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1061
free_buf(Text, buffer);
1064
agrep_outpointer += outindex;
1067
if (agrep_finalfp != NULL)
1068
fprintf(agrep_finalfp, "\n");
1070
if (agrep_outpointer+1>=agrep_outlen) {
1072
free_buf(Text, buffer);
1075
else agrep_outbuffer[agrep_outpointer++] = '\n';
1078
free_buf(Text, buffer);
1082
if (-1 == r_output(buffer, i, end, j)) {free_buf(Text, buffer); return -1;}
1083
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
1084
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
1085
free_buf(Text, buffer);
1086
return 0; /* done */
1089
for(k=0; k<=D; k++) A[k] = Init0;
1091
B[0] = ((Next[A[0]>>hh] | Next1[A[0]&LL]) & CMask) | r1;
1092
for(k=1; k<=D; k++) {
1095
r2 = A[k-1] | B[k-1];
1096
B[k] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((A[k-1] | Next[r2>>hh] | Next1[r2&LL]) & r_NO_ERR) | r1;
1098
if (DELIMITER) CurrentByteOffset += 1*D_length;
1099
else CurrentByteOffset += 1*1;
1103
CurrentByteOffset ++;
1104
} /* while i < end */
1106
strncpy(buffer, buffer+num_read, Maxline);
1107
} /* while fill_buf... */
1108
free_buf(Text, buffer);
1112
else { /* within the memory buffer: assume it starts with a newline at position 0, the actual pattern follows that, and it ends with a '\n' */
1113
num_read = agrep_inlen;
1114
buffer = (CHAR *)agrep_inbuffer;
1116
/* buffer[end-1] = '\n';*/ /* at end of the text. */
1117
/* buffer[0] = '\n';*/ /* in front of the text. */
1120
/* An exact copy of the above RE1_PROCESS_WHEN_DNOTZERO: the while loop below */
1130
A[0] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
1133
r0 = r2 | A[0]; /* A[0] | B[0] */
1134
A[1] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | (( r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1135
if(D == 1) goto Nextcharmem;
1139
A[2] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1140
if(D == 2) goto Nextcharmem;
1144
A[3] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1145
if(D == 3) goto Nextcharmem;
1149
A[4] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1150
if(D == 4) goto Nextcharmem;
1158
A[1] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1159
if(D == 1) goto Nextcharmem;
1163
A[2] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1164
if(D == 2) goto Nextcharmem;
1168
A[3] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1169
if(D == 3) goto Nextcharmem;
1173
A[4] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1174
if(D == 4) goto Nextcharmem;
1179
if (DELIMITER) CurrentByteOffset -= D_length;
1180
else CurrentByteOffset -= 1;
1181
r1 = Init1 & B[D]; /* match against endofline */
1182
A[D] = ((Next[B[D]>>hh] | Next1[B[D]&LL]) & CMask) | r1;
1183
if(TAIL) A[D] = ( Next[A[D]>>hh] | Next1[A[D]&LL] ) | A[D];
1185
if(( A[D] & 1 ) ^ INVERSE) {
1186
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
1189
if (agrep_finalfp != NULL)
1190
fprintf(agrep_finalfp, "%s", CurrentFileName);
1193
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1194
(CurrentFileName[outindex] != '\0'); outindex++) {
1195
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
1197
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1199
free_buf(Text, buffer);
1202
agrep_outpointer += outindex;
1204
if (PRINTFILETIME) {
1205
char *s = aprint_file_time(CurrentFileTime);
1206
if (agrep_finalfp != NULL)
1207
fprintf(agrep_finalfp, "%s", s);
1210
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1211
(s[outindex] != '\0'); outindex++) {
1212
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
1214
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1216
free_buf(Text, buffer);
1219
agrep_outpointer += outindex;
1222
if (agrep_finalfp != NULL)
1223
fprintf(agrep_finalfp, "\n");
1225
if (agrep_outpointer+1>=agrep_outlen) {
1227
free_buf(Text, buffer);
1230
else agrep_outbuffer[agrep_outpointer++] = '\n';
1233
free_buf(Text, buffer);
1237
if (-1 == r_output(buffer, i, end, j)) {free_buf(Text, buffer); return -1;}
1238
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
1239
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
1240
free_buf(Text, buffer);
1241
return 0; /* done */
1244
for(k=0; k<=D; k++) B[k] = Init[0];
1246
A[0] = (( Next[B[0]>>hh] | Next1[B[0]&LL]) & CMask) | r1;
1247
for(k=1; k<=D; k++) {
1250
r2 = A[k-1] | B[k-1];
1251
A[k] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((B[k-1] | Next[r2>>hh] | Next1[r2&LL]) & r_NO_ERR) | r1;
1253
if (DELIMITER) CurrentByteOffset += 1*D_length;
1254
else CurrentByteOffset += 1*1;
1258
CurrentByteOffset ++;
1266
B[0] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
1270
B[1] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((r2 | Next[r0>>hh] | Next1[r0&LL]) & r_NO_ERR) | r1 ;
1271
if(D == 1) goto Nextchar1mem;
1275
B[2] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1276
if(D == 2) goto Nextchar1mem;
1280
B[3] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1281
if(D == 3) goto Nextchar1mem;
1285
B[4] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1286
if(D == 4) goto Nextchar1mem;
1294
B[1] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1295
if(D == 1) goto Nextchar1mem;
1299
B[2] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1300
if(D == 2) goto Nextchar1mem;
1304
B[3] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1305
if(D == 3) goto Nextchar1mem;
1309
B[4] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
1310
if(D == 4) goto Nextchar1mem;
1312
} /* if(NOT Newline) */
1315
if (DELIMITER) CurrentByteOffset -= D_length;
1316
else CurrentByteOffset -= 1;
1317
r1 = Init1 & A[D]; /* match against endofline */
1318
B[D] = ((Next[A[D]>>hh] | Next1[A[D]&LL]) & CMask) | r1;
1319
if(TAIL) B[D] = ( Next[B[D]>>hh] | Next1[B[D]&LL] ) | B[D];
1321
if(( B[D] & 1 ) ^ INVERSE) {
1322
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
1325
if (agrep_finalfp != NULL)
1326
fprintf(agrep_finalfp, "%s", CurrentFileName);
1329
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1330
(CurrentFileName[outindex] != '\0'); outindex++) {
1331
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
1333
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1335
free_buf(Text, buffer);
1338
agrep_outpointer += outindex;
1340
if (PRINTFILETIME) {
1341
char *s = aprint_file_time(CurrentFileTime);
1342
if (agrep_finalfp != NULL)
1343
fprintf(agrep_finalfp, "%s", s);
1346
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1347
(s[outindex] != '\0'); outindex++) {
1348
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
1350
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1352
free_buf(Text, buffer);
1355
agrep_outpointer += outindex;
1358
if (agrep_finalfp != NULL)
1359
fprintf(agrep_finalfp, "\n");
1361
if (agrep_outpointer+1>=agrep_outlen) {
1363
free_buf(Text, buffer);
1366
else agrep_outbuffer[agrep_outpointer++] = '\n';
1369
free_buf(Text, buffer);
1373
if (-1 == r_output(buffer, i, end, j)) {free_buf(Text, buffer); return -1;}
1374
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
1375
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
1376
free_buf(Text, buffer);
1377
return 0; /* done */
1380
for(k=0; k<=D; k++) A[k] = Init0;
1382
B[0] = ((Next[A[0]>>hh] | Next1[A[0]&LL]) & CMask) | r1;
1383
for(k=1; k<=D; k++) {
1386
r2 = A[k-1] | B[k-1];
1387
B[k] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((A[k-1] | Next[r2>>hh] | Next1[r2&LL]) & r_NO_ERR) | r1;
1389
if (DELIMITER) CurrentByteOffset += 1*D_length;
1390
else CurrentByteOffset += 1*1;
1394
CurrentByteOffset ++;
1395
} /* while i < end */
1399
#endif /*AGREP_POINTER*/
1406
register unsigned i, c, r1, r2, r3, CMask, k, Newline, Init0, Init1, end;
1407
register unsigned r_even, r_odd, r_NO_ERR ;
1408
unsigned RMask[MAXSYM];
1409
unsigned A[MaxRerror+1], B[MaxRerror+1];
1410
int num_read, j=0, lasti, base, ResidueSize;
1411
int FIRST_TIME; /* Flag */
1417
compute_next(M, Next, Next1);
1420
for(i=0; i< MAXSYM; i++) RMask[i] = Mask[i];
1421
r_NO_ERR = NO_ERR_MASK;
1423
Init0 = Init[0] = Bit[base];
1424
if(HEAD) Init0 = Init[0] = Init0 | Bit[base+1] ;
1425
for(i=1; i<= D; i++) Init[i] = Init[i-1] | Next[Init[i-1]]; /* can be out? */
1428
for(k=0; k<= D; k++) {
1429
A[k] = B[k] = Init[0];
1432
alloc_buf(Text, &buffer, BlockSize+Maxline+1);
1437
#endif /*AGREP_POINTER*/
1439
while ((num_read = fill_buf(Text, buffer + Maxline, BlockSize)) > 0)
1442
end = Maxline + num_read ;
1444
/* pab: Don't do this here; it's done in bitap.fill_buf,
1445
* where we can handle eof on a block boundary right */
1446
if((num_read < BlockSize) && buffer[end-1] != '\n') buffer[end++] = '\n';
1451
CurrentByteOffset --;
1455
/* RE_PROCESS_WHEN_DZERO: the while-loop below */
1459
CurrentByteOffset ++;
1464
r2 = (Next[r3] & CMask) | r1;
1467
r1 = Init1 & r3; /* match against '\n' */
1468
r2 = Next[r3] & CMask | r1;
1470
if (DELIMITER) CurrentByteOffset -= D_length;
1471
else CurrentByteOffset -= 1;
1472
if(TAIL) r2 = Next[r2] | r2 ; /* epsilon move */
1473
if(( r2 & 1) ^ INVERSE) {
1474
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
1477
if (agrep_finalfp != NULL)
1478
fprintf(agrep_finalfp, "%s", CurrentFileName);
1481
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1482
(CurrentFileName[outindex] != '\0'); outindex++) {
1483
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
1485
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1487
free_buf(Text, buffer);
1490
agrep_outpointer += outindex;
1492
if (PRINTFILETIME) {
1493
char *s = aprint_file_time(CurrentFileTime);
1494
if (agrep_finalfp != NULL)
1495
fprintf(agrep_finalfp, "%s", s);
1498
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1499
(s[outindex] != '\0'); outindex++) {
1500
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
1502
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1504
free_buf(Text, buffer);
1507
agrep_outpointer += outindex;
1510
if (agrep_finalfp != NULL)
1511
fprintf(agrep_finalfp, "\n");
1513
if (agrep_outpointer+1>=agrep_outlen) {
1515
free_buf(Text, buffer);
1518
else agrep_outbuffer[agrep_outpointer++] = '\n';
1521
free_buf(Text, buffer);
1525
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
1526
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
1527
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
1528
free_buf(Text, buffer);
1529
return 0; /* done */
1534
r2 = (Next[r3] & CMask) | Init0;
1535
if (DELIMITER) CurrentByteOffset += 1*D_length;
1536
else CurrentByteOffset += 1*1;
1539
CurrentByteOffset ++;
1544
r3 = (Next[r2] & CMask) | r1;
1548
if (DELIMITER) CurrentByteOffset -= D_length;
1549
else CurrentByteOffset -= 1;
1550
r1 = Init1 & r2; /* match against endofline */
1551
r3 = Next[r2] & CMask | r1;
1552
if(TAIL) r3 = Next[r3] | r3;
1553
if(( r3 & 1) ^ INVERSE) {
1554
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
1557
if (agrep_finalfp != NULL)
1558
fprintf(agrep_finalfp, "%s", CurrentFileName);
1561
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1562
(CurrentFileName[outindex] != '\0'); outindex++) {
1563
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
1565
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1567
free_buf(Text, buffer);
1570
agrep_outpointer += outindex;
1572
if (PRINTFILETIME) {
1573
char *s = aprint_file_time(CurrentFileTime);
1574
if (agrep_finalfp != NULL)
1575
fprintf(agrep_finalfp, "%s", s);
1578
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1579
(s[outindex] != '\0'); outindex++) {
1580
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
1582
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1584
free_buf(Text, buffer);
1587
agrep_outpointer += outindex;
1590
if (agrep_finalfp != NULL)
1591
fprintf(agrep_finalfp, "\n");
1593
if (agrep_outpointer+1>=agrep_outlen) {
1595
free_buf(Text, buffer);
1598
else agrep_outbuffer[agrep_outpointer++] = '\n';
1601
free_buf(Text, buffer);
1605
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
1606
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
1607
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
1608
free_buf(Text, buffer);
1609
return 0; /* done */
1614
r3 = (Next[r2] & CMask) | Init0; /* match the newline */
1615
if (DELIMITER) CurrentByteOffset += 1*D_length;
1616
else CurrentByteOffset += 1*1;
1620
ResidueSize = Maxline + num_read - lasti;
1621
if(ResidueSize > Maxline) {
1622
ResidueSize = Maxline;
1624
strncpy(buffer+Maxline-ResidueSize, buffer+lasti, ResidueSize);
1625
lasti = Maxline - ResidueSize;
1626
} /* while fill_buf() */
1627
free_buf(Text, buffer);
1632
num_read = agrep_inlen;
1633
buffer = (CHAR *)agrep_inbuffer;
1635
/* buffer[end-1] = '\n';*/ /* at end of the text. */
1636
/* buffer[0] = '\n';*/ /* in front of the text. */
1640
/* An exact copy of the above RE_PROCESS_WHEN_DZERO: the while-loop below */
1644
CurrentByteOffset ++;
1649
r2 = (Next[r3] & CMask) | r1;
1652
r1 = Init1 & r3; /* match against '\n' */
1653
r2 = Next[r3] & CMask | r1;
1655
if (DELIMITER) CurrentByteOffset -= D_length;
1656
else CurrentByteOffset -= 1;
1657
if(TAIL) r2 = Next[r2] | r2 ; /* epsilon move */
1658
if(( r2 & 1) ^ INVERSE) {
1659
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
1662
if (agrep_finalfp != NULL)
1663
fprintf(agrep_finalfp, "%s", CurrentFileName);
1666
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1667
(CurrentFileName[outindex] != '\0'); outindex++) {
1668
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
1670
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1672
free_buf(Text, buffer);
1675
agrep_outpointer += outindex;
1677
if (PRINTFILETIME) {
1678
char *s = aprint_file_time(CurrentFileTime);
1679
if (agrep_finalfp != NULL)
1680
fprintf(agrep_finalfp, "%s", s);
1683
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1684
(s[outindex] != '\0'); outindex++) {
1685
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
1687
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1689
free_buf(Text, buffer);
1692
agrep_outpointer += outindex;
1695
if (agrep_finalfp != NULL)
1696
fprintf(agrep_finalfp, "\n");
1698
if (agrep_outpointer+1>=agrep_outlen) {
1700
free_buf(Text, buffer);
1703
else agrep_outbuffer[agrep_outpointer++] = '\n';
1706
free_buf(Text, buffer);
1710
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
1711
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
1712
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
1713
free_buf(Text, buffer);
1714
return 0; /* done */
1719
r2 = (Next[r3] & CMask) | Init0;
1720
if (DELIMITER) CurrentByteOffset += 1*D_length;
1721
else CurrentByteOffset += 1*1;
1724
CurrentByteOffset ++;
1729
r3 = (Next[r2] & CMask) | r1;
1733
if (DELIMITER) CurrentByteOffset -= D_length;
1734
else CurrentByteOffset -= 1;
1735
r1 = Init1 & r2; /* match against endofline */
1736
r3 = Next[r2] & CMask | r1;
1737
if(TAIL) r3 = Next[r3] | r3;
1738
if(( r3 & 1) ^ INVERSE) {
1739
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
1742
if (agrep_finalfp != NULL)
1743
fprintf(agrep_finalfp, "%s", CurrentFileName);
1746
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1747
(CurrentFileName[outindex] != '\0'); outindex++) {
1748
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
1750
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1752
free_buf(Text, buffer);
1755
agrep_outpointer += outindex;
1757
if (PRINTFILETIME) {
1758
char *s = aprint_file_time(CurrentFileTime);
1759
if (agrep_finalfp != NULL)
1760
fprintf(agrep_finalfp, "%s", s);
1763
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1764
(s[outindex] != '\0'); outindex++) {
1765
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
1767
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1769
free_buf(Text, buffer);
1772
agrep_outpointer += outindex;
1775
if (agrep_finalfp != NULL)
1776
fprintf(agrep_finalfp, "\n");
1778
if (agrep_outpointer+1>=agrep_outlen) {
1780
free_buf(Text, buffer);
1783
else agrep_outbuffer[agrep_outpointer++] = '\n';
1786
free_buf(Text, buffer);
1790
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
1791
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
1792
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
1793
free_buf(Text, buffer);
1794
return 0; /* done */
1799
r3 = (Next[r2] & CMask) | Init0; /* match the newline */
1800
if (DELIMITER) CurrentByteOffset += 1*D_length;
1801
else CurrentByteOffset += 1*1;
1805
/* If a residue is left for within-memory-buffer, since nothing can be "read" after that, we can ignore it: as if only 1 iteration of while */
1808
#endif /*AGREP_POINTER*/
1809
} /* end if(D==0) */
1813
#endif /*AGREP_POINTER*/
1814
while ((num_read = fill_buf(Text, buffer + Maxline, BlockSize)) > 0)
1817
end = Maxline+num_read;
1819
/* pab: Don't do this here; it's done in bitap.fill_buf,
1820
* where we can handle eof on a block boundary right */
1821
if((num_read < BlockSize) && buffer[end-1] != '\n') buffer[end++] = '\n';
1826
CurrentByteOffset --;
1830
/* RE_PROCESS_WHEN_DNOTZERO: the while-loop below */
1834
CurrentByteOffset ++;
1839
r1 = Init1 & r_even;
1840
A[0] = (Next[r_even] & CMask) | r1;
1843
r2 = (r_even | Next[r_even|A[0]]) &r_NO_ERR;
1844
A[1] = (Next[r_odd] & CMask) | r2 | r1 ;
1845
if(D == 1) goto Nextcharfile;
1847
r1 = Init1 & r_even;
1848
r2 = (r_odd | Next[r_odd|A[1]]) &r_NO_ERR;
1849
A[2] = (Next[r_even] & CMask) | r2 | r1 ;
1850
if(D == 2) goto Nextcharfile;
1853
r2 = (r_even | Next[r_even|A[2]]) &r_NO_ERR;
1854
A[3] = (Next[r_odd] & CMask) | r2 | r1 ;
1855
if(D == 3) goto Nextcharfile;
1857
r1 = Init1 & r_even;
1858
r2 = (r_odd | Next[r_odd|A[3]]) &r_NO_ERR;
1859
A[4] = (Next[r_even] & CMask) | r2 | r1 ;
1861
} /* if NOT Newline */
1864
if (DELIMITER) CurrentByteOffset -= D_length;
1865
else CurrentByteOffset -= 1;
1866
r1 = Init1 & B[D]; /* match endofline */
1867
A[D] = (Next[B[D]] & CMask) | r1;
1868
if(TAIL) A[D] = Next[A[D]] | A[D];
1869
if((A[D] & 1) ^ INVERSE ) {
1870
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
1873
if (agrep_finalfp != NULL)
1874
fprintf(agrep_finalfp, "%s", CurrentFileName);
1877
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1878
(CurrentFileName[outindex] != '\0'); outindex++) {
1879
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
1881
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1883
free_buf(Text, buffer);
1886
agrep_outpointer += outindex;
1888
if (PRINTFILETIME) {
1889
char *s = aprint_file_time(CurrentFileTime);
1890
if (agrep_finalfp != NULL)
1891
fprintf(agrep_finalfp, "%s", s);
1894
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1895
(s[outindex] != '\0'); outindex++) {
1896
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
1898
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1900
free_buf(Text, buffer);
1903
agrep_outpointer += outindex;
1906
if (agrep_finalfp != NULL)
1907
fprintf(agrep_finalfp, "\n");
1909
if (agrep_outpointer+1>=agrep_outlen) {
1911
free_buf(Text, buffer);
1914
else agrep_outbuffer[agrep_outpointer++] = '\n';
1917
free_buf(Text, buffer);
1921
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
1922
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
1923
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
1924
free_buf(Text, buffer);
1925
return 0; /* done */
1928
for(k=0; k<= D; k++) {
1929
A[k] = B[k] = Init[k];
1932
A[0] = (Next[B[0]] & CMask) | r1;
1933
for(k=1; k<= D; k++) {
1935
r2 = (B[k-1] | Next[A[k-1]|B[k-1]]) &r_NO_ERR;
1936
A[k] = (Next[B[k]] & CMask) | r1 | r2;
1938
if (DELIMITER) CurrentByteOffset += 1*D_length;
1939
else CurrentByteOffset += 1*1;
1947
B[0] = (Next[A[0]] & CMask) | r1;
1949
B[1] = (Next[A[1]] & CMask) | ((A[0] | Next[A[0] | B[0]]) & r_NO_ERR) | r1 ;
1950
if(D == 1) goto Nextchar1file;
1952
B[2] = (Next[A[2]] & CMask) | ((A[1] | Next[A[1] | B[1]]) &r_NO_ERR) | r1 ;
1953
if(D == 2) goto Nextchar1file;
1955
B[3] = (Next[A[3]] & CMask) | ((A[2] | Next[A[2] | B[2]])&r_NO_ERR) | r1 ;
1956
if(D == 3) goto Nextchar1file;
1958
B[4] = (Next[A[4]] & CMask) | ((A[3] | Next[A[3] | B[3]])&r_NO_ERR) | r1 ;
1960
} /* if(NOT Newline) */
1963
if (DELIMITER) CurrentByteOffset -= D_length;
1964
else CurrentByteOffset -= 1;
1965
r1 = Init1 & A[D]; /* match endofline */
1966
B[D] = (Next[A[D]] & CMask) | r1;
1967
if(TAIL) B[D] = Next[B[D]] | B[D];
1968
if((B[D] & 1) ^ INVERSE ) {
1969
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
1972
if (agrep_finalfp != NULL)
1973
fprintf(agrep_finalfp, "%s", CurrentFileName);
1976
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1977
(CurrentFileName[outindex] != '\0'); outindex++) {
1978
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
1980
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1982
free_buf(Text, buffer);
1985
agrep_outpointer += outindex;
1987
if (PRINTFILETIME) {
1988
char *s = aprint_file_time(CurrentFileTime);
1989
if (agrep_finalfp != NULL)
1990
fprintf(agrep_finalfp, "%s", s);
1993
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
1994
(s[outindex] != '\0'); outindex++) {
1995
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
1997
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
1999
free_buf(Text, buffer);
2002
agrep_outpointer += outindex;
2005
if (agrep_finalfp != NULL)
2006
fprintf(agrep_finalfp, "\n");
2008
if (agrep_outpointer+1>=agrep_outlen) {
2010
free_buf(Text, buffer);
2013
else agrep_outbuffer[agrep_outpointer++] = '\n';
2016
free_buf(Text, buffer);
2020
if (-1 == r_output(buffer, i, end, j)) {free_buf(Text, buffer); return -1;}
2021
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
2022
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
2023
free_buf(Text, buffer);
2024
return 0; /* done */
2027
for(k=0; k<= D; k++) {
2028
A[k] = B[k] = Init[k];
2031
B[0] = (Next[A[0]] & CMask) | r1;
2032
for(k=1; k<= D; k++) {
2034
r2 = (A[k-1] | Next[A[k-1]|B[k-1]])&r_NO_ERR;
2035
B[k] = (Next[A[k]] & CMask) | r1 | r2;
2037
if (DELIMITER) CurrentByteOffset += 1*D_length;
2038
else CurrentByteOffset += 1*1;
2042
CurrentByteOffset ++;
2043
} /* while i < end */
2045
strncpy(buffer, buffer+num_read, Maxline);
2046
} /* while fill_buf() */
2047
free_buf(Text, buffer);
2052
num_read = agrep_inlen;
2053
buffer = (CHAR *)agrep_inbuffer;
2055
/* buffer[end-1] = '\n';*/ /* at end of the text. */
2056
/* buffer[0] = '\n';*/ /* in front of the text. */
2059
/* An exact copy of the above RE_PROCESS_WHEN_DNOTZERO: the while-loop below */
2063
CurrentByteOffset ++;
2068
r1 = Init1 & r_even;
2069
A[0] = (Next[r_even] & CMask) | r1;
2072
r2 = (r_even | Next[r_even|A[0]]) &r_NO_ERR;
2073
A[1] = (Next[r_odd] & CMask) | r2 | r1 ;
2074
if(D == 1) goto Nextcharmem;
2076
r1 = Init1 & r_even;
2077
r2 = (r_odd | Next[r_odd|A[1]]) &r_NO_ERR;
2078
A[2] = (Next[r_even] & CMask) | r2 | r1 ;
2079
if(D == 2) goto Nextcharmem;
2082
r2 = (r_even | Next[r_even|A[2]]) &r_NO_ERR;
2083
A[3] = (Next[r_odd] & CMask) | r2 | r1 ;
2084
if(D == 3) goto Nextcharmem;
2086
r1 = Init1 & r_even;
2087
r2 = (r_odd | Next[r_odd|A[3]]) &r_NO_ERR;
2088
A[4] = (Next[r_even] & CMask) | r2 | r1 ;
2090
} /* if NOT Newline */
2093
if (DELIMITER) CurrentByteOffset -= D_length;
2094
else CurrentByteOffset -= 1;
2095
r1 = Init1 & B[D]; /* match endofline */
2096
A[D] = (Next[B[D]] & CMask) | r1;
2097
if(TAIL) A[D] = Next[A[D]] | A[D];
2098
if((A[D] & 1) ^ INVERSE ) {
2099
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
2102
if (agrep_finalfp != NULL)
2103
fprintf(agrep_finalfp, "%s", CurrentFileName);
2106
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
2107
(CurrentFileName[outindex] != '\0'); outindex++) {
2108
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
2110
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
2112
free_buf(Text, buffer);
2115
agrep_outpointer += outindex;
2117
if (PRINTFILETIME) {
2118
char *s = aprint_file_time(CurrentFileTime);
2119
if (agrep_finalfp != NULL)
2120
fprintf(agrep_finalfp, "%s", s);
2123
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
2124
(s[outindex] != '\0'); outindex++) {
2125
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
2127
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
2129
free_buf(Text, buffer);
2132
agrep_outpointer += outindex;
2135
if (agrep_finalfp != NULL)
2136
fprintf(agrep_finalfp, "\n");
2138
if (agrep_outpointer+1>=agrep_outlen) {
2140
free_buf(Text, buffer);
2143
else agrep_outbuffer[agrep_outpointer++] = '\n';
2146
free_buf(Text, buffer);
2150
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
2151
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
2152
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
2153
free_buf(Text, buffer);
2154
return 0; /* done */
2157
for(k=0; k<= D; k++) {
2158
A[k] = B[k] = Init[k];
2161
A[0] = (Next[B[0]] & CMask) | r1;
2162
for(k=1; k<= D; k++) {
2164
r2 = (B[k-1] | Next[A[k-1]|B[k-1]]) &r_NO_ERR;
2165
A[k] = (Next[B[k]] & CMask) | r1 | r2;
2167
if (DELIMITER) CurrentByteOffset += 1*D_length;
2168
else CurrentByteOffset += 1*1;
2176
B[0] = (Next[A[0]] & CMask) | r1;
2178
B[1] = (Next[A[1]] & CMask) | ((A[0] | Next[A[0] | B[0]]) & r_NO_ERR) | r1 ;
2179
if(D == 1) goto Nextchar1mem;
2181
B[2] = (Next[A[2]] & CMask) | ((A[1] | Next[A[1] | B[1]]) &r_NO_ERR) | r1 ;
2182
if(D == 2) goto Nextchar1mem;
2184
B[3] = (Next[A[3]] & CMask) | ((A[2] | Next[A[2] | B[2]])&r_NO_ERR) | r1 ;
2185
if(D == 3) goto Nextchar1mem;
2187
B[4] = (Next[A[4]] & CMask) | ((A[3] | Next[A[3] | B[3]])&r_NO_ERR) | r1 ;
2189
} /* if(NOT Newline) */
2192
if (DELIMITER) CurrentByteOffset -= D_length;
2193
else CurrentByteOffset -= 1;
2194
r1 = Init1 & A[D]; /* match endofline */
2195
B[D] = (Next[A[D]] & CMask) | r1;
2196
if(TAIL) B[D] = Next[B[D]] | B[D];
2197
if((B[D] & 1) ^ INVERSE ) {
2198
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
2201
if (agrep_finalfp != NULL)
2202
fprintf(agrep_finalfp, "%s", CurrentFileName);
2205
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
2206
(CurrentFileName[outindex] != '\0'); outindex++) {
2207
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
2209
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
2211
free_buf(Text, buffer);
2214
agrep_outpointer += outindex;
2216
if (PRINTFILETIME) {
2217
char *s = aprint_file_time(CurrentFileTime);
2218
if (agrep_finalfp != NULL)
2219
fprintf(agrep_finalfp, "%s", s);
2222
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
2223
(s[outindex] != '\0'); outindex++) {
2224
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
2226
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
2228
free_buf(Text, buffer);
2231
agrep_outpointer += outindex;
2234
if (agrep_finalfp != NULL)
2235
fprintf(agrep_finalfp, "\n");
2237
if (agrep_outpointer+1>=agrep_outlen) {
2239
free_buf(Text, buffer);
2242
else agrep_outbuffer[agrep_outpointer++] = '\n';
2245
free_buf(Text, buffer);
2249
if (-1 == r_output(buffer, i, end, j)) {free_buf(Text, buffer); return -1;}
2250
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
2251
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
2252
free_buf(Text, buffer);
2253
return 0; /* done */
2256
for(k=0; k<= D; k++) {
2257
A[k] = B[k] = Init[k];
2260
B[0] = (Next[A[0]] & CMask) | r1;
2261
for(k=1; k<= D; k++) {
2263
r2 = (A[k-1] | Next[A[k-1]|B[k-1]])&r_NO_ERR;
2264
B[k] = (Next[A[k]] & CMask) | r1 | r2;
2266
if (DELIMITER) CurrentByteOffset += 1*D_length;
2267
else CurrentByteOffset += 1*1;
2271
CurrentByteOffset ++;
2272
} /* while i < end */
2276
#endif /*AGREP_POINTER*/
2280
r_output (buffer, i, end, j)
2286
if(i >= end) return 0;
2287
if ((j < 1) || (CurrentByteOffset < 0)) return 0;
2290
if (SILENT) return 0;
2291
if(FNAME && (NEW_FILE || !POST_FILTER)) {
2292
char nextchar = (POST_FILTER == ON)?'\n':' ';
2293
char *prevstring = (POST_FILTER == ON)?"\n":"";
2295
if (agrep_finalfp != NULL)
2296
fprintf(agrep_finalfp, "%s%s", prevstring, CurrentFileName);
2299
if (prevstring[0] != '\0') {
2300
if(agrep_outpointer + 1 >= agrep_outlen) {
2304
else agrep_outbuffer[agrep_outpointer ++] = prevstring[0];
2306
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
2307
(CurrentFileName[outindex] != '\0'); outindex++) {
2308
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
2310
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
2314
agrep_outpointer += outindex;
2316
if (PRINTFILETIME) {
2317
char *s = aprint_file_time(CurrentFileTime);
2318
if (agrep_finalfp != NULL)
2319
fprintf(agrep_finalfp, "%s", s);
2322
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
2323
(s[outindex] != '\0'); outindex++) {
2324
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
2326
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
2330
agrep_outpointer += outindex;
2333
if (agrep_finalfp != NULL)
2334
fprintf(agrep_finalfp, ":%c", nextchar);
2336
if (agrep_outpointer+2>= agrep_outlen) {
2341
agrep_outbuffer[agrep_outpointer++] = ':';
2342
agrep_outbuffer[agrep_outpointer++] = nextchar;
2350
while ((buffer[bp] != '\n') && (bp > 0)) bp--;
2352
if (agrep_finalfp != NULL)
2353
fprintf(agrep_finalfp, "%d: ", j-1);
2358
sprintf(s, "%d: ", j-1);
2359
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
2360
(s[outindex] != '\0'); outindex++) {
2361
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
2363
if (s[outindex] != '\0') {
2367
agrep_outpointer += outindex;
2373
if (agrep_finalfp != NULL)
2374
fprintf(agrep_finalfp, "%d= ", CurrentByteOffset);
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];
2383
if (s[outindex] != '\0') {
2387
agrep_outpointer += outindex;
2392
if(buffer[bp] != '\n') bp = Maxline-1;
2396
if (agrep_finalfp != NULL)
2397
fprintf(agrep_finalfp, "@%d{%d} ", CurrentByteOffset - (i-bp), i-bp);
2401
sprintf(s, "@%d{%d} ", CurrentByteOffset - (i-bp), i-bp);
2402
for (outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
2403
(s[outindex] != '\0'); outindex ++) {
2404
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
2406
if (s[outindex] != '\0') {
2410
agrep_outpointer += outindex;
2415
if (agrep_finalfp != NULL)
2416
while(bp <= i) fputc(buffer[bp++], agrep_finalfp);
2418
if (i - bp + 1 + agrep_outpointer >= agrep_outlen) {
2422
while(bp <= i) agrep_outbuffer[agrep_outpointer ++] = buffer[bp++];
2426
if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);
2427
else agrep_outbuffer[agrep_outpointer ++] = '\n';
2435
* Processes the options specified in argc and argv, and fetches the pattern.
2436
* Also sets the set of filenames to be searched for internally. Returns: -1
2437
* if there is a serious error, 0 if there is no pattern or an error in getting
2438
* the file names, the length (> 0) of the pattern if there is no error. When a
2439
* 0 is returned, it means that at least the options were processed correctly.
2442
agrep_init(argc, argv, initialfd, pattern_len, pattern_buffer)
2447
CHAR *pattern_buffer;
2449
int i, j, seenlsq = 0;
2452
char **original_argv = argv;
2458
if (pattern_len < 1) {
2459
fprintf(stderr, "agrep_init: pattern length %d too small\n", pattern_len);
2463
agrep_initialfd = initialfd;
2464
strncpy(Progname, argv[0], MAXNAME);
2465
if (argc < 2) return agrep_usage();
2466
printf(""); /* dummy statement which avoids program crash with
2467
SYS3175 when piping the output of complex AGREP
2468
results into a file.
2470
This bug is regarded as COMPILER-UNSPECIFIC.
2472
For sure, the problem SHOULD BE FIXED somewhere
2473
else in AGREP, later.
2476
Thomas Gries gries@epo.e-mail.com, gries@ibm.net
2485
while(--argc > 0 && (*++argv)[0] == '-') { /* argv is incremented automatically here */
2486
p = argv[0]+1; /* ptr to first character after '-' */
2489
while(!quitwhile && (*p != '\0')) {
2493
NOOUTPUTZERO = ON; /* don't output files with 0 matches */
2494
PRINT(printf("z\n");
2499
COUNT = ON; /* output the # of matches */
2500
PRINT(printf("c\n");
2505
SILENT = ON; /* silent mode */
2506
PRINT(printf("s\n");
2511
I = 0; /* insertion cost is 0 */
2512
PRINT(printf("p\n");
2516
PRINTPATTERN = 1; /* print pattern before every matched line */
2517
PRINT(printf("p\n");
2522
WHOLELINE = ON; /* match the whole line */
2523
PRINT(printf("x\n");
2526
fprintf(stderr, "%s: illegal option combination (-x and -w)\n", Progname);
2528
errno = AGREP_ERROR;
2537
PRINT(printf("b\n");
2543
PRINT(printf("q\n");
2549
PRINT(printf("u\n");
2554
PRINTNONEXISTENTFILE = ON;
2555
PRINT(printf("X\n");
2560
PRINTFILENUMBER = ON;
2561
PRINT(printf("g\n");
2567
PRINT(printf("@\n");
2572
if ( *(p + 1) == '\0') {/* space after -L option */
2574
fprintf(stderr, "%s: the -L option must have an output-limit argument\n", Progname);
2576
errno = AGREP_ERROR;
2582
LIMITOUTPUT = LIMITTOTALFILE = LIMITPERFILE = 0;
2583
sscanf(argv[0], "%d:%d:%d", &LIMITOUTPUT, &LIMITTOTALFILE, &LIMITPERFILE);
2584
if ((LIMITOUTPUT < 0) || (LIMITTOTALFILE < 0) || (LIMITPERFILE < 0)) {
2585
fprintf(stderr, "%s: invalid output limit %s\n", Progname, argv[0]);
2587
errno = AGREP_ERROR;
2595
LIMITOUTPUT = LIMITTOTALFILE = LIMITPERFILE = 0;
2596
sscanf(p+1, "%d:%d:%d", &LIMITOUTPUT, &LIMITTOTALFILE, &LIMITPERFILE);
2597
if ((LIMITOUTPUT < 0) || (LIMITTOTALFILE < 0) || (LIMITPERFILE < 0)) {
2598
fprintf(stderr, "%s: invalid output limit %s\n", Progname, p+1);
2600
errno = AGREP_ERROR;
2606
PRINT(printf("L\n");
2612
DELIMITER = ON; /* user defines delimiter */
2613
PRINT(printf("d\n");
2615
if ( *(p + 1) == '\0') {/* space after -d option */
2617
fprintf(stderr, "%s: the -d option must have a delimiter argument\n", Progname);
2619
errno = AGREP_ERROR;
2625
if ((D_length = strlen(argv[0])) > MaxDelimit) {
2626
fprintf(stderr, "%s: delimiter pattern too long (has > %d chars)\n", Progname, MaxDelimit);
2628
errno = AGREP_ERROR;
2634
strcpy(D_pattern+1, argv[0]);
2635
if (((argv[0][D_length-1] == '\n') || (argv[0][D_length-1] == '$') || (argv[0][D_length-1] == '^')) && (D_length == 1))
2638
PRINT(printf("space\n");
2642
if ((D_length = strlen(p + 1)) > MaxDelimit) {
2643
fprintf(stderr, "%s: delimiter pattern too long (has > %d chars)\n", Progname, MaxDelimit);
2645
errno = AGREP_ERROR;
2651
strcpy(D_pattern+1, p + 1);
2652
if ((((p+1)[D_length-1] == '\n') || ((p+1)[D_length-1] == '$') || ((p+1)[D_length-1] == '^')) && (D_length == 1))
2655
strcat(D_pattern, ">; ");
2656
D_length++; /* to count '<' as one */
2657
PRINT(printf("D_pattern=%s\n", D_pattern);
2659
strcpy(original_D_pattern, D_pattern);
2660
original_D_length = D_length;
2665
if (*(p + 1) == '\0') {/* space after - option */
2667
fprintf(stderr, "%s: a directory name must follow the -H option\n", Progname);
2669
errno = AGREP_ERROR;
2670
return agrep_usage();
2675
strcpy(COMP_DIR, argv[0]);
2679
strcpy(COMP_DIR, p+1);
2685
if ( *(p + 1) == '\0') {/* space after -e option */
2687
fprintf(stderr, "%s: the -e option must have a pattern argument\n", Progname);
2689
errno = AGREP_ERROR;
2695
if(argv[0][0] == '-') { /* not strictly necessary but no harm done */
2697
strcat(Pattern, (argv)[0]);
2699
else strcat(Pattern, argv[0]);
2703
if (*(p+1) == '-') { /* not strictly necessary but no harm done */
2705
strcat(Pattern, p+1);
2707
else strcat (Pattern, p+1);
2710
PRINT(printf("Pattern=%s\n", Pattern);
2712
pattern_index = abs(argv - original_argv);
2718
if ( *(p + 1) == '\0') {/* space after -e option */
2720
fprintf(stderr, "%s: the -k option must have a pattern argument\n", Progname);
2722
errno = AGREP_ERROR;
2728
strcat(Pattern, argv[0]);
2729
if((argc > 2) && (argv[1][0] == '-')) {
2730
fprintf(stderr, "%s: -k should be the last option in the command\n", Progname);
2732
errno = AGREP_ERROR;
2740
if((argc > 1) && (argv[1][0] == '-')) {
2741
fprintf(stderr, "%s: -k should be the last option in the command\n", Progname);
2743
errno = AGREP_ERROR;
2748
strcat (Pattern, p+1);
2751
pattern_index = abs(argv - original_argv);
2756
if (PAT_FILE == ON) {
2757
fprintf(stderr, "%s: multiple -f options\n", Progname);
2758
if (multifd >= 0) close(multifd);
2760
errno = AGREP_ERROR;
2765
if (PAT_BUFFER == ON) {
2766
fprintf(stderr, "%s: -f and -m are incompatible\n", Progname);
2767
if (multibuf != NULL) free(multibuf);
2771
errno = AGREP_ERROR;
2777
PRINT(printf("f\n");
2782
if (argv[0] == NULL) {
2783
/* A -f option with a NULL file name is a NO-OP: stupid, but simplifies glimpse :-) */
2789
if((multifd = open(argv[0], O_RDONLY)) < 0) {
2791
fprintf(stderr, "%s: can't open pattern file for reading: %s\n", Progname, argv[0]);
2793
errno = AGREP_ERROR;
2798
PRINT(printf("file=%s\n", argv[0]);
2800
strcpy(PAT_FILE_NAME, argv[0]);
2801
if (prepf(multifd, NULL, 0) <= -1) {
2804
fprintf(stderr, "%s: error in processing pattern file: %s\n", Progname, argv[0]);
2806
errno = AGREP_ERROR;
2815
if (PAT_BUFFER == ON) {
2816
fprintf(stderr, "%s: multiple -m options\n", Progname);
2817
if (multibuf != NULL) free(multibuf);
2821
errno = AGREP_ERROR;
2826
if (PAT_FILE == ON) {
2827
fprintf(stderr, "%s: -f and -m are incompatible\n", Progname);
2828
if (multifd >= 0) close(multifd);
2830
errno = AGREP_ERROR;
2836
PRINT(printf("m\n");
2842
if ((argv[0] == NULL) || ((multilen = strlen(argv[0])) <= 0)) {
2843
/* A -m option with a NULL or empty pattern buffer is a NO-OP: stupid, but simplifies glimpse :-) */
2845
if (multibuf != NULL) free(multibuf);
2850
multibuf = (char *)malloc(multilen + 2);
2851
strcpy(multibuf, argv[0]);
2852
PRINT(printf("patterns=%s\n", multibuf);
2854
if (prepf(-1, multibuf, multilen) <= -1) {
2859
fprintf(stderr, "%s: error in processing pattern buffer\n", Progname);
2861
errno = AGREP_ERROR;
2872
PRINT(printf("h\n");
2878
PRINT(printf("i\n");
2884
PRINT(printf("l\n");
2889
LINENUM = ON; /* output prefixed by line no*/
2890
PRINT(printf("n\n");
2896
PRINT(printf("r\n");
2901
printf("\nThis is agrep version %s, %s.\n\n", AGREP_VERSION, AGREP_DATE);
2905
INVERSE = ON; /* output no-matched lines */
2906
PRINT(printf("v\n");
2911
OUTTAIL = ON; /* output from tail of delimiter */
2912
PRINT(printf("t\n");
2917
NOOUTTAIL = ON; /* output from front of delimiter */
2918
PRINT(printf("t\n");
2924
PRINT(printf("B\n");
2929
WORDBOUND = ON;/* match to words */
2930
PRINT(printf("w\n");
2933
fprintf(stderr, "%s: illegal option combination (-w and -x)\n", Progname);
2935
errno = AGREP_ERROR;
2944
PRINT(printf("y\n");
2949
I = atoi(p + 1); /* Insertion Cost */
2955
S = atoi(p + 1); /* Substitution Cost */
2961
DD = atoi(p + 1); /* Deletion Cost */
2972
ALWAYSFILENAME = ON;
2983
case 'Z': break; /* no-op: used by glimpse */
2990
fprintf(stderr,"%s: the maximum number of errors is %d\n", Progname, MaxError);
2992
errno = AGREP_ERROR;
2997
quitwhile = ON; /* note that even a number should occur at the end of a group of options, as f & e */
3000
fprintf(stderr, "%s: illegal option -%c\n",Progname, c);
3001
return agrep_usage();
3007
} /* while (--argc > 0 && (*++argv)[0] == '-') */
3009
if (NOOUTTAIL == ON) OUTTAIL = OFF;
3011
if (COMP_DIR[0] == '\0') {
3012
if ((home = (char *)getenv("HOME")) == NULL) {
3013
getcwd(COMP_DIR, MAX_LINE_LEN-1);
3014
fprintf(stderr, "using working-directory '%s' to locate dictionaries\n", COMP_DIR);
3016
else strncpy(COMP_DIR, home, MAX_LINE_LEN);
3019
strcpy(FREQ_FILE, COMP_DIR);
3020
strcat(FREQ_FILE, "/");
3021
strcat(FREQ_FILE, DEF_FREQ_FILE);
3022
strcpy(HASH_FILE, COMP_DIR);
3023
strcat(HASH_FILE, "/");
3024
strcat(HASH_FILE, DEF_HASH_FILE);
3025
strcpy(STRING_FILE, COMP_DIR);
3026
strcat(STRING_FILE, "/");
3027
strcat(STRING_FILE, DEF_STRING_FILE);
3028
initialize_common(FREQ_FILE, 0); /* no error msgs */
3030
if (FILENAMEONLY && NOFILENAME) {
3031
fprintf(stderr, "%s: -h and -l options are mutually exclusive\n",Progname);
3033
if (COUNT && (FILENAMEONLY || NOFILENAME)) {
3035
if(!FILEOUT) NOFILENAME = OFF;
3048
if (!(PAT_FILE || PAT_BUFFER) && Pattern[0] == '\0') { /* Pattern not set with -e option */
3053
strcpy(Pattern, *argv);
3054
pattern_index = abs(argv - original_argv);
3058
/* if multi-pattern search, just ignore any specified pattern altogether: treat it as a filename */
3060
if (copied_from_argv) {
3061
for (i=0; i<Numfiles; i++) free(Textfiles[i]);
3062
if (Textfiles != NULL) free(Textfiles);
3064
copied_from_argv = 0;
3065
Numfiles = 0; Textfiles = NULL;
3066
execfd = agrep_initialfd;
3067
if (argc <= 0) /* check Pattern against stdin */
3069
else { /* filenames were specified as a part of the command line */
3070
if (!(Textfiles = (CHAR **)malloc(argc * sizeof(CHAR *) ))) {
3071
fprintf(stderr, "%s: malloc failure in %s:%d\n", Progname, __FILE__, __LINE__);
3073
errno = AGREP_ERROR;
3078
copied_from_argv = 1; /* should I free Textfiles next time? */
3080
{ /* one or more filenames on command line -- put the valid filenames in a array of strings */
3082
if (!glimpse_call && ((filetype = check_file(*argv)) == NOSUCHFILE) && !PRINTNONEXISTENTFILE) {
3083
fprintf(stderr,"%s: '%s' no such file or directory\n",Progname,*argv);
3086
else { /* file is ascii*/
3087
if (!(Textfiles[Numfiles] = (CHAR *)malloc((strlen(*argv)+2)))) {
3088
fprintf(stderr, "%s: malloc failure in %s:%d\n", Progname, __FILE__, __LINE__);
3090
errno = AGREP_ERROR;
3095
strcpy(Textfiles[Numfiles++], *argv++);
3097
} /* while (argc--) */
3098
if (Numfiles <= 0) return 0;
3099
/* If user specified wrong filenames, then we quit rather than taking input from stdin! */
3102
M = strlen(Pattern);
3104
for (i=0; i<M; i++) {
3105
if (( ((unsigned char *)Pattern)[i] > USERRANGE_MIN) && ( ((unsigned char *)Pattern)[i] <= USERRANGE_MAX)) {
3106
fprintf(stderr, "Warning: pattern has some meta-characters interpreted by agrep!\n");
3109
else if (Pattern[i] == '\\') i++; /* extra */
3110
else if (Pattern[i] == '[') seenlsq = 1;
3111
else if ((Pattern[i] == '-') && !seenlsq) {
3112
for (j=M; j>=i; j--)
3113
Pattern[j+1] = Pattern[j]; /* right shift including '\0' */
3114
Pattern[i] = '\\'; /* escape the - */
3118
else if (Pattern[i] == ']') seenlsq = 0;
3121
if (M > pattern_len - 1) {
3122
fprintf(stderr, "%s: pattern '%s' does not fit in specified buffer\n", Progname, Pattern);
3126
if (pattern_buffer != Pattern) /* not from mem/file-agrep() */
3127
strncpy(pattern_buffer, Pattern, M+1); /* copy \0 */
3132
* User need not bother about initialfd.
3133
* Both functions return -1 on error, 0 if there was no pattern,
3134
* length (>=1) of pattern otherwise.
3138
memagrep_init(argc, argv, pattern_len, pattern_buffer)
3142
char *pattern_buffer;
3144
return (agrep_init(argc, argv, -1, pattern_len, pattern_buffer));
3148
fileagrep_init(argc, argv, pattern_len, pattern_buffer)
3152
char *pattern_buffer;
3154
return (agrep_init(argc, argv, 3, pattern_len, pattern_buffer));
3157
/* returns -1 on error, num of matches (>=0) otherwise */
3159
agrep_search(pattern_len, pattern_buffer, initialfd, input_len, input, output_len, output)
3161
CHAR *pattern_buffer;
3171
int pattern_has_changed = 1;
3173
if ((multifd == -1) && (multibuf == NULL) && (pattern_len < 1)) {
3174
fprintf(stderr, "%s: pattern length %d too small\n", Progname, pattern_len);
3178
if (pattern_len >= MAXPAT) {
3179
fprintf(stderr, "%s: pattern '%s' too long\n", Progname, pattern_buffer);
3184
/* courtesy: crd@hplb.hpl.hp.com */
3185
if (agrep_saved_pattern) {
3186
if (strcmp(agrep_saved_pattern, pattern_buffer)) {
3187
free(agrep_saved_pattern);
3188
agrep_saved_pattern = NULL;
3191
pattern_has_changed = 0;
3194
if (! agrep_saved_pattern) {
3195
agrep_saved_pattern = (CHAR *)malloc(pattern_len+1);
3196
memcpy(agrep_saved_pattern, pattern_buffer, pattern_len);
3197
agrep_saved_pattern[pattern_len] = '\0';
3200
if (!pattern_has_changed) {
3201
reinit_value_partial();
3205
if (pattern_buffer != Pattern) /* not from mem/file-agrep() */
3206
strncpy(Pattern, pattern_buffer, pattern_len+1); /* copy \0 */
3207
M = strlen(Pattern);
3210
if (output == NULL) {
3211
fprintf(stderr, "%s: invalid output descriptor\n", Progname);
3214
if (output_len <= 0) {
3215
agrep_finalfp = (FILE *)output;
3217
agrep_outbuffer = NULL;
3218
agrep_outpointer = 0;
3221
agrep_finalfp = NULL;
3222
agrep_outlen = output_len;
3223
agrep_outbuffer = (CHAR *)output;
3224
agrep_outpointer = 0;
3227
agrep_initialfd = initialfd;
3229
if (initialfd == -1) {
3230
agrep_inbuffer = (CHAR *)input;
3231
agrep_inlen = input_len;
3232
agrep_inpointer = 0;
3234
else if ((input_len > 0) && (input != NULL)) {
3235
/* Copy the set of filenames into Textfiles */
3236
if (copied_from_argv) {
3237
for (i=0; i<Numfiles; i++) free(Textfiles[i]);
3238
if (Textfiles != NULL) free(Textfiles);
3240
copied_from_argv = 0;
3241
Numfiles = 0; Textfiles = NULL;
3243
if (!(Textfiles = (CHAR **)malloc(input_len * sizeof(CHAR *) ))) {
3244
fprintf(stderr, "%s: malloc failure in %s:%d\n", Progname, __FILE__, __LINE__);
3246
errno = AGREP_ERROR;
3252
Textfiles = (CHAR **)input;
3255
while (input_len --)
3256
{ /* one or more filenames on command line -- put the valid filenames in a array of strings */
3258
if (!glimpse_call && ((filetype = check_file(*(char **)input)) == NOSUCHFILE) && !PRINTNONEXISTENTFILE) {
3259
fprintf(stderr,"%s: '%s' no such file or directory\n",Progname,*(char **)input);
3260
input = (void *)(((long)input) + sizeof(char *));
3262
else { /* file is ascii*/
3264
if (!(Textfiles[Numfiles] = (CHAR *)malloc((strlen(*(char **)input)+2)))) {
3265
fprintf(stderr, "%s: malloc failure in %s:%d\n", Progname, __FILE__, __LINE__);
3267
errno = AGREP_ERROR;
3272
strcpy(Textfiles[Numfiles++], *((char **)input));
3274
Textfiles[Numfiles++] = *((CHAR **)input);
3276
input = (void *)(((long)input) + sizeof(char *));
3278
} /* while (argc--) */
3279
if (Numfiles <= 0) return 0;
3280
/* If user specified wrong filenames, then we quit rather than taking input from stdin! */
3282
else if (Numfiles <= 0) execfd = 0;
3283
/* the old set of files (agrep_init) are used */
3286
printf("agrep_search: pat=%s buf=%s\n", Pattern, ((agrep_initialfd == -1) && (agrep_inlen < 20)) ? agrep_inbuffer : NULL);
3289
if (pattern_has_changed) {
3290
if (-1 == checksg(Pattern, D, 1)) return -1; /* check if the pattern is simple */
3291
strcpy(OldPattern, Pattern);
3293
if (SGREP == 0) { /* complex pattern search */
3294
if (-1 == preprocess(D_pattern, Pattern)) return -1;
3295
strcpy(old_D_pat, D_pattern);
3296
/* fprintf(stderr, "agrep_search: len=%d pat=%s\n", strlen(Pattern), Pattern); */
3297
if(!AParse && ((M = maskgen(Pattern, D)) == -1)) return -1;
3299
else { /* sgrep too can handle delimiters */
3301
/* D_pattern is "<PAT>; ", D_length is 1 + length of string PAT: see agrep.c/'d' */
3302
preprocess_delimiter(D_pattern+1, D_length - 1, D_pattern, &D_length);
3303
/* D_pattern is the exact stuff we want to match, D_length is its strlen */
3304
if ((tc_D_length = quick_tcompress(FREQ_FILE,HASH_FILE,D_pattern,D_length,tc_D_pattern,MaxDelimit*2,TC_EASYSEARCH)) <= 0) {
3305
strcpy(tc_D_pattern, D_pattern);
3306
tc_D_length = D_length;
3308
/* printf("sgrep's delim=%s,%d tc_delim=%s,%d\n", D_pattern, D_length, tc_D_pattern, tc_D_length); */
3310
M = strlen(OldPattern);
3314
if (AParse) { /* boolean converted to multi-pattern search */
3316
if (pattern_has_changed)
3317
prepf_ret= prepf(-1, multibuf, multilen);
3318
if (prepf_ret <= -1) {
3319
if (AComplexBoolean) destroy_tree(AParse);
3322
if (multibuf != NULL) free(multibuf); /* this was allocated for arbit booleans, not multipattern search */
3325
/* Cannot free multifd here since that is always allocated for multipattern search */
3329
if (Numfiles > 1) FNAME = ON;
3330
if (NOFILENAME) FNAME = 0;
3331
if (ALWAYSFILENAME) FNAME = ON; /* used by glimpse ONLY: 15/dec/93 */
3333
if (agrep_initialfd == -1) ret = exec(execfd, NULL);
3334
else if(RECURSIVE) ret = (recursive(Numfiles, Textfiles));
3335
else ret = (exec(execfd, Textfiles));
3340
* User need not bother about initialfd.
3341
* Both functions return -1 on error, 0 otherwise.
3345
memagrep_search(pattern_len, pattern_buffer, input_len, input_buffer, output_len, output)
3347
char *pattern_buffer;
3353
return(agrep_search(pattern_len, pattern_buffer, -1, input_len, input_buffer, output_len, output));
3357
fileagrep_search(pattern_len, pattern_buffer, file_num, file_buffer, output_len, output)
3359
char *pattern_buffer;
3365
return(agrep_search(pattern_len, pattern_buffer, 3, file_num, file_buffer, output_len, output));
3369
* The original agrep_run() routine was split into agrep_search and agrep_init
3370
* so that the interface with glimpse could be made cleaner: see glimpse.
3371
* Now, the user can specify an initial set of options, and use them in future
3372
* searches. If agrep_init does not find the pattern, options are still SET.
3373
* In fileagrep_search, the user can specify a NEW set of files to be searched
3374
* after the options are processed (this is used in glimpse).
3376
* Both functions return -1 on error, 0 otherwise.
3378
* The arguments are self explanatory. The pattern should be specified in
3379
* one of the argvs. Options too can be specified in one of the argvs -- it
3380
* is exactly as if the options are being given to agrep at run time.
3381
* The only restrictions are that the input_buffer should begin with a '\n'
3382
* and after its end, there must be valid memory to store a copy of the pattern.
3386
memagrep(argc, argv, input_len, input_buffer, output_len, output)
3396
if ((ret = memagrep_init(argc, argv, MAXPAT, Pattern)) < 0) return -1;
3397
else if ((ret == 0) && (multifd == -1) && (multibuf == NULL)) return -1;
3398
/* ^^^ because one need not specify the pattern on the cmd line if -f OR -m */
3399
return memagrep_search(ret, Pattern, input_len, input_buffer, output_len, output);
3403
fileagrep(argc, argv, output_len, output)
3411
if ((ret = fileagrep_init(argc, argv, MAXPAT, Pattern)) < 0) return -1;
3412
else if ((ret == 0) && (multifd == -1) && (multibuf == NULL)) return -1;
3413
/* ^^^ because one need not specify the pattern on the cmd line if -f OR -m */
3414
return fileagrep_search(ret, Pattern, 0, NULL, output_len, output);
3418
* RETURNS: total number of matched lines in all files that were searched.
3420
* The pattern(s) remain(s) constant irrespective of the number of files.
3421
* Hence, essentially, all the interface routines below have to be changed
3422
* so that they DONT do that preprocessing again and again for multiple
3423
* files. This bug was found while interfacing agrep with cast.
3425
* At present, sgrep() has been modified to have another parameter,
3426
* "samepattern" that tells it whether the pattern is the same as before.
3427
* Other funtions too should have such a parameter and should not repeat
3428
* preprocessing for all patterns. Since preprocessing for a pattern to
3429
* be searched in compressed files is siginificant, this bug was found.
3431
* - bgopal on 15/Nov/93.
3440
int ret = 0; /* no error */
3442
if ((Numfiles > 1) && (NOFILENAME == OFF)) FNAME = ON;
3443
if ((-1 == compat())) return -1; /* check compatibility between options */
3446
TCOMPRESSED = ON; /* there is a possibility that the data might be tuncompressible */
3447
if (!SetCurrentByteOffset) CurrentByteOffset = 0;
3448
if((fd == 0) && FILENAMEONLY) {
3449
fprintf(stderr, "%s: -l option is not compatible with standard input\n", Progname);
3451
errno = AGREP_ERROR;
3456
if(PAT_FILE || PAT_BUFFER) mgrep(fd, AParse);
3458
if(SGREP) ret = sgrep(OldPattern, strlen(OldPattern), fd, D, 0);
3459
else ret = bitap(old_D_pat, Pattern, fd, M, D);
3461
if (ret <= -1) return -1;
3462
if (COUNT /* && ret */) { /* dirty solution for glimpse's -b! */
3463
if(INVERSE && (PAT_FILE || PAT_BUFFER)) { /* inverse will never be set in glimpse */
3464
if (agrep_finalfp != NULL)
3465
fprintf(agrep_finalfp, "%d\n", total_line-(num_of_matched - prev_num_of_matched));
3470
sprintf(s, "%d\n", total_line-(num_of_matched - prev_num_of_matched));
3472
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
3473
(s[outindex] != '\0'); outindex++) {
3474
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
3476
if (s[outindex] != '\0') {
3480
agrep_outpointer += outindex;
3484
if (agrep_finalfp != NULL)
3485
fprintf(agrep_finalfp, "%d\n", (num_of_matched - prev_num_of_matched));
3490
sprintf(s, "%d\n", (num_of_matched - prev_num_of_matched));
3492
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
3493
(s[outindex] != '\0'); outindex++) {
3494
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
3496
if (s[outindex] != '\0') {
3500
agrep_outpointer += outindex;
3506
/* fd > 0 => Numfiles > 0 */
3508
for (i = 0; i < Numfiles; i++, close(fd)) {
3509
prev_num_of_matched = num_of_matched;
3510
if (!SetCurrentByteOffset) CurrentByteOffset = 0;
3511
if (!SetCurrentFileName) {
3512
if (PRINTFILENUMBER) sprintf(CurrentFileName, "%d", i);
3513
else strcpy(CurrentFileName, file_list[i]);
3515
if (!SetCurrentFileTime) {
3516
if (PRINTFILETIME) CurrentFileTime = aget_file_time(NULL, file_list[i]);
3519
if (!tuncompressible_filename(file_list[i], strlen(file_list[i]))) TCOMPRESSED = OFF;
3521
if ((fd = my_open(file_list[i], O_RDONLY)) < /*=*/ 0) {
3522
if (PRINTNONEXISTENTFILE) printf("%s\n", CurrentFileName);
3523
else if (!glimpse_call) fprintf(stderr, "%s: can't open file for reading: %s\n",Progname, file_list[i]);
3526
if(PAT_FILE || PAT_BUFFER) mgrep(fd, AParse);
3528
if(SGREP) ret = sgrep(OldPattern, strlen(OldPattern), fd, D, i);
3529
else ret = bitap(old_D_pat, Pattern, fd, M, D);
3535
if (num_of_matched - prev_num_of_matched > 0) {
3540
if (COUNT && !FILEOUT) {
3541
if( (INVERSE && (PAT_FILE || PAT_BUFFER)) && ((total_line - (num_of_matched - prev_num_of_matched)> 0) || !NOOUTPUTZERO) ) {
3542
if(FNAME && (NEW_FILE || !POST_FILTER)) {
3544
if (agrep_finalfp != NULL)
3545
fprintf(agrep_finalfp, "%s", CurrentFileName);
3548
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
3549
(CurrentFileName[outindex] != '\0'); outindex++) {
3550
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
3552
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
3557
agrep_outpointer += outindex;
3559
if (PRINTFILETIME) {
3560
char *s = aprint_file_time(CurrentFileTime);
3561
if (agrep_finalfp != NULL)
3562
fprintf(agrep_finalfp, "%s", s);
3565
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
3566
(s[outindex] != '\0'); outindex++) {
3567
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
3569
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
3574
agrep_outpointer += outindex;
3577
if (agrep_finalfp != NULL)
3578
fprintf(agrep_finalfp, ": %d\n", total_line - (num_of_matched - prev_num_of_matched));
3582
sprintf(s, ": %d\n", total_line - (num_of_matched - prev_num_of_matched));
3583
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
3584
(s[outindex] != '\0'); outindex++) {
3585
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
3587
if (s[outindex] != '\0') {
3592
agrep_outpointer += outindex;
3598
if (agrep_finalfp != NULL)
3599
fprintf(agrep_finalfp, "%d\n", total_line - (num_of_matched - prev_num_of_matched));
3604
sprintf(s, "%d\n", total_line - (num_of_matched - prev_num_of_matched));
3606
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
3607
(s[outindex] != '\0'); outindex++) {
3608
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
3610
if (s[outindex] != '\0') {
3615
agrep_outpointer += outindex;
3619
else if ( !(INVERSE && (PAT_FILE || PAT_BUFFER)) && (((num_of_matched - prev_num_of_matched) > 0) || !NOOUTPUTZERO) ) {
3620
/* inverse is always 0 in glimpse, so we always come here */
3621
if(FNAME && (NEW_FILE || !POST_FILTER)) {
3623
if (agrep_finalfp != NULL)
3624
fprintf(agrep_finalfp, "%s", CurrentFileName);
3627
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
3628
(CurrentFileName[outindex] != '\0'); outindex++) {
3629
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
3631
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
3636
agrep_outpointer += outindex;
3638
if (PRINTFILETIME) {
3639
char *s = aprint_file_time(CurrentFileTime);
3640
if (agrep_finalfp != NULL)
3641
fprintf(agrep_finalfp, "%s", s);
3644
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
3645
(s[outindex] != '\0'); outindex++) {
3646
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
3648
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
3653
agrep_outpointer += outindex;
3656
if (agrep_finalfp != NULL)
3657
fprintf(agrep_finalfp, ": %d\n", (num_of_matched - prev_num_of_matched));
3661
sprintf(s, ": %d\n", (num_of_matched - prev_num_of_matched));
3662
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
3663
(s[outindex] != '\0'); outindex++) {
3664
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
3666
if (s[outindex] != '\0') {
3671
agrep_outpointer += outindex;
3677
if (agrep_finalfp != NULL)
3678
fprintf(agrep_finalfp, "%d\n", (num_of_matched - prev_num_of_matched));
3683
sprintf(s, "%d\n", (num_of_matched - prev_num_of_matched));
3685
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
3686
(s[outindex] != '\0'); outindex++) {
3687
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
3689
if (s[outindex] != '\0') {
3694
agrep_outpointer += outindex;
3699
if(FILEOUT && (num_of_matched - prev_num_of_matched)) {
3700
if (-1 == file_out(file_list[i])) {
3706
if (glimpse_clientdied) {
3710
if (agrep_finalfp != NULL) fflush(agrep_finalfp);
3711
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
3712
((LIMITTOTALFILE > 0) && (LIMITTOTALFILE <= files_matched))) {
3716
} /* for i < Numfiles */
3718
if(NOMATCH && BESTMATCH) {
3719
if(WORDBOUND || WHOLELINE || INVERSE) {
3721
if(-1 == preprocess(D_pattern, Pattern)) return -1;
3722
strcpy(old_D_pat, D_pattern);
3723
if((M = maskgen(Pattern, D)) == -1) return -1;
3727
while(D<M && D<=MaxError && (num_of_matched - prev_num_of_matched == 0)) {
3728
for (i = 0; i < Numfiles; i++, close(fd)) {
3729
prev_num_of_matched = num_of_matched;
3730
CurrentByteOffset = 0;
3731
if (PRINTFILENUMBER) sprintf(CurrentFileName, "%d", i);
3732
else strcpy(CurrentFileName, file_list[i]);
3733
if (!SetCurrentFileTime) if (PRINTFILETIME) CurrentFileTime = aget_file_time(NULL, file_list[i]);
3735
if ((fd = my_open(Textfiles[i], O_RDONLY)) > 0) {
3736
if(PAT_FILE || PAT_BUFFER) mgrep(fd, AParse);
3738
if(SGREP) ret = sgrep(OldPattern,strlen(OldPattern),fd,D, i);
3739
else ret = bitap(old_D_pat,Pattern,fd,M,D);
3741
if (ret <= -1) return -1;
3743
/* else don't have to process PRINTNONEXISTENTFILE since must print only once */
3744
if (glimpse_clientdied) {
3748
if (agrep_finalfp != NULL) fflush(agrep_finalfp);
3749
if ((((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
3750
((LIMITTOTALFILE > 0) && (LIMITTOTALFILE <= files_matched))) &&
3751
(num_of_matched > prev_num_of_matched)) {
3755
} /* for i < Numfiles */
3759
if(num_of_matched - prev_num_of_matched > 0) {
3761
errno = D; /* #of errors if proper return */
3764
if(num_of_matched - prev_num_of_matched == 1) fprintf(stderr,"%s: 1 word matches ", Progname);
3765
else fprintf(stderr,"%s: %d words match ", Progname, num_of_matched - prev_num_of_matched);
3766
if(D==1) fprintf(stderr, "within 1 error");
3767
else fprintf(stderr, "within %d errors", D);
3771
if(NOPROMPT) fprintf(stderr, "\n");
3773
if(num_of_matched - prev_num_of_matched == 1) fprintf(stderr,"; search for it? (y/n)");
3774
else fprintf(stderr,"; search for them? (y/n)");
3776
if (!glimpse_isserver && (fgets(c, 4, stdin) == NULL)) goto CONT;
3777
if(c[0] != 'y') goto CONT;
3780
for (i = 0; i < Numfiles; i++, close(fd)) {
3781
prev_num_of_matched = num_of_matched;
3782
CurrentByteOffset = 0;
3783
if (PRINTFILENUMBER) sprintf(CurrentFileName, "%d", i);
3784
else strcpy(CurrentFileName, file_list[i]);
3785
if (!SetCurrentFileTime) if (PRINTFILETIME) CurrentFileTime = aget_file_time(NULL, file_list[i]);
3787
if ((fd = my_open(Textfiles[i], O_RDONLY)) > 0) {
3788
if(PAT_FILE || PAT_BUFFER) mgrep(fd, AParse);
3790
if(SGREP) ret = sgrep(OldPattern,strlen(OldPattern),fd,D, i);
3791
else ret = bitap(old_D_pat,Pattern,fd,M,D);
3798
/* else don't have to process PRINTNONEXISTENTFILE since must print only once */
3799
if (glimpse_clientdied) {
3803
if (agrep_finalfp != NULL) fflush(agrep_finalfp);
3804
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
3805
((LIMITTOTALFILE > 0) && (LIMITTOTALFILE <= files_matched))) {
3809
} /* for i < Numfiles */
3816
if (agrep_finalfp != NULL) fprintf(agrep_finalfp, "\n");
3817
else if (agrep_outpointer >= agrep_outlen) {
3821
else agrep_outbuffer[agrep_outpointer++] = '\n';
3824
if(num_of_matched - prev_num_of_matched > 0) NOMATCH = OFF;
3825
/* if(NOMATCH) return(0); */
3826
/*printf("exec=%d\n", num_of_matched);*/
3827
return(num_of_matched);
3829
} /* end of exec() */
3832
/* Just output the contents of the file fname onto the std output */
3842
len = strlen(fname);
3843
if (agrep_finalfp != NULL) {
3844
fputc('\n', agrep_finalfp);
3845
for(i=0; i< len; i++) fputc(':', agrep_finalfp);
3846
fputc('\n', agrep_finalfp);
3847
fprintf(agrep_finalfp, "%s\n", fname);
3848
for(i=0; i< len; i++) fputc(':', agrep_finalfp);
3849
fputc('\n', agrep_finalfp);
3850
fflush(agrep_finalfp);
3853
if (1+len+1+len+1+len+1+agrep_outpointer >= agrep_outlen) {
3857
agrep_outbuffer[agrep_outpointer++] = '\n';
3858
for (i=0; i<len; i++) agrep_outbuffer[agrep_outpointer++] = ':';
3859
agrep_outbuffer[agrep_outpointer++] = '\n';
3860
for (i=0; i<len; i++) agrep_outbuffer[agrep_outpointer++] = fname[i];
3861
agrep_outbuffer[agrep_outpointer++] = '\n';
3862
for (i=0; i<len; i++) agrep_outbuffer[agrep_outpointer++] = ':';
3863
agrep_outbuffer[agrep_outpointer++] = '\n';
3866
fd = my_open(fname, O_RDONLY);
3867
if (agrep_finalfp != NULL) {
3868
while((num_read = fill_buf(fd, buf, SIZE)) > 0)
3869
write(1, buf, num_read);
3870
if (glimpse_clientdied) {
3876
if ((num_read = fill_buf(fd, agrep_outbuffer + agrep_outpointer, agrep_outlen - agrep_outpointer)) > 0)
3877
agrep_outpointer += num_read;
3884
output(buffer, i1, i2, j)
3885
register CHAR *buffer;
3889
register CHAR *bp, *outend;
3890
if(i1 > i2) return 0;
3893
if(SILENT) return 0;
3894
if(OUTTAIL || (!DELIMITER && (D_length == 1) && (D_pattern[0] == '\n')) ) {
3895
if (j>1) i1 = i1 + D_length;
3898
if(DELIMITER) j = j+1;
3900
if (buffer[i1] == '\n') {
3907
fprintf(stderr, "WARNING! some lines have been truncated in output record #%d\n", num_of_matched-1);
3910
/* Why do we have to do this? */
3911
while ((buffer[i1] == '\n') && (i1 <= i2)) {
3912
if (agrep_finalfp != NULL) fprintf(agrep_finalfp, "\n");
3914
if (agrep_outpointer < agrep_outlen)
3915
agrep_outbuffer[agrep_outpointer ++] = '\n';
3924
if(FNAME && (NEW_FILE || !POST_FILTER)) {
3925
char nextchar = (POST_FILTER == ON)?'\n':' ';
3926
char *prevstring = (POST_FILTER == ON)?"\n":"";
3928
if (agrep_finalfp != NULL)
3929
fprintf(agrep_finalfp, "%s%s", prevstring, CurrentFileName);
3932
if (prevstring[0] != '\0') {
3933
if(agrep_outpointer + 1 >= agrep_outlen) {
3937
else agrep_outbuffer[agrep_outpointer ++] = prevstring[0];
3939
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
3940
(CurrentFileName[outindex] != '\0'); outindex++) {
3941
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
3943
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
3947
agrep_outpointer += outindex;
3949
if (PRINTFILETIME) {
3950
char *s = aprint_file_time(CurrentFileTime);
3951
if (agrep_finalfp != NULL)
3952
fprintf(agrep_finalfp, "%s", s);
3955
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
3956
(s[outindex] != '\0'); outindex++) {
3957
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
3959
if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
3963
agrep_outpointer += outindex;
3966
if (agrep_finalfp != NULL)
3967
fprintf(agrep_finalfp, ":%c", nextchar);
3969
if (agrep_outpointer+2>= agrep_outlen) {
3974
agrep_outbuffer[agrep_outpointer++] = ':';
3975
agrep_outbuffer[agrep_outpointer++] = nextchar;
3983
if (agrep_finalfp != NULL)
3984
fprintf(agrep_finalfp, "%d: ", j-1);
3989
sprintf(s, "%d: ", j-1);
3990
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
3991
(s[outindex] != '\0'); outindex++) {
3992
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
3994
if (s[outindex] != '\0') {
3998
agrep_outpointer += outindex;
4003
if (agrep_finalfp != NULL)
4004
fprintf(agrep_finalfp, "%d= ", CurrentByteOffset-1);
4008
sprintf(s, "%d= ", CurrentByteOffset-1);
4009
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
4010
(s[outindex] != '\0'); outindex++) {
4011
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
4013
if (s[outindex] != '\0') {
4017
agrep_outpointer += outindex;
4023
outend = buffer + i2;
4026
if (agrep_finalfp != NULL)
4027
fprintf(agrep_finalfp, "@%d{%d}\n", CurrentByteOffset - (i2-i1), i2-i1);
4031
sprintf(s, "@%d{%d}\n", CurrentByteOffset - (i2-i1), i2-i1);
4032
for (outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
4033
(s[outindex] != '\0'); outindex ++) {
4034
agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
4036
if (s[outindex] != '\0') {
4040
agrep_outpointer += outindex;
4046
if (agrep_finalfp != NULL)
4047
while(bp <= outend) fputc(*bp++, agrep_finalfp);
4049
if (outend - bp + 1 + agrep_outpointer >= agrep_outlen) {
4053
while(bp <= outend) agrep_outbuffer[agrep_outpointer ++] = *bp++;
4057
if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);
4058
else agrep_outbuffer[agrep_outpointer ++] = '\n';
4067
if (glimpse_call) return -1;
4068
fprintf(stderr, "usage: %s [-@#abcdehiklnoprstvwxyBDGIMSV] [-f patternfile] [-H dir] pattern [files]\n", Progname);
4069
fprintf(stderr, "\n");
4070
fprintf(stderr, "summary of frequently used options:\n");
4071
fprintf(stderr, "(For a more detailed listing see 'man agrep'.)\n");
4072
fprintf(stderr, "-#: find matches with at most # errors\n");
4073
fprintf(stderr, "-c: output the number of matched records\n");
4074
fprintf(stderr, "-d: define record delimiter\n");
4075
fprintf(stderr, "-h: do not output file names\n");
4076
fprintf(stderr, "-i: case-insensitive search, e.g., 'a' = 'A'\n");
4077
fprintf(stderr, "-l: output the names of files that contain a match\n");
4078
fprintf(stderr, "-n: output record prefixed by record number\n");
4079
fprintf(stderr, "-v: output those records that have no matches\n");
4080
fprintf(stderr, "-w: pattern has to match as a word, e.g., 'win' will not match 'wind'\n");
4081
fprintf(stderr, "-B: best match mode. find the closest matches to the pattern\n");
4082
fprintf(stderr, "-G: output the files that contain a match\n");
4083
fprintf(stderr, "-H 'dir': the cast-dictionary is located in directory 'dir'\n");
4084
fprintf(stderr, "\n");
4087
errno = AGREP_ERROR;