~ubuntu-branches/ubuntu/trusty/agrep/trusty

« back to all changes in this revision

Viewing changes to agrep.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2006-07-05 13:29:00 UTC
  • mfrom: (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20060705132900-j24rz8zdk4xqdmoz
Tags: 4.17-3
* New email address.
* Some packaging style changes.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*
 
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.
 
5
 */
 
6
#include <sys/stat.h>
 
7
#include "agrep.h"
 
8
#include "checkfile.h"
 
9
#include <errno.h>
 
10
 
 
11
#define PRINT(s)
 
12
 
 
13
extern char **environ;
 
14
extern int errno;
 
15
int pattern_index;      /* index in argv where the pattern is */
 
16
 
 
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? */
 
21
CHAR *agrep_inbuffer;
 
22
int  agrep_inlen;
 
23
int  agrep_inpointer;
 
24
 
 
25
FILE *agrep_finalfp;    /* Where does output go to? File/Memory? */
 
26
CHAR *agrep_outbuffer;
 
27
int  agrep_outlen;
 
28
int  agrep_outpointer;
 
29
 
 
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;
 
33
#if     DOTCOMPRESSED
 
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! */
 
38
 
 
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];
 
42
unsigned Bit[WORD+1];
 
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; 
 
52
int  NOOUTPUTZERO;
 
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;
 
57
int  BYTECOUNT;
 
58
int  PRINTOFFSET;
 
59
int  PRINTRECORD;
 
60
int  PRINTNONEXISTENTFILE;
 
61
int  FILEOUT;
 
62
int  DNA;
 
63
int  APPROX;
 
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 */
 
67
int  CONSTANT;
 
68
int  RECURSIVE;
 
69
int  total_line; /* used in mgrep */
 
70
int  D;
 
71
int  M;
 
72
int  TCOMPRESSED;
 
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) */
 
82
 
 
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 */
 
91
 
 
92
#if     MEASURE_TIMES
 
93
/* timing variables */
 
94
int OUTFILTER_ms;
 
95
int FILTERALGO_ms;
 
96
int INFILTER_ms;
 
97
#endif  /*MEASURE_TIMES*/
 
98
 
 
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 */
 
117
 
 
118
int  NOFILENAME,  /* Boolean flag, set for -h option */
 
119
     FILENAMEONLY;/* Boolean flag, set for -l option */
 
120
extern int init();
 
121
int table[WORD][WORD];
 
122
CHAR *agrep_saved_pattern = NULL;       /* to prevent multiple prepfs for each boolean search: crd@hplb.hpl.hp.com */
 
123
 
 
124
long
 
125
aget_file_time(stbuf, name)
 
126
        struct stat *stbuf;
 
127
        char    *name;
 
128
{
 
129
        long    ret = 0;
 
130
        struct stat mystbuf;
 
131
        if (stbuf != NULL) ret = stbuf->st_mtime;
 
132
        else {
 
133
                if (my_stat(name, &mystbuf) == -1) ret = 0;
 
134
                else ret = mystbuf.st_mtime;
 
135
        }
 
136
        return ret;
 
137
}
 
138
 
 
139
char *
 
140
aprint_file_time(thetime)
 
141
        time_t  thetime;
 
142
{
 
143
#if     0
 
144
        char    s[256], s1[16], s2[16], s3[16], s4[16], s5[16];
 
145
        static char buffer[256];
 
146
 
 
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 */
 
151
#else
 
152
        static char buffer[256];
 
153
        buffer[0] = ':';
 
154
        buffer[1] = ' ';
 
155
        strftime(&buffer[2], 256, "%h %e %Y", gmtime(&thetime));
 
156
#endif
 
157
        return &buffer[0];
 
158
}
 
159
 
 
160
/* Called when multipattern search and pattern has not changed */
 
161
void
 
162
reinit_value_partial()
 
163
{
 
164
        num_of_matched = prev_num_of_matched = 0;
 
165
        errno = 0;
 
166
        FIRST_IN_RE = ON;
 
167
}
 
168
 
 
169
/* This must be called before every agrep_search to reset agrep globals */
 
170
void
 
171
reinit_value()
 
172
{
 
173
        int i, j;
 
174
 
 
175
        /* Added on 7th Oct 194 */
 
176
        if (AParse) {
 
177
                if (AComplexBoolean) destroy_tree(AParse);
 
178
                AComplexBoolean = 0;
 
179
                AParse = 0;
 
180
                PAT_BUFFER = 0;
 
181
                if (multibuf != NULL) free(multibuf);   /* this was allocated for arbit booleans, not multipattern search */
 
182
                multibuf = NULL;
 
183
                multilen = 0;
 
184
                /* Cannot free multifd here since that is always allocated for multipattern search */
 
185
        }
 
186
        for (i=0; i<anum_terminals; i++) {
 
187
                free(aterminals[i].data.leaf.value);
 
188
                memset(&aterminals[i], '\0', sizeof(ParseTree));
 
189
        }
 
190
        anum_terminals = 0;
 
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);
 
193
 
 
194
        Bit[WORD] = 1;
 
195
        for (i = WORD - 1; i > 0  ; i--)  Bit[i] = Bit[i+1] << 1;
 
196
        for (i=0; i< MAXSYM; i++) Mask[i] = 0;
 
197
 
 
198
        /* bg: new things added on Mar 13 94 */
 
199
        Init1 = 0;
 
200
        NO_ERR_MASK = 0;
 
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++)
 
207
                        table[i][j] = 0;
 
208
 
 
209
        strcpy(D_pattern, original_D_pattern);
 
210
        D_length = original_D_length;
 
211
        strcpy(old_D_pat, original_old_D_pat);
 
212
 
 
213
        /* Changed on Dec 26th: bg */
 
214
        FASTREGEX = REGEX = 0;
 
215
        HEAD = TAIL = ON;       /* were off initially */
 
216
        RE_ERR = 0;
 
217
        AND = 0;
 
218
        M = 0;
 
219
        pos_cnt = 0;    /* added 31 Jan 95 */
 
220
 
 
221
        reinit_value_partial();
 
222
}
 
223
 
 
224
/* This must be called before every agrep_init to reset agrep options */
 
225
void
 
226
initial_value()
 
227
{
 
228
        SetCurrentFileName = 0; /* 16/9/94 */
 
229
        SetCurrentFileTime = 0;
 
230
        SetCurrentByteOffset = 0;       /* 23/9/94 */
 
231
 
 
232
        /* courtesy: crd@hplb.hpl.hp.com */
 
233
        if (agrep_saved_pattern) {
 
234
                free(agrep_saved_pattern);
 
235
                agrep_saved_pattern= NULL;
 
236
        }
 
237
        /* bg: new stuff on 17/Feb/94 */
 
238
        if (multifd != -1) close(multifd);
 
239
        multifd = -1;
 
240
        if (multibuf != NULL) free(multibuf);
 
241
        multibuf = NULL;
 
242
        multilen = 0;
 
243
        if (pat_spool != NULL) free(pat_spool);
 
244
        pat_spool = NULL;
 
245
#if     DOTCOMPRESSED
 
246
        if (tc_pat_spool != NULL) free(tc_pat_spool);
 
247
        tc_pat_spool = NULL;
 
248
#endif  /* DOTCOMPRESSED */
 
249
        LIMITOUTPUT = 0;        /* means infinity = current semantics */
 
250
        LIMITPERFILE = 0;       /* means infinity = current semantics */
 
251
        LIMITTOTALFILE = 0;     /* means infinity = current semantics */
 
252
        EASYSEARCH = 1;
 
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;
 
256
 
 
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';
 
263
 
 
264
        strcpy(tc_D_pattern, "\n");
 
265
        tc_D_length = 1;
 
266
 
 
267
        /* the functions agrep_init and agrep_search take care of Textfiles and Numfiles */
 
268
        agrep_inpointer = 0;
 
269
        agrep_outpointer = 0;
 
270
        agrep_outlen = 0;
 
271
#if     MEASURE_TIMES
 
272
        OUTFILTER_ms = FILTERALGO_ms = INFILTER_ms = 0;
 
273
#endif  /*MEASURE_TIMES*/
 
274
 
 
275
        MULTI_OUTPUT = 0;
 
276
        PRINTPATTERN = 0;
 
277
        PRINTFILENUMBER = 0;
 
278
        PRINTFILETIME = 0;
 
279
        JUMP = FNAME = BESTMATCH = NOPROMPT = NOUPPER = 0;
 
280
        RECURSIVE = 0;
 
281
        COUNT = LINENUM = WHOLELINE = SGREP = 0;
 
282
        NOOUTPUTZERO = 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;
 
289
 
 
290
        COMP_DIR[0] = '\0';
 
291
        FREQ_FILE[0] = '\0';
 
292
        HASH_FILE[0] = '\0';
 
293
        STRING_FILE[0] = '\0';
 
294
        BYTECOUNT = OFF;
 
295
        PRINTOFFSET = OFF;
 
296
        PRINTRECORD = ON;
 
297
        PRINTNONEXISTENTFILE = OFF;
 
298
 
 
299
        glimpse_clientdied = 0; /* added 15th Feb 95 */
 
300
 
 
301
        /* Pattern, OldPattern, execfd, Numfiles are set in agrep_init: so no need to initialize */
 
302
        reinit_value();
 
303
}
 
304
 
 
305
void
 
306
compute_next(M, Next, Next1)
 
307
int M; 
 
308
unsigned *Next, *Next1;
 
309
{
 
310
        int i, j=0, n,  k, temp;
 
311
        int mid, pp;
 
312
        int MM, base;
 
313
        unsigned V[WORD];
 
314
 
 
315
        base = WORD - M;
 
316
        temp = Bit[base]; 
 
317
        Bit[base] = 0;
 
318
        for (i=0; i<WORD; i++) V[i] = 0;
 
319
        for (i=1; i<M; i++)
 
320
        {  
 
321
                j=0;
 
322
                while (table[i][j] > 0 && j < 10) {
 
323
                        V[i] = V[i] | Bit[base + table[i][j++]];
 
324
                }
 
325
        }
 
326
        Bit[base]=temp;
 
327
        if(M <= SHORTREG)
 
328
        {
 
329
                k = exponen(M);
 
330
                pp = 2*k;
 
331
                for(i=k; i<pp ; i++)
 
332
                {   
 
333
                        n = i;
 
334
                        Next[i]= (k>>1);
 
335
                        for(j=M; j>=1; j--)
 
336
                        {
 
337
                                if(n & Bit[WORD]) Next[i] = Next[i] | V[j];
 
338
                                n = (n>>1);
 
339
                        }
 
340
                }      
 
341
                return;
 
342
        }
 
343
        if(M > MAXREG) fprintf(stderr, "%s: regular expression too long\n", Progname);
 
344
        MM = M;
 
345
        if(M & 1) M=M+1;
 
346
        k = exponen(M/2);
 
347
        pp = 2*k;
 
348
        mid = MM/2;
 
349
        for(i=k; i<pp ; i++)
 
350
        {     
 
351
                n = i;
 
352
                Next[i]= (Bit[base]>>1);
 
353
                for(j=MM; j>mid ; j--)
 
354
                {
 
355
                        if(n & Bit[WORD]) Next[i] = Next[i] | V[j-mid];
 
356
                        n = (n>>1);
 
357
                }
 
358
                n=i-k;
 
359
                Next1[i-k] = 0;
 
360
                for(j = 0; j<mid; j++)
 
361
                {
 
362
                        if(n & Bit[WORD]) Next1[i-k] = Next1[i-k] | V[MM-j];
 
363
                        n = (n>>1);
 
364
                }
 
365
        }      
 
366
        return;
 
367
}
 
368
 
 
369
int
 
370
exponen(m)
 
371
int m;
 
372
 
373
        int i, ex;
 
374
        ex= 1;
 
375
        for (i=0; i<m; i++) ex <<= 1;   /* was ex *= 2 */
 
376
        return(ex);
 
377
}
 
378
 
 
379
int
 
380
re1(Text, M, D)
 
381
int Text, M, D;
 
382
{
 
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];
 
389
        CHAR *buffer;
 
390
        int FIRST_LOOP = 1;
 
391
 
 
392
        r_NO_ERR = NO_ERR_MASK;
 
393
        if(M > 30) {
 
394
                fprintf(stderr, "%s: regular expression too long\n", Progname);
 
395
                if (!EXITONERROR){
 
396
                        errno = AGREP_ERROR;
 
397
                        return -1;
 
398
                }
 
399
                else exit(2);
 
400
        }
 
401
        base = WORD - M;
 
402
        hh = M/2;
 
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 */
 
406
        FIRST_IN_RE = 0;
 
407
        Newline = '\n';
 
408
        Init[0] = Bit[base];
 
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];
 
411
        Init1 = Init[0] | 1; 
 
412
        Init0 = Init[0];
 
413
        r2 = r3 = Init[0];
 
414
        for(k=0; k<= D; k++) { 
 
415
                A[k] = B[k] = Init[k]; 
 
416
        }
 
417
 
 
418
        if ( D == 0 )
 
419
        {
 
420
#if     AGREP_POINTER
 
421
            if (Text != -1)
 
422
            {
 
423
#endif  /*AGREP_POINTER*/
 
424
                alloc_buf(Text, &buffer, BlockSize+Maxline+1);
 
425
                while ((num_read = fill_buf(Text, buffer + Maxline, BlockSize)) > 0)
 
426
                {
 
427
                        i=Maxline; 
 
428
                        end = num_read + Maxline;
 
429
#if 0
 
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';
 
433
#endif /* 0 */
 
434
                        if(FIRST_LOOP) {         /* if first time in the loop add a newline */
 
435
                                buffer[i-1] = '\n';  /* in front the  text.  */
 
436
                                i--;
 
437
                                CurrentByteOffset --;
 
438
                                FIRST_LOOP = 0;
 
439
                        }
 
440
 
 
441
                        /* RE1_PROCESS_WHEN_DZERO: the while-loop below */
 
442
                        while ( i < end )
 
443
                        {
 
444
                                c = buffer[i++];
 
445
                                CurrentByteOffset ++;
 
446
                                CMask = Mask[c];
 
447
                                if(c != Newline)
 
448
                                {
 
449
                                        if(CMask != 0) {
 
450
                                                r1 = Init1 & r3;
 
451
                                                r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
 
452
                                        }
 
453
                                        else  {
 
454
                                                r2 = r3 & Init1;
 
455
                                        }
 
456
                                }
 
457
                                else {
 
458
                                        j++;
 
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)) {
 
466
                                                        num_of_matched++;
 
467
 
 
468
                                                        if (agrep_finalfp != NULL)
 
469
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
470
                                                        else {
 
471
                                                                int outindex;
 
472
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
473
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
474
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
475
                                                                }
 
476
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
477
                                                                        OUTPUT_OVERFLOW;
 
478
                                                                        free_buf(Text, buffer);
 
479
                                                                        return -1;
 
480
                                                                }
 
481
                                                                agrep_outpointer += outindex;
 
482
                                                        }
 
483
                                                        if (PRINTFILETIME) {
 
484
                                                                char *s = aprint_file_time(CurrentFileTime);
 
485
                                                                if (agrep_finalfp != NULL)
 
486
                                                                        fprintf(agrep_finalfp, "%s", s);
 
487
                                                                else {
 
488
                                                                        int outindex;
 
489
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
490
                                                                                        (s[outindex] != '\0'); outindex++) {
 
491
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
492
                                                                        }
 
493
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
494
                                                                                OUTPUT_OVERFLOW;
 
495
                                                                                free_buf(Text, buffer);
 
496
                                                                                return -1;
 
497
                                                                        }
 
498
                                                                        agrep_outpointer += outindex;
 
499
                                                                }
 
500
                                                        }
 
501
                                                        if (agrep_finalfp != NULL)
 
502
                                                                fprintf(agrep_finalfp, "\n");
 
503
                                                        else {
 
504
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
505
                                                                        OUTPUT_OVERFLOW;
 
506
                                                                        free_buf(Text, buffer);
 
507
                                                                        return -1;
 
508
                                                                }
 
509
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
510
                                                        }
 
511
 
 
512
                                                        free_buf(Text, buffer);
 
513
                                                        NEW_FILE = OFF;
 
514
                                                        return 0;
 
515
                                                }
 
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);
 
520
                                                        return 0;       /* done */
 
521
                                                }
 
522
                                        }
 
523
                                        r3 = Init0;
 
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;
 
528
                                }
 
529
                                c = buffer[i++];
 
530
                                CurrentByteOffset ++;
 
531
                                CMask = Mask[c];
 
532
                                if(c != Newline)
 
533
                                {
 
534
                                        if(CMask != 0) {
 
535
                                                r1 = Init1 & r2;
 
536
                                                r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
 
537
                                        }
 
538
                                        else   r3 = r2 & Init1;
 
539
                                } /* if(NOT Newline) */
 
540
                                else {
 
541
                                        j++;
 
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;
 
547
                                        /* epsilon move */
 
548
                                        if(( r3 & 1 ) ^ INVERSE) {
 
549
                                                if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
 
550
                                                        num_of_matched++;
 
551
 
 
552
                                                        if (agrep_finalfp != NULL)
 
553
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
554
                                                        else {
 
555
                                                                int outindex;
 
556
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
557
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
558
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
559
                                                                }
 
560
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
561
                                                                        OUTPUT_OVERFLOW;
 
562
                                                                        free_buf(Text, buffer);
 
563
                                                                        return -1;
 
564
                                                                }
 
565
                                                                agrep_outpointer += outindex;
 
566
                                                        }
 
567
                                                        if (PRINTFILETIME) {
 
568
                                                                char *s = aprint_file_time(CurrentFileTime);
 
569
                                                                if (agrep_finalfp != NULL)
 
570
                                                                        fprintf(agrep_finalfp, "%s", s);
 
571
                                                                else {
 
572
                                                                        int outindex;
 
573
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
574
                                                                                        (s[outindex] != '\0'); outindex++) {
 
575
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
576
                                                                        }
 
577
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
578
                                                                                OUTPUT_OVERFLOW;
 
579
                                                                                free_buf(Text, buffer);
 
580
                                                                                return -1;
 
581
                                                                        }
 
582
                                                                        agrep_outpointer += outindex;
 
583
                                                                }
 
584
                                                        }
 
585
                                                        if (agrep_finalfp != NULL)
 
586
                                                                fprintf(agrep_finalfp, "\n");
 
587
                                                        else {
 
588
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
589
                                                                        OUTPUT_OVERFLOW;
 
590
                                                                        free_buf(Text, buffer);
 
591
                                                                        return -1;
 
592
                                                                }
 
593
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
594
                                                        }
 
595
 
 
596
                                                        free_buf(Text, buffer);
 
597
                                                        NEW_FILE = OFF;
 
598
                                                        return 0;
 
599
                                                }
 
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);
 
604
                                                        return 0;       /* done */
 
605
                                                }
 
606
                                        }
 
607
                                        r2 = Init0;
 
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;
 
612
                                }
 
613
                        } /* while i < end ... */
 
614
 
 
615
                        strncpy(buffer, buffer+num_read, Maxline);
 
616
                } /* end while fill_buf()... */
 
617
 
 
618
                free_buf(Text, buffer);
 
619
                return 0;
 
620
#if     AGREP_POINTER
 
621
            }
 
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;
 
625
                end = num_read;
 
626
                /* buffer[end-1] = '\n';*/ /* at end of the text. */
 
627
                /* buffer[0] = '\n';*/  /* in front of the  text. */
 
628
                i = 0;
 
629
 
 
630
                        /* An exact copy of the above RE1_PROCESS_WHEN_DZERO: the while-loop below */
 
631
                        while ( i < end )
 
632
                        {
 
633
                                c = buffer[i++];
 
634
                                CurrentByteOffset ++;
 
635
                                CMask = Mask[c];
 
636
                                if(c != Newline)
 
637
                                {
 
638
                                        if(CMask != 0) {
 
639
                                                r1 = Init1 & r3;
 
640
                                                r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
 
641
                                        }
 
642
                                        else  {
 
643
                                                r2 = r3 & Init1;
 
644
                                        }
 
645
                                }
 
646
                                else {
 
647
                                        j++;
 
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)) {
 
655
                                                        num_of_matched++;
 
656
 
 
657
                                                        if (agrep_finalfp != NULL)
 
658
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
659
                                                        else {
 
660
                                                                int outindex;
 
661
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
662
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
663
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
664
                                                                }
 
665
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
666
                                                                        OUTPUT_OVERFLOW;
 
667
                                                                        free_buf(Text, buffer);
 
668
                                                                        return -1;
 
669
                                                                }
 
670
                                                                agrep_outpointer += outindex;
 
671
                                                        }
 
672
                                                        if (PRINTFILETIME) {
 
673
                                                                char *s = aprint_file_time(CurrentFileTime);
 
674
                                                                if (agrep_finalfp != NULL)
 
675
                                                                        fprintf(agrep_finalfp, "%s", s);
 
676
                                                                else {
 
677
                                                                        int outindex;
 
678
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
679
                                                                                        (s[outindex] != '\0'); outindex++) {
 
680
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
681
                                                                        }
 
682
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
683
                                                                                OUTPUT_OVERFLOW;
 
684
                                                                                free_buf(Text, buffer);
 
685
                                                                                return -1;
 
686
                                                                        }
 
687
                                                                        agrep_outpointer += outindex;
 
688
                                                                }
 
689
                                                        }
 
690
                                                        if (agrep_finalfp != NULL)
 
691
                                                                fprintf(agrep_finalfp, "\n");
 
692
                                                        else {
 
693
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
694
                                                                        OUTPUT_OVERFLOW;
 
695
                                                                        free_buf(Text, buffer);
 
696
                                                                        return -1;
 
697
                                                                }
 
698
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
699
                                                        }
 
700
 
 
701
                                                        free_buf(Text, buffer);
 
702
                                                        NEW_FILE = OFF;
 
703
                                                        return 0;
 
704
                                                }
 
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);
 
709
                                                        return 0;       /* done */
 
710
                                                }
 
711
                                        }
 
712
                                        r3 = Init0;
 
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;
 
717
                                }
 
718
                                c = buffer[i++];
 
719
                                CurrentByteOffset ++;
 
720
                                CMask = Mask[c];
 
721
                                if(c != Newline)
 
722
                                {
 
723
                                        if(CMask != 0) {
 
724
                                                r1 = Init1 & r2;
 
725
                                                r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
 
726
                                        }
 
727
                                        else   r3 = r2 & Init1;
 
728
                                } /* if(NOT Newline) */
 
729
                                else {
 
730
                                        j++;
 
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;
 
736
                                        /* epsilon move */
 
737
                                        if(( r3 & 1 ) ^ INVERSE) {
 
738
                                                if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
 
739
                                                        num_of_matched++;
 
740
 
 
741
                                                        if (agrep_finalfp != NULL)
 
742
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
743
                                                        else {
 
744
                                                                int outindex;
 
745
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
746
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
747
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
748
                                                                }
 
749
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
750
                                                                        OUTPUT_OVERFLOW;
 
751
                                                                        free_buf(Text, buffer);
 
752
                                                                        return -1;
 
753
                                                                }
 
754
                                                                agrep_outpointer += outindex;
 
755
                                                        }
 
756
                                                        if (PRINTFILETIME) {
 
757
                                                                char *s = aprint_file_time(CurrentFileTime);
 
758
                                                                if (agrep_finalfp != NULL)
 
759
                                                                        fprintf(agrep_finalfp, "%s", s);
 
760
                                                                else {
 
761
                                                                        int outindex;
 
762
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
763
                                                                                        (s[outindex] != '\0'); outindex++) {
 
764
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
765
                                                                        }
 
766
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
767
                                                                                OUTPUT_OVERFLOW;
 
768
                                                                                free_buf(Text, buffer);
 
769
                                                                                return -1;
 
770
                                                                        }
 
771
                                                                        agrep_outpointer += outindex;
 
772
                                                                }
 
773
                                                        }
 
774
                                                        if (agrep_finalfp != NULL)
 
775
                                                                fprintf(agrep_finalfp, "\n");
 
776
                                                        else {
 
777
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
778
                                                                        OUTPUT_OVERFLOW;
 
779
                                                                        free_buf(Text, buffer);
 
780
                                                                        return -1;
 
781
                                                                }
 
782
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
783
                                                        }
 
784
 
 
785
                                                        free_buf(Text, buffer);
 
786
                                                        NEW_FILE = OFF;
 
787
                                                        return 0;
 
788
                                                }
 
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);
 
793
                                                        return 0;       /* done */
 
794
                                                }
 
795
                                        }
 
796
                                        r2 = Init0;
 
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;
 
801
                                }
 
802
                        } /* while i < end ... */
 
803
 
 
804
                return 0;
 
805
            }
 
806
#endif  /*AGREP_POINTER*/
 
807
        } /*  end if (D == 0) */
 
808
 
 
809
#if     AGREP_POINTER
 
810
        if (Text != -1)
 
811
        {
 
812
#endif  /*AGREP_POINTER*/
 
813
                while ((num_read = fill_buf(Text, buffer + Maxline, BlockSize)) > 0)
 
814
                {
 
815
                        i=Maxline; 
 
816
                        end = Maxline + num_read;
 
817
#if 0
 
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';
 
821
#endif /* 0 */
 
822
                        if(FIRST_TIME) {         /* if first time in the loop add a newline */
 
823
                                buffer[i-1] = '\n';  /* in front the  text.  */
 
824
                                i--;
 
825
                                CurrentByteOffset --;
 
826
                                FIRST_TIME = 0;
 
827
                        }
 
828
 
 
829
                        /* RE1_PROCESS_WHEN_DNOTZERO: the while loop below */
 
830
                        while (i < end )
 
831
                        {
 
832
                                c = buffer[i];
 
833
                                CMask = Mask[c];
 
834
                                if(c !=  Newline)
 
835
                                {
 
836
                                        if(CMask != 0) {  
 
837
                                                r2 = B[0];
 
838
                                                r1 = Init1 & r2;
 
839
                                                A[0] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
 
840
                                                r3 = B[1];
 
841
                                                r1 = Init1 & r3;
 
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;
 
845
                                                r2 = B[2];
 
846
                                                r1 = Init1 & r2;
 
847
                                                r0 = r3 | A[1];
 
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;
 
850
                                                r3 = B[3];
 
851
                                                r1 = Init1 & r3;
 
852
                                                r0 = r2 | A[2];
 
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;
 
855
                                                r2 = B[4];
 
856
                                                r1 = Init1 & r2;
 
857
                                                r0 = r3 | A[3];
 
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;
 
860
                                        }  /* if(CMask) */
 
861
                                        else  {
 
862
                                                r2 = B[0];
 
863
                                                A[0] = r2 & Init1; 
 
864
                                                r3 = B[1];
 
865
                                                r1 = Init1 & r3;
 
866
                                                r0 = r2 | A[0];
 
867
                                                A[1] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
868
                                                if(D == 1) goto Nextcharfile;
 
869
                                                r2 = B[2];
 
870
                                                r1 = Init1 & r2;
 
871
                                                r0 = r3 | A[1];
 
872
                                                A[2] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
873
                                                if(D == 2) goto Nextcharfile;
 
874
                                                r3 = B[3];
 
875
                                                r1 = Init1 & r3;
 
876
                                                r0 = r2 | A[2];
 
877
                                                A[3] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
878
                                                if(D == 3) goto Nextcharfile;
 
879
                                                r2 = B[4];
 
880
                                                r1 = Init1 & r2;
 
881
                                                r0 = r3 | A[3];
 
882
                                                A[4] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
883
                                                if(D == 4) goto Nextcharfile;
 
884
                                        }
 
885
                                }
 
886
                                else {  
 
887
                                        j++;
 
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]; 
 
893
                                        /* epsilon move */
 
894
                                        if(( A[D] & 1 ) ^ INVERSE) {
 
895
                                                if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
 
896
                                                        num_of_matched++;
 
897
 
 
898
                                                        if (agrep_finalfp != NULL)
 
899
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
900
                                                        else {
 
901
                                                                int outindex;
 
902
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
903
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
904
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
905
                                                                }
 
906
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
907
                                                                        OUTPUT_OVERFLOW;
 
908
                                                                        free_buf(Text, buffer);
 
909
                                                                        return -1;
 
910
                                                                }
 
911
                                                                agrep_outpointer += outindex;
 
912
                                                        }
 
913
                                                        if (PRINTFILETIME) {
 
914
                                                                char *s = aprint_file_time(CurrentFileTime);
 
915
                                                                if (agrep_finalfp != NULL)
 
916
                                                                        fprintf(agrep_finalfp, "%s", s);
 
917
                                                                else {
 
918
                                                                        int outindex;
 
919
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
920
                                                                                        (s[outindex] != '\0'); outindex++) {
 
921
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
922
                                                                        }
 
923
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
924
                                                                                OUTPUT_OVERFLOW;
 
925
                                                                                free_buf(Text, buffer);
 
926
                                                                                return -1;
 
927
                                                                        }
 
928
                                                                        agrep_outpointer += outindex;
 
929
                                                                }
 
930
                                                        }
 
931
                                                        if (agrep_finalfp != NULL)
 
932
                                                                fprintf(agrep_finalfp, "\n");
 
933
                                                        else {
 
934
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
935
                                                                        OUTPUT_OVERFLOW;
 
936
                                                                        free_buf(Text, buffer);
 
937
                                                                        return -1;
 
938
                                                                }
 
939
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
940
                                                        }
 
941
 
 
942
                                                        free_buf(Text, buffer);
 
943
                                                        NEW_FILE = OFF;
 
944
                                                        return 0;
 
945
                                                } 
 
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);
 
950
                                                        return 0;       /* done */
 
951
                                                }
 
952
                                        }
 
953
                                        for(k=0; k<=D; k++)  B[k] = Init[0];
 
954
                                        r1 = Init1 & B[0];
 
955
                                        A[0] = (( Next[B[0]>>hh] | Next1[B[0]&LL]) & CMask) | r1;
 
956
                                        for(k=1; k<=D; k++) {
 
957
                                                r3 = B[k];
 
958
                                                r1 = Init1 & r3;
 
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;
 
961
                                        }
 
962
                                        if (DELIMITER) CurrentByteOffset += 1*D_length;
 
963
                                        else CurrentByteOffset += 1*1;
 
964
                                }
 
965
        Nextcharfile: 
 
966
                                i=i+1;
 
967
                                CurrentByteOffset ++;
 
968
                                c = buffer[i];
 
969
                                CMask = Mask[c];
 
970
                                if(c != Newline)
 
971
                                {
 
972
                                        if(CMask != 0) {  
 
973
                                                r2 = A[0];
 
974
                                                r1 = Init1 & r2;
 
975
                                                B[0] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
 
976
                                                r3 = A[1];
 
977
                                                r1 = Init1 & r3;
 
978
                                                r0 = B[0] | r2;
 
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;
 
981
                                                r2 = A[2];
 
982
                                                r1 = Init1 & r2;
 
983
                                                r0 = B[1] | r3;
 
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;
 
986
                                                r3 = A[3];
 
987
                                                r1 = Init1 & r3;
 
988
                                                r0 = B[2] | r2;
 
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;
 
991
                                                r2 = A[4];
 
992
                                                r1 = Init1 & r2;
 
993
                                                r0 = B[3] | r3;
 
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;
 
996
                                        }  /* if(CMask) */
 
997
                                        else  {
 
998
                                                r2 = A[0];
 
999
                                                B[0] = r2 & Init1; 
 
1000
                                                r3 = A[1];
 
1001
                                                r1 = Init1 & r3;
 
1002
                                                r0 = B[0] | r2;
 
1003
                                                B[1] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
1004
                                                if(D == 1) goto Nextchar1file;
 
1005
                                                r2 = A[2];
 
1006
                                                r1 = Init1 & r2;
 
1007
                                                r0 = B[1] | r3;
 
1008
                                                B[2] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
1009
                                                if(D == 2) goto Nextchar1file;
 
1010
                                                r3 = A[3];
 
1011
                                                r1 = Init1 & r3;
 
1012
                                                r0 = B[2] | r2;
 
1013
                                                B[3] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
1014
                                                if(D == 3) goto Nextchar1file;
 
1015
                                                r2 = A[4];
 
1016
                                                r1 = Init1 & r2;
 
1017
                                                r0 = B[3] | r3;
 
1018
                                                B[4] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
1019
                                                if(D == 4) goto Nextchar1file;
 
1020
                                        }
 
1021
                                } /* if(NOT Newline) */
 
1022
                                else {  
 
1023
                                        j++;
 
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]; 
 
1029
                                        /* epsilon move */
 
1030
                                        if(( B[D] & 1 ) ^ INVERSE) {
 
1031
                                                if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
 
1032
                                                        num_of_matched++;
 
1033
 
 
1034
                                                        if (agrep_finalfp != NULL)
 
1035
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
1036
                                                        else {
 
1037
                                                                int outindex;
 
1038
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1039
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
1040
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
1041
                                                                }
 
1042
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1043
                                                                        OUTPUT_OVERFLOW;
 
1044
                                                                        free_buf(Text, buffer);
 
1045
                                                                        return -1;
 
1046
                                                                }
 
1047
                                                                agrep_outpointer += outindex;
 
1048
                                                        }
 
1049
                                                        if (PRINTFILETIME) {
 
1050
                                                                char *s = aprint_file_time(CurrentFileTime);
 
1051
                                                                if (agrep_finalfp != NULL)
 
1052
                                                                        fprintf(agrep_finalfp, "%s", s);
 
1053
                                                                else {
 
1054
                                                                        int outindex;
 
1055
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1056
                                                                                        (s[outindex] != '\0'); outindex++) {
 
1057
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1058
                                                                        }
 
1059
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1060
                                                                                OUTPUT_OVERFLOW;
 
1061
                                                                                free_buf(Text, buffer);
 
1062
                                                                                return -1;
 
1063
                                                                        }
 
1064
                                                                        agrep_outpointer += outindex;
 
1065
                                                                }
 
1066
                                                        }
 
1067
                                                        if (agrep_finalfp != NULL)
 
1068
                                                                fprintf(agrep_finalfp, "\n");
 
1069
                                                        else {
 
1070
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
1071
                                                                        OUTPUT_OVERFLOW;
 
1072
                                                                        free_buf(Text, buffer);
 
1073
                                                                        return -1;
 
1074
                                                                }
 
1075
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
1076
                                                        }
 
1077
 
 
1078
                                                        free_buf(Text, buffer);
 
1079
                                                        NEW_FILE = OFF;
 
1080
                                                        return 0;
 
1081
                                                } 
 
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 */
 
1087
                                                }
 
1088
                                        }
 
1089
                                        for(k=0; k<=D; k++) A[k] = Init0; 
 
1090
                                        r1 = Init1 & A[0];
 
1091
                                        B[0] = ((Next[A[0]>>hh] | Next1[A[0]&LL]) & CMask) | r1;
 
1092
                                        for(k=1; k<=D; k++) {
 
1093
                                                r3 = A[k];
 
1094
                                                r1 = Init1 & r3;
 
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;
 
1097
                                        }
 
1098
                                        if (DELIMITER) CurrentByteOffset += 1*D_length;
 
1099
                                        else CurrentByteOffset += 1*1;
 
1100
                                }
 
1101
        Nextchar1file: 
 
1102
                                i=i+1;
 
1103
                                CurrentByteOffset ++;
 
1104
                        } /* while i < end */
 
1105
 
 
1106
                        strncpy(buffer, buffer+num_read, Maxline);
 
1107
                } /* while fill_buf... */
 
1108
                free_buf(Text, buffer);
 
1109
                return 0;
 
1110
#if     AGREP_POINTER
 
1111
        }
 
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;
 
1115
                end = num_read;
 
1116
                /* buffer[end-1] = '\n';*/ /* at end of the text. */
 
1117
                /* buffer[0] = '\n';*/  /* in front of the  text. */
 
1118
                i = 0;
 
1119
 
 
1120
                        /* An exact copy of the above RE1_PROCESS_WHEN_DNOTZERO: the while loop below */
 
1121
                        while (i < end )
 
1122
                        {
 
1123
                                c = buffer[i];
 
1124
                                CMask = Mask[c];
 
1125
                                if(c !=  Newline)
 
1126
                                {
 
1127
                                        if(CMask != 0) {  
 
1128
                                                r2 = B[0];
 
1129
                                                r1 = Init1 & r2;
 
1130
                                                A[0] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
 
1131
                                                r3 = B[1];
 
1132
                                                r1 = Init1 & r3;
 
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;
 
1136
                                                r2 = B[2];
 
1137
                                                r1 = Init1 & r2;
 
1138
                                                r0 = r3 | A[1];
 
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;
 
1141
                                                r3 = B[3];
 
1142
                                                r1 = Init1 & r3;
 
1143
                                                r0 = r2 | A[2];
 
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;
 
1146
                                                r2 = B[4];
 
1147
                                                r1 = Init1 & r2;
 
1148
                                                r0 = r3 | A[3];
 
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;
 
1151
                                        }  /* if(CMask) */
 
1152
                                        else  {
 
1153
                                                r2 = B[0];
 
1154
                                                A[0] = r2 & Init1; 
 
1155
                                                r3 = B[1];
 
1156
                                                r1 = Init1 & r3;
 
1157
                                                r0 = r2 | A[0];
 
1158
                                                A[1] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
1159
                                                if(D == 1) goto Nextcharmem;
 
1160
                                                r2 = B[2];
 
1161
                                                r1 = Init1 & r2;
 
1162
                                                r0 = r3 | A[1];
 
1163
                                                A[2] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
1164
                                                if(D == 2) goto Nextcharmem;
 
1165
                                                r3 = B[3];
 
1166
                                                r1 = Init1 & r3;
 
1167
                                                r0 = r2 | A[2];
 
1168
                                                A[3] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
1169
                                                if(D == 3) goto Nextcharmem;
 
1170
                                                r2 = B[4];
 
1171
                                                r1 = Init1 & r2;
 
1172
                                                r0 = r3 | A[3];
 
1173
                                                A[4] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
1174
                                                if(D == 4) goto Nextcharmem;
 
1175
                                        }
 
1176
                                }
 
1177
                                else {  
 
1178
                                        j++;
 
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]; 
 
1184
                                        /* epsilon move */
 
1185
                                        if(( A[D] & 1 ) ^ INVERSE) {
 
1186
                                                if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
 
1187
                                                        num_of_matched++;
 
1188
 
 
1189
                                                        if (agrep_finalfp != NULL)
 
1190
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
1191
                                                        else {
 
1192
                                                                int outindex;
 
1193
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1194
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
1195
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
1196
                                                                }
 
1197
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1198
                                                                        OUTPUT_OVERFLOW;
 
1199
                                                                        free_buf(Text, buffer);
 
1200
                                                                        return -1;
 
1201
                                                                }
 
1202
                                                                agrep_outpointer += outindex;
 
1203
                                                        }
 
1204
                                                        if (PRINTFILETIME) {
 
1205
                                                                char *s = aprint_file_time(CurrentFileTime);
 
1206
                                                                if (agrep_finalfp != NULL)
 
1207
                                                                        fprintf(agrep_finalfp, "%s", s);
 
1208
                                                                else {
 
1209
                                                                        int outindex;
 
1210
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1211
                                                                                        (s[outindex] != '\0'); outindex++) {
 
1212
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1213
                                                                        }
 
1214
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1215
                                                                                OUTPUT_OVERFLOW;
 
1216
                                                                                free_buf(Text, buffer);
 
1217
                                                                                return -1;
 
1218
                                                                        }
 
1219
                                                                        agrep_outpointer += outindex;
 
1220
                                                                }
 
1221
                                                        }
 
1222
                                                        if (agrep_finalfp != NULL)
 
1223
                                                                fprintf(agrep_finalfp, "\n");
 
1224
                                                        else {
 
1225
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
1226
                                                                        OUTPUT_OVERFLOW;
 
1227
                                                                        free_buf(Text, buffer);
 
1228
                                                                        return -1;
 
1229
                                                                }
 
1230
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
1231
                                                        }
 
1232
 
 
1233
                                                        free_buf(Text, buffer);
 
1234
                                                        NEW_FILE = OFF;
 
1235
                                                        return 0;
 
1236
                                                } 
 
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 */
 
1242
                                                }
 
1243
                                        }
 
1244
                                        for(k=0; k<=D; k++)  B[k] = Init[0];
 
1245
                                        r1 = Init1 & B[0];
 
1246
                                        A[0] = (( Next[B[0]>>hh] | Next1[B[0]&LL]) & CMask) | r1;
 
1247
                                        for(k=1; k<=D; k++) {
 
1248
                                                r3 = B[k];
 
1249
                                                r1 = Init1 & r3;
 
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;
 
1252
                                        }
 
1253
                                        if (DELIMITER) CurrentByteOffset += 1*D_length;
 
1254
                                        else CurrentByteOffset += 1*1;
 
1255
                                }
 
1256
        Nextcharmem: 
 
1257
                                i=i+1;
 
1258
                                CurrentByteOffset ++;
 
1259
                                c = buffer[i];
 
1260
                                CMask = Mask[c];
 
1261
                                if(c != Newline)
 
1262
                                {
 
1263
                                        if(CMask != 0) {  
 
1264
                                                r2 = A[0];
 
1265
                                                r1 = Init1 & r2;
 
1266
                                                B[0] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
 
1267
                                                r3 = A[1];
 
1268
                                                r1 = Init1 & r3;
 
1269
                                                r0 = B[0] | r2;
 
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;
 
1272
                                                r2 = A[2];
 
1273
                                                r1 = Init1 & r2;
 
1274
                                                r0 = B[1] | r3;
 
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;
 
1277
                                                r3 = A[3];
 
1278
                                                r1 = Init1 & r3;
 
1279
                                                r0 = B[2] | r2;
 
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;
 
1282
                                                r2 = A[4];
 
1283
                                                r1 = Init1 & r2;
 
1284
                                                r0 = B[3] | r3;
 
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;
 
1287
                                        }  /* if(CMask) */
 
1288
                                        else  {
 
1289
                                                r2 = A[0];
 
1290
                                                B[0] = r2 & Init1; 
 
1291
                                                r3 = A[1];
 
1292
                                                r1 = Init1 & r3;
 
1293
                                                r0 = B[0] | r2;
 
1294
                                                B[1] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
1295
                                                if(D == 1) goto Nextchar1mem;
 
1296
                                                r2 = A[2];
 
1297
                                                r1 = Init1 & r2;
 
1298
                                                r0 = B[1] | r3;
 
1299
                                                B[2] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
1300
                                                if(D == 2) goto Nextchar1mem;
 
1301
                                                r3 = A[3];
 
1302
                                                r1 = Init1 & r3;
 
1303
                                                r0 = B[2] | r2;
 
1304
                                                B[3] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
1305
                                                if(D == 3) goto Nextchar1mem;
 
1306
                                                r2 = A[4];
 
1307
                                                r1 = Init1 & r2;
 
1308
                                                r0 = B[3] | r3;
 
1309
                                                B[4] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;  
 
1310
                                                if(D == 4) goto Nextchar1mem;
 
1311
                                        }
 
1312
                                } /* if(NOT Newline) */
 
1313
                                else {  
 
1314
                                        j++;
 
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]; 
 
1320
                                        /* epsilon move */
 
1321
                                        if(( B[D] & 1 ) ^ INVERSE) {
 
1322
                                                if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
 
1323
                                                        num_of_matched++;
 
1324
 
 
1325
                                                        if (agrep_finalfp != NULL)
 
1326
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
1327
                                                        else {
 
1328
                                                                int outindex;
 
1329
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1330
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
1331
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
1332
                                                                }
 
1333
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1334
                                                                        OUTPUT_OVERFLOW;
 
1335
                                                                        free_buf(Text, buffer);
 
1336
                                                                        return -1;
 
1337
                                                                }
 
1338
                                                                agrep_outpointer += outindex;
 
1339
                                                        }
 
1340
                                                        if (PRINTFILETIME) {
 
1341
                                                                char *s = aprint_file_time(CurrentFileTime);
 
1342
                                                                if (agrep_finalfp != NULL)
 
1343
                                                                        fprintf(agrep_finalfp, "%s", s);
 
1344
                                                                else {
 
1345
                                                                        int outindex;
 
1346
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1347
                                                                                        (s[outindex] != '\0'); outindex++) {
 
1348
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1349
                                                                        }
 
1350
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1351
                                                                                OUTPUT_OVERFLOW;
 
1352
                                                                                free_buf(Text, buffer);
 
1353
                                                                                return -1;
 
1354
                                                                        }
 
1355
                                                                        agrep_outpointer += outindex;
 
1356
                                                                }
 
1357
                                                        }
 
1358
                                                        if (agrep_finalfp != NULL)
 
1359
                                                                fprintf(agrep_finalfp, "\n");
 
1360
                                                        else {
 
1361
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
1362
                                                                        OUTPUT_OVERFLOW;
 
1363
                                                                        free_buf(Text, buffer);
 
1364
                                                                        return -1;
 
1365
                                                                }
 
1366
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
1367
                                                        }
 
1368
 
 
1369
                                                        free_buf(Text, buffer);
 
1370
                                                        NEW_FILE = OFF;
 
1371
                                                        return 0;
 
1372
                                                } 
 
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 */
 
1378
                                                }
 
1379
                                        }
 
1380
                                        for(k=0; k<=D; k++) A[k] = Init0; 
 
1381
                                        r1 = Init1 & A[0];
 
1382
                                        B[0] = ((Next[A[0]>>hh] | Next1[A[0]&LL]) & CMask) | r1;
 
1383
                                        for(k=1; k<=D; k++) {
 
1384
                                                r3 = A[k];
 
1385
                                                r1 = Init1 & r3;
 
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;
 
1388
                                        }
 
1389
                                        if (DELIMITER) CurrentByteOffset += 1*D_length;
 
1390
                                        else CurrentByteOffset += 1*1;
 
1391
                                }
 
1392
        Nextchar1mem: 
 
1393
                                i=i+1;
 
1394
                                CurrentByteOffset ++;
 
1395
                        } /* while i < end */
 
1396
 
 
1397
                return 0;
 
1398
        }
 
1399
#endif  /*AGREP_POINTER*/
 
1400
} /* re1 */
 
1401
 
 
1402
int
 
1403
re(Text, M, D)
 
1404
int Text, M, D;
 
1405
{
 
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 */
 
1412
        CHAR *buffer;
 
1413
 
 
1414
        base = WORD - M;
 
1415
        k = 2*exponen(M);
 
1416
        if(FIRST_IN_RE) {
 
1417
                compute_next(M, Next, Next1); 
 
1418
                FIRST_IN_RE = 0;    
 
1419
        }
 
1420
        for(i=0; i< MAXSYM; i++) RMask[i] = Mask[i];
 
1421
        r_NO_ERR = NO_ERR_MASK;
 
1422
        Newline = '\n';
 
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? */
 
1426
        Init1 = Init0 | 1; 
 
1427
        r2 = r3 = Init0;
 
1428
        for(k=0; k<= D; k++) { 
 
1429
                A[k] = B[k] = Init[0]; 
 
1430
        }  /* can be out? */
 
1431
        FIRST_TIME = ON;
 
1432
        alloc_buf(Text, &buffer, BlockSize+Maxline+1);
 
1433
        if ( D == 0 )
 
1434
        {
 
1435
#if     AGREP_POINTER
 
1436
            if(Text != -1) {
 
1437
#endif  /*AGREP_POINTER*/
 
1438
                lasti = Maxline;
 
1439
                while ((num_read = fill_buf(Text, buffer + Maxline, BlockSize)) > 0)
 
1440
                {
 
1441
                        i=Maxline;
 
1442
                        end = Maxline + num_read ;
 
1443
#if 0
 
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';
 
1447
#endif /* 0 */
 
1448
                        if(FIRST_TIME) {
 
1449
                                buffer[i-1] = '\n';
 
1450
                                i--;
 
1451
                                CurrentByteOffset --;
 
1452
                                FIRST_TIME = 0;
 
1453
                        }
 
1454
 
 
1455
                        /* RE_PROCESS_WHEN_DZERO: the while-loop below */
 
1456
                        while (i < end) 
 
1457
                        {
 
1458
                                c = buffer[i++];
 
1459
                                CurrentByteOffset ++;
 
1460
                                CMask = RMask[c];
 
1461
                                if(c != Newline)
 
1462
                                {  
 
1463
                                        r1 = Init1 & r3;
 
1464
                                        r2 = (Next[r3] & CMask) | r1;
 
1465
                                }
 
1466
                                else {  
 
1467
                                        r1 = Init1 & r3;            /* match against '\n' */
 
1468
                                        r2 = Next[r3] & CMask | r1;
 
1469
                                        j++;
 
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)) {
 
1475
                                                        num_of_matched++;
 
1476
 
 
1477
                                                        if (agrep_finalfp != NULL)
 
1478
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
1479
                                                        else {
 
1480
                                                                int outindex;
 
1481
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1482
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
1483
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
1484
                                                                }
 
1485
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1486
                                                                        OUTPUT_OVERFLOW;
 
1487
                                                                        free_buf(Text, buffer);
 
1488
                                                                        return -1;
 
1489
                                                                }
 
1490
                                                                agrep_outpointer += outindex;
 
1491
                                                        }
 
1492
                                                        if (PRINTFILETIME) {
 
1493
                                                                char *s = aprint_file_time(CurrentFileTime);
 
1494
                                                                if (agrep_finalfp != NULL)
 
1495
                                                                        fprintf(agrep_finalfp, "%s", s);
 
1496
                                                                else {
 
1497
                                                                        int outindex;
 
1498
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1499
                                                                                        (s[outindex] != '\0'); outindex++) {
 
1500
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1501
                                                                        }
 
1502
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1503
                                                                                OUTPUT_OVERFLOW;
 
1504
                                                                                free_buf(Text, buffer);
 
1505
                                                                                return -1;
 
1506
                                                                        }
 
1507
                                                                        agrep_outpointer += outindex;
 
1508
                                                                }
 
1509
                                                        }
 
1510
                                                        if (agrep_finalfp != NULL)
 
1511
                                                                fprintf(agrep_finalfp, "\n");
 
1512
                                                        else {
 
1513
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
1514
                                                                        OUTPUT_OVERFLOW;
 
1515
                                                                        free_buf(Text, buffer);
 
1516
                                                                        return -1;
 
1517
                                                                }
 
1518
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
1519
                                                        }
 
1520
 
 
1521
                                                        free_buf(Text, buffer);
 
1522
                                                        NEW_FILE = OFF;
 
1523
                                                        return 0;
 
1524
                                                } 
 
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 */
 
1530
                                                }
 
1531
                                        }
 
1532
                                        lasti = i - 1;
 
1533
                                        r3 = Init0;
 
1534
                                        r2 = (Next[r3] & CMask) | Init0;
 
1535
                                        if (DELIMITER) CurrentByteOffset += 1*D_length;
 
1536
                                        else CurrentByteOffset += 1*1;
 
1537
                                }
 
1538
                                c = buffer[i++];   
 
1539
                                CurrentByteOffset ++;
 
1540
                                CMask = RMask[c];
 
1541
                                if(c != Newline)
 
1542
                                {
 
1543
                                        r1 = Init1 & r2;
 
1544
                                        r3 = (Next[r2] & CMask) | r1;
 
1545
                                }
 
1546
                                else {  
 
1547
                                        j++;
 
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)) {
 
1555
                                                        num_of_matched++;
 
1556
 
 
1557
                                                        if (agrep_finalfp != NULL)
 
1558
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
1559
                                                        else {
 
1560
                                                                int outindex;
 
1561
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1562
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
1563
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
1564
                                                                }
 
1565
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1566
                                                                        OUTPUT_OVERFLOW;
 
1567
                                                                        free_buf(Text, buffer);
 
1568
                                                                        return -1;
 
1569
                                                                }
 
1570
                                                                agrep_outpointer += outindex;
 
1571
                                                        }
 
1572
                                                        if (PRINTFILETIME) {
 
1573
                                                                char *s = aprint_file_time(CurrentFileTime);
 
1574
                                                                if (agrep_finalfp != NULL)
 
1575
                                                                        fprintf(agrep_finalfp, "%s", s);
 
1576
                                                                else {
 
1577
                                                                        int outindex;
 
1578
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1579
                                                                                        (s[outindex] != '\0'); outindex++) {
 
1580
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1581
                                                                        }
 
1582
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1583
                                                                                OUTPUT_OVERFLOW;
 
1584
                                                                                free_buf(Text, buffer);
 
1585
                                                                                return -1;
 
1586
                                                                        }
 
1587
                                                                        agrep_outpointer += outindex;
 
1588
                                                                }
 
1589
                                                        }
 
1590
                                                        if (agrep_finalfp != NULL)
 
1591
                                                                fprintf(agrep_finalfp, "\n");
 
1592
                                                        else {
 
1593
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
1594
                                                                        OUTPUT_OVERFLOW;
 
1595
                                                                        free_buf(Text, buffer);
 
1596
                                                                        return -1;
 
1597
                                                                }
 
1598
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
1599
                                                        }
 
1600
 
 
1601
                                                        free_buf(Text, buffer);
 
1602
                                                        NEW_FILE = OFF;
 
1603
                                                        return 0;
 
1604
                                                } 
 
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 */
 
1610
                                                }
 
1611
                                        }
 
1612
                                        lasti = i - 1;
 
1613
                                        r2 = Init0; 
 
1614
                                        r3 = (Next[r2] & CMask) | Init0;  /* match the newline */
 
1615
                                        if (DELIMITER) CurrentByteOffset += 1*D_length;
 
1616
                                        else CurrentByteOffset += 1*1;
 
1617
                                }
 
1618
                        } /* while */
 
1619
 
 
1620
                        ResidueSize = Maxline + num_read - lasti;
 
1621
                        if(ResidueSize > Maxline) {
 
1622
                                ResidueSize = Maxline;  
 
1623
                        }
 
1624
                        strncpy(buffer+Maxline-ResidueSize, buffer+lasti, ResidueSize);
 
1625
                        lasti = Maxline - ResidueSize;
 
1626
                } /* while fill_buf() */
 
1627
                free_buf(Text, buffer);
 
1628
                return 0;
 
1629
#if     AGREP_POINTER
 
1630
            }
 
1631
            else {
 
1632
                num_read = agrep_inlen;
 
1633
                buffer = (CHAR *)agrep_inbuffer;
 
1634
                end = num_read;
 
1635
                /* buffer[end-1] = '\n';*/ /* at end of the text. */
 
1636
                /* buffer[0] = '\n';*/  /* in front of the  text. */
 
1637
                i = 0;
 
1638
                lasti = 1;
 
1639
 
 
1640
                        /* An exact copy of the above RE_PROCESS_WHEN_DZERO: the while-loop below */
 
1641
                        while (i < end) 
 
1642
                        {
 
1643
                                c = buffer[i++];
 
1644
                                CurrentByteOffset ++;
 
1645
                                CMask = RMask[c];
 
1646
                                if(c != Newline)
 
1647
                                {  
 
1648
                                        r1 = Init1 & r3;
 
1649
                                        r2 = (Next[r3] & CMask) | r1;
 
1650
                                }
 
1651
                                else {  
 
1652
                                        r1 = Init1 & r3;            /* match against '\n' */
 
1653
                                        r2 = Next[r3] & CMask | r1;
 
1654
                                        j++;
 
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)) {
 
1660
                                                        num_of_matched++;
 
1661
 
 
1662
                                                        if (agrep_finalfp != NULL)
 
1663
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
1664
                                                        else {
 
1665
                                                                int outindex;
 
1666
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1667
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
1668
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
1669
                                                                }
 
1670
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1671
                                                                        OUTPUT_OVERFLOW;
 
1672
                                                                        free_buf(Text, buffer);
 
1673
                                                                        return -1;
 
1674
                                                                }
 
1675
                                                                agrep_outpointer += outindex;
 
1676
                                                        }
 
1677
                                                        if (PRINTFILETIME) {
 
1678
                                                                char *s = aprint_file_time(CurrentFileTime);
 
1679
                                                                if (agrep_finalfp != NULL)
 
1680
                                                                        fprintf(agrep_finalfp, "%s", s);
 
1681
                                                                else {
 
1682
                                                                        int outindex;
 
1683
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1684
                                                                                        (s[outindex] != '\0'); outindex++) {
 
1685
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1686
                                                                        }
 
1687
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1688
                                                                                OUTPUT_OVERFLOW;
 
1689
                                                                                free_buf(Text, buffer);
 
1690
                                                                                return -1;
 
1691
                                                                        }
 
1692
                                                                        agrep_outpointer += outindex;
 
1693
                                                                }
 
1694
                                                        }
 
1695
                                                        if (agrep_finalfp != NULL)
 
1696
                                                                fprintf(agrep_finalfp, "\n");
 
1697
                                                        else {
 
1698
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
1699
                                                                        OUTPUT_OVERFLOW;
 
1700
                                                                        free_buf(Text, buffer);
 
1701
                                                                        return -1;
 
1702
                                                                }
 
1703
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
1704
                                                        }
 
1705
 
 
1706
                                                        free_buf(Text, buffer);
 
1707
                                                        NEW_FILE = OFF;
 
1708
                                                        return 0;
 
1709
                                                } 
 
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 */
 
1715
                                                }
 
1716
                                        }
 
1717
                                        lasti = i - 1;
 
1718
                                        r3 = Init0;
 
1719
                                        r2 = (Next[r3] & CMask) | Init0;
 
1720
                                        if (DELIMITER) CurrentByteOffset += 1*D_length;
 
1721
                                        else CurrentByteOffset += 1*1;
 
1722
                                }
 
1723
                                c = buffer[i++];   
 
1724
                                CurrentByteOffset ++;
 
1725
                                CMask = RMask[c];
 
1726
                                if(c != Newline)
 
1727
                                {
 
1728
                                        r1 = Init1 & r2;
 
1729
                                        r3 = (Next[r2] & CMask) | r1;
 
1730
                                }
 
1731
                                else {  
 
1732
                                        j++;
 
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)) {
 
1740
                                                        num_of_matched++;
 
1741
 
 
1742
                                                        if (agrep_finalfp != NULL)
 
1743
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
1744
                                                        else {
 
1745
                                                                int outindex;
 
1746
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1747
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
1748
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
1749
                                                                }
 
1750
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1751
                                                                        OUTPUT_OVERFLOW;
 
1752
                                                                        free_buf(Text, buffer);
 
1753
                                                                        return -1;
 
1754
                                                                }
 
1755
                                                                agrep_outpointer += outindex;
 
1756
                                                        }
 
1757
                                                        if (PRINTFILETIME) {
 
1758
                                                                char *s = aprint_file_time(CurrentFileTime);
 
1759
                                                                if (agrep_finalfp != NULL)
 
1760
                                                                        fprintf(agrep_finalfp, "%s", s);
 
1761
                                                                else {
 
1762
                                                                        int outindex;
 
1763
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1764
                                                                                        (s[outindex] != '\0'); outindex++) {
 
1765
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1766
                                                                        }
 
1767
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1768
                                                                                OUTPUT_OVERFLOW;
 
1769
                                                                                free_buf(Text, buffer);
 
1770
                                                                                return -1;
 
1771
                                                                        }
 
1772
                                                                        agrep_outpointer += outindex;
 
1773
                                                                }
 
1774
                                                        }
 
1775
                                                        if (agrep_finalfp != NULL)
 
1776
                                                                fprintf(agrep_finalfp, "\n");
 
1777
                                                        else {
 
1778
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
1779
                                                                        OUTPUT_OVERFLOW;
 
1780
                                                                        free_buf(Text, buffer);
 
1781
                                                                        return -1;
 
1782
                                                                }
 
1783
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
1784
                                                        }
 
1785
 
 
1786
                                                        free_buf(Text, buffer);
 
1787
                                                        NEW_FILE = OFF;
 
1788
                                                        return 0;
 
1789
                                                } 
 
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 */
 
1795
                                                }
 
1796
                                        }
 
1797
                                        lasti = i - 1;
 
1798
                                        r2 = Init0; 
 
1799
                                        r3 = (Next[r2] & CMask) | Init0;  /* match the newline */
 
1800
                                        if (DELIMITER) CurrentByteOffset += 1*D_length;
 
1801
                                        else CurrentByteOffset += 1*1;
 
1802
                                }
 
1803
                        } /* while */
 
1804
 
 
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 */
 
1806
                return 0;
 
1807
            }
 
1808
#endif  /*AGREP_POINTER*/
 
1809
        } /* end if(D==0) */
 
1810
 
 
1811
#if     AGREP_POINTER
 
1812
        if (Text != -1) {
 
1813
#endif  /*AGREP_POINTER*/
 
1814
                while ((num_read = fill_buf(Text, buffer + Maxline, BlockSize)) > 0)
 
1815
                {
 
1816
                        i=Maxline; 
 
1817
                        end = Maxline+num_read;
 
1818
#if 0
 
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';
 
1822
#endif /* 0 */
 
1823
                        if(FIRST_TIME) {
 
1824
                                buffer[i-1] = '\n';
 
1825
                                i--;
 
1826
                                CurrentByteOffset --;
 
1827
                                FIRST_TIME = 0;
 
1828
                        }
 
1829
 
 
1830
                        /* RE_PROCESS_WHEN_DNOTZERO: the while-loop below */
 
1831
                        while (i < end)
 
1832
                        {   
 
1833
                                c = buffer[i++];
 
1834
                                CurrentByteOffset ++;
 
1835
                                CMask = RMask[c];
 
1836
                                if (c != Newline)
 
1837
                                {  
 
1838
                                        r_even = B[0];
 
1839
                                        r1 = Init1 & r_even;
 
1840
                                        A[0] = (Next[r_even] & CMask) | r1;
 
1841
                                        r_odd = B[1];
 
1842
                                        r1 = Init1 & r_odd;
 
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;
 
1846
                                        r_even = B[2];
 
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;
 
1851
                                        r_odd = B[3];
 
1852
                                        r1 = Init1 & r_odd;
 
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;
 
1856
                                        r_even = B[4];
 
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 ;  
 
1860
                                        goto Nextcharfile;
 
1861
                                } /* if NOT Newline */
 
1862
                                else {  
 
1863
                                        j++;
 
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)) {
 
1871
                                                        num_of_matched++;    
 
1872
 
 
1873
                                                        if (agrep_finalfp != NULL)
 
1874
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
1875
                                                        else {
 
1876
                                                                int outindex;
 
1877
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1878
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
1879
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
1880
                                                                }
 
1881
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1882
                                                                        OUTPUT_OVERFLOW;
 
1883
                                                                        free_buf(Text, buffer);
 
1884
                                                                        return -1;
 
1885
                                                                }
 
1886
                                                                agrep_outpointer += outindex;
 
1887
                                                        }
 
1888
                                                        if (PRINTFILETIME) {
 
1889
                                                                char *s = aprint_file_time(CurrentFileTime);
 
1890
                                                                if (agrep_finalfp != NULL)
 
1891
                                                                        fprintf(agrep_finalfp, "%s", s);
 
1892
                                                                else {
 
1893
                                                                        int outindex;
 
1894
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1895
                                                                                        (s[outindex] != '\0'); outindex++) {
 
1896
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1897
                                                                        }
 
1898
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1899
                                                                                OUTPUT_OVERFLOW;
 
1900
                                                                                free_buf(Text, buffer);
 
1901
                                                                                return -1;
 
1902
                                                                        }
 
1903
                                                                        agrep_outpointer += outindex;
 
1904
                                                                }
 
1905
                                                        }
 
1906
                                                        if (agrep_finalfp != NULL)
 
1907
                                                                fprintf(agrep_finalfp, "\n");
 
1908
                                                        else {
 
1909
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
1910
                                                                        OUTPUT_OVERFLOW;
 
1911
                                                                        free_buf(Text, buffer);
 
1912
                                                                        return -1;
 
1913
                                                                }
 
1914
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
1915
                                                        }
 
1916
 
 
1917
                                                        free_buf(Text, buffer);
 
1918
                                                        NEW_FILE = OFF;
 
1919
                                                        return 0;
 
1920
                                                }  
 
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 */
 
1926
                                                }
 
1927
                                        }
 
1928
                                        for(k=0; k<= D; k++) { 
 
1929
                                                A[k] = B[k] = Init[k]; 
 
1930
                                        }
 
1931
                                        r1 = Init1 & B[0]; 
 
1932
                                        A[0] = (Next[B[0]] & CMask) | r1;
 
1933
                                        for(k=1; k<= D; k++) {
 
1934
                                                r1 = Init1 & B[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;
 
1937
                                        }
 
1938
                                        if (DELIMITER) CurrentByteOffset += 1*D_length;
 
1939
                                        else CurrentByteOffset += 1*1;
 
1940
                                }
 
1941
        Nextcharfile: 
 
1942
                                c = buffer[i];
 
1943
                                CMask = RMask[c];
 
1944
                                if(c != Newline)
 
1945
                                { 
 
1946
                                        r1 = Init1 & A[0];
 
1947
                                        B[0] = (Next[A[0]] & CMask) | r1;
 
1948
                                        r1 = Init1 & A[1];
 
1949
                                        B[1] = (Next[A[1]] & CMask) | ((A[0] | Next[A[0] | B[0]]) & r_NO_ERR) | r1 ;  
 
1950
                                        if(D == 1) goto Nextchar1file;
 
1951
                                        r1 = Init1 & A[2];
 
1952
                                        B[2] = (Next[A[2]] & CMask) | ((A[1] | Next[A[1] | B[1]]) &r_NO_ERR) | r1 ;  
 
1953
                                        if(D == 2) goto Nextchar1file;
 
1954
                                        r1 = Init1 & A[3];
 
1955
                                        B[3] = (Next[A[3]] & CMask) | ((A[2] | Next[A[2] | B[2]])&r_NO_ERR) | r1 ;  
 
1956
                                        if(D == 3) goto Nextchar1file;
 
1957
                                        r1 = Init1 & A[4];
 
1958
                                        B[4] = (Next[A[4]] & CMask) | ((A[3] | Next[A[3] | B[3]])&r_NO_ERR) | r1 ;  
 
1959
                                        goto Nextchar1file;
 
1960
                                } /* if(NOT Newline) */
 
1961
                                else {  
 
1962
                                        j++;
 
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)) {
 
1970
                                                        num_of_matched++;
 
1971
 
 
1972
                                                        if (agrep_finalfp != NULL)
 
1973
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
1974
                                                        else {
 
1975
                                                                int outindex;
 
1976
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1977
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
1978
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
1979
                                                                }
 
1980
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1981
                                                                        OUTPUT_OVERFLOW;
 
1982
                                                                        free_buf(Text, buffer);
 
1983
                                                                        return -1;
 
1984
                                                                }
 
1985
                                                                agrep_outpointer += outindex;
 
1986
                                                        }
 
1987
                                                        if (PRINTFILETIME) {
 
1988
                                                                char *s = aprint_file_time(CurrentFileTime);
 
1989
                                                                if (agrep_finalfp != NULL)
 
1990
                                                                        fprintf(agrep_finalfp, "%s", s);
 
1991
                                                                else {
 
1992
                                                                        int outindex;
 
1993
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
1994
                                                                                        (s[outindex] != '\0'); outindex++) {
 
1995
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
1996
                                                                        }
 
1997
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
1998
                                                                                OUTPUT_OVERFLOW;
 
1999
                                                                                free_buf(Text, buffer);
 
2000
                                                                                return -1;
 
2001
                                                                        }
 
2002
                                                                        agrep_outpointer += outindex;
 
2003
                                                                }
 
2004
                                                        }
 
2005
                                                        if (agrep_finalfp != NULL)
 
2006
                                                                fprintf(agrep_finalfp, "\n");
 
2007
                                                        else {
 
2008
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
2009
                                                                        OUTPUT_OVERFLOW;
 
2010
                                                                        free_buf(Text, buffer);
 
2011
                                                                        return -1;
 
2012
                                                                }
 
2013
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
2014
                                                        }
 
2015
 
 
2016
                                                        free_buf(Text, buffer);
 
2017
                                                        NEW_FILE = OFF;
 
2018
                                                        return 0;
 
2019
                                                } 
 
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 */
 
2025
                                                }
 
2026
                                        }
 
2027
                                        for(k=0; k<= D; k++) { 
 
2028
                                                A[k] = B[k] = Init[k]; 
 
2029
                                        }
 
2030
                                        r1 = Init1 & A[0]; 
 
2031
                                        B[0] = (Next[A[0]] & CMask) | r1;
 
2032
                                        for(k=1; k<= D; k++) {
 
2033
                                                r1 = Init1 & A[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;
 
2036
                                        }
 
2037
                                        if (DELIMITER) CurrentByteOffset += 1*D_length;
 
2038
                                        else CurrentByteOffset += 1*1;
 
2039
                                }
 
2040
        Nextchar1file: 
 
2041
                                i++;
 
2042
                                CurrentByteOffset ++;
 
2043
                        } /* while i < end */
 
2044
 
 
2045
                        strncpy(buffer, buffer+num_read, Maxline);
 
2046
                } /* while  fill_buf() */
 
2047
                free_buf(Text, buffer);
 
2048
                return 0;
 
2049
#if     AGREP_POINTER
 
2050
        }
 
2051
        else {
 
2052
                num_read = agrep_inlen;
 
2053
                buffer = (CHAR *)agrep_inbuffer;
 
2054
                end = num_read;
 
2055
                /* buffer[end-1] = '\n';*/ /* at end of the text. */
 
2056
                /* buffer[0] = '\n';*/  /* in front of the  text. */
 
2057
                i = 0;
 
2058
 
 
2059
                        /* An exact copy of the above RE_PROCESS_WHEN_DNOTZERO: the while-loop below */
 
2060
                        while (i < end)
 
2061
                        {   
 
2062
                                c = buffer[i++];
 
2063
                                CurrentByteOffset ++;
 
2064
                                CMask = RMask[c];
 
2065
                                if (c != Newline)
 
2066
                                {  
 
2067
                                        r_even = B[0];
 
2068
                                        r1 = Init1 & r_even;
 
2069
                                        A[0] = (Next[r_even] & CMask) | r1;
 
2070
                                        r_odd = B[1];
 
2071
                                        r1 = Init1 & r_odd;
 
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;
 
2075
                                        r_even = B[2];
 
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;
 
2080
                                        r_odd = B[3];
 
2081
                                        r1 = Init1 & r_odd;
 
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;
 
2085
                                        r_even = B[4];
 
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 ;  
 
2089
                                        goto Nextcharmem;
 
2090
                                } /* if NOT Newline */
 
2091
                                else {  
 
2092
                                        j++;
 
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)) {
 
2100
                                                        num_of_matched++;    
 
2101
 
 
2102
                                                        if (agrep_finalfp != NULL)
 
2103
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
2104
                                                        else {
 
2105
                                                                int outindex;
 
2106
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
2107
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
2108
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
2109
                                                                }
 
2110
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
2111
                                                                        OUTPUT_OVERFLOW;
 
2112
                                                                        free_buf(Text, buffer);
 
2113
                                                                        return -1;
 
2114
                                                                }
 
2115
                                                                agrep_outpointer += outindex;
 
2116
                                                        }
 
2117
                                                        if (PRINTFILETIME) {
 
2118
                                                                char *s = aprint_file_time(CurrentFileTime);
 
2119
                                                                if (agrep_finalfp != NULL)
 
2120
                                                                        fprintf(agrep_finalfp, "%s", s);
 
2121
                                                                else {
 
2122
                                                                        int outindex;
 
2123
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
2124
                                                                                        (s[outindex] != '\0'); outindex++) {
 
2125
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
2126
                                                                        }
 
2127
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
2128
                                                                                OUTPUT_OVERFLOW;
 
2129
                                                                                free_buf(Text, buffer);
 
2130
                                                                                return -1;
 
2131
                                                                        }
 
2132
                                                                        agrep_outpointer += outindex;
 
2133
                                                                }
 
2134
                                                        }
 
2135
                                                        if (agrep_finalfp != NULL)
 
2136
                                                                fprintf(agrep_finalfp, "\n");
 
2137
                                                        else {
 
2138
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
2139
                                                                        OUTPUT_OVERFLOW;
 
2140
                                                                        free_buf(Text, buffer);
 
2141
                                                                        return -1;
 
2142
                                                                }
 
2143
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
2144
                                                        }
 
2145
 
 
2146
                                                        free_buf(Text, buffer);
 
2147
                                                        NEW_FILE = OFF;
 
2148
                                                        return 0;
 
2149
                                                }  
 
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 */
 
2155
                                                }
 
2156
                                        }
 
2157
                                        for(k=0; k<= D; k++) { 
 
2158
                                                A[k] = B[k] = Init[k]; 
 
2159
                                        }
 
2160
                                        r1 = Init1 & B[0]; 
 
2161
                                        A[0] = (Next[B[0]] & CMask) | r1;
 
2162
                                        for(k=1; k<= D; k++) {
 
2163
                                                r1 = Init1 & B[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;
 
2166
                                        }
 
2167
                                        if (DELIMITER) CurrentByteOffset += 1*D_length;
 
2168
                                        else CurrentByteOffset += 1*1;
 
2169
                                }
 
2170
        Nextcharmem: 
 
2171
                                c = buffer[i];
 
2172
                                CMask = RMask[c];
 
2173
                                if(c != Newline)
 
2174
                                { 
 
2175
                                        r1 = Init1 & A[0];
 
2176
                                        B[0] = (Next[A[0]] & CMask) | r1;
 
2177
                                        r1 = Init1 & A[1];
 
2178
                                        B[1] = (Next[A[1]] & CMask) | ((A[0] | Next[A[0] | B[0]]) & r_NO_ERR) | r1 ;  
 
2179
                                        if(D == 1) goto Nextchar1mem;
 
2180
                                        r1 = Init1 & A[2];
 
2181
                                        B[2] = (Next[A[2]] & CMask) | ((A[1] | Next[A[1] | B[1]]) &r_NO_ERR) | r1 ;  
 
2182
                                        if(D == 2) goto Nextchar1mem;
 
2183
                                        r1 = Init1 & A[3];
 
2184
                                        B[3] = (Next[A[3]] & CMask) | ((A[2] | Next[A[2] | B[2]])&r_NO_ERR) | r1 ;  
 
2185
                                        if(D == 3) goto Nextchar1mem;
 
2186
                                        r1 = Init1 & A[4];
 
2187
                                        B[4] = (Next[A[4]] & CMask) | ((A[3] | Next[A[3] | B[3]])&r_NO_ERR) | r1 ;  
 
2188
                                        goto Nextchar1mem;
 
2189
                                } /* if(NOT Newline) */
 
2190
                                else {  
 
2191
                                        j++;
 
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)) {
 
2199
                                                        num_of_matched++;
 
2200
 
 
2201
                                                        if (agrep_finalfp != NULL)
 
2202
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
2203
                                                        else {
 
2204
                                                                int outindex;
 
2205
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
2206
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
2207
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
2208
                                                                }
 
2209
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
2210
                                                                        OUTPUT_OVERFLOW;
 
2211
                                                                        free_buf(Text, buffer);
 
2212
                                                                        return -1;
 
2213
                                                                }
 
2214
                                                                agrep_outpointer += outindex;
 
2215
                                                        }
 
2216
                                                        if (PRINTFILETIME) {
 
2217
                                                                char *s = aprint_file_time(CurrentFileTime);
 
2218
                                                                if (agrep_finalfp != NULL)
 
2219
                                                                        fprintf(agrep_finalfp, "%s", s);
 
2220
                                                                else {
 
2221
                                                                        int outindex;
 
2222
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
2223
                                                                                        (s[outindex] != '\0'); outindex++) {
 
2224
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
2225
                                                                        }
 
2226
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
2227
                                                                                OUTPUT_OVERFLOW;
 
2228
                                                                                free_buf(Text, buffer);
 
2229
                                                                                return -1;
 
2230
                                                                        }
 
2231
                                                                        agrep_outpointer += outindex;
 
2232
                                                                }
 
2233
                                                        }
 
2234
                                                        if (agrep_finalfp != NULL)
 
2235
                                                                fprintf(agrep_finalfp, "\n");
 
2236
                                                        else {
 
2237
                                                                if (agrep_outpointer+1>=agrep_outlen) {
 
2238
                                                                        OUTPUT_OVERFLOW;
 
2239
                                                                        free_buf(Text, buffer);
 
2240
                                                                        return -1;
 
2241
                                                                }
 
2242
                                                                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
2243
                                                        }
 
2244
 
 
2245
                                                        free_buf(Text, buffer);
 
2246
                                                        NEW_FILE = OFF;
 
2247
                                                        return 0;
 
2248
                                                } 
 
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 */
 
2254
                                                }
 
2255
                                        }
 
2256
                                        for(k=0; k<= D; k++) { 
 
2257
                                                A[k] = B[k] = Init[k]; 
 
2258
                                        }
 
2259
                                        r1 = Init1 & A[0]; 
 
2260
                                        B[0] = (Next[A[0]] & CMask) | r1;
 
2261
                                        for(k=1; k<= D; k++) {
 
2262
                                                r1 = Init1 & A[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;
 
2265
                                        }
 
2266
                                        if (DELIMITER) CurrentByteOffset += 1*D_length;
 
2267
                                        else CurrentByteOffset += 1*1;
 
2268
                                }
 
2269
        Nextchar1mem: 
 
2270
                                i++;
 
2271
                                CurrentByteOffset ++;
 
2272
                        } /* while i < end */
 
2273
 
 
2274
                return 0;
 
2275
        }
 
2276
#endif  /*AGREP_POINTER*/
 
2277
} /* re */
 
2278
 
 
2279
int
 
2280
r_output (buffer, i, end, j) 
 
2281
int i, end, j; 
 
2282
CHAR *buffer;
 
2283
{
 
2284
        int PRINTED = 0;
 
2285
        int bp;
 
2286
        if(i >= end) return 0;
 
2287
        if ((j < 1) || (CurrentByteOffset < 0)) return 0;
 
2288
        num_of_matched++;
 
2289
        if(COUNT)  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":"";
 
2294
 
 
2295
                if (agrep_finalfp != NULL)
 
2296
                        fprintf(agrep_finalfp, "%s%s", prevstring, CurrentFileName);
 
2297
                else {
 
2298
                        int outindex;
 
2299
                        if (prevstring[0] != '\0') {
 
2300
                                if(agrep_outpointer + 1 >= agrep_outlen) {
 
2301
                                        OUTPUT_OVERFLOW;
 
2302
                                        return -1;
 
2303
                                }
 
2304
                                else agrep_outbuffer[agrep_outpointer ++] = prevstring[0];
 
2305
                        }
 
2306
                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
2307
                                        (CurrentFileName[outindex] != '\0'); outindex++) {
 
2308
                                agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
2309
                        }
 
2310
                        if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
2311
                                OUTPUT_OVERFLOW;
 
2312
                                return -1;
 
2313
                        }
 
2314
                        agrep_outpointer += outindex;
 
2315
                }
 
2316
                if (PRINTFILETIME) {
 
2317
                        char *s = aprint_file_time(CurrentFileTime);
 
2318
                        if (agrep_finalfp != NULL)
 
2319
                                fprintf(agrep_finalfp, "%s", s);
 
2320
                        else {
 
2321
                                int outindex;
 
2322
                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
2323
                                                (s[outindex] != '\0'); outindex++) {
 
2324
                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
2325
                                }
 
2326
                                if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
2327
                                        OUTPUT_OVERFLOW;
 
2328
                                        return -1;
 
2329
                                }
 
2330
                                agrep_outpointer += outindex;
 
2331
                        }
 
2332
                }
 
2333
                if (agrep_finalfp != NULL)
 
2334
                        fprintf(agrep_finalfp, ":%c", nextchar);
 
2335
                else {
 
2336
                        if (agrep_outpointer+2>= agrep_outlen) {
 
2337
                                OUTPUT_OVERFLOW;
 
2338
                                return -1;
 
2339
                        }
 
2340
                        else {
 
2341
                                agrep_outbuffer[agrep_outpointer++] = ':';
 
2342
                                agrep_outbuffer[agrep_outpointer++] = nextchar;
 
2343
                        }
 
2344
                }
 
2345
 
 
2346
                NEW_FILE = OFF;
 
2347
                PRINTED = 1;
 
2348
        }
 
2349
        bp = i-1;
 
2350
        while ((buffer[bp] != '\n') && (bp > 0)) bp--;
 
2351
        if(LINENUM) {
 
2352
                if (agrep_finalfp != NULL)
 
2353
                        fprintf(agrep_finalfp, "%d: ", j-1); 
 
2354
                else {
 
2355
                        char s[32];
 
2356
                        int  outindex;
 
2357
 
 
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];
 
2362
                        }
 
2363
                        if (s[outindex] != '\0') {
 
2364
                                OUTPUT_OVERFLOW;
 
2365
                                return -1;
 
2366
                        }
 
2367
                        agrep_outpointer += outindex;
 
2368
                }
 
2369
                PRINTED = 1;
 
2370
        }
 
2371
 
 
2372
        if(BYTECOUNT) {
 
2373
                if (agrep_finalfp != NULL)
 
2374
                        fprintf(agrep_finalfp, "%d= ", CurrentByteOffset);
 
2375
                else {
 
2376
                        char s[32];
 
2377
                        int  outindex;
 
2378
                        sprintf(s, "%d= ", CurrentByteOffset);
 
2379
                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
2380
                                        (s[outindex] != '\0'); outindex++) {
 
2381
                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
2382
                        }
 
2383
                        if (s[outindex] != '\0') {
 
2384
                                OUTPUT_OVERFLOW;
 
2385
                                return -1;
 
2386
                        }
 
2387
                        agrep_outpointer += outindex;
 
2388
                }
 
2389
                PRINTED = 1;
 
2390
        }
 
2391
 
 
2392
        if(buffer[bp] != '\n') bp = Maxline-1;
 
2393
        bp++;
 
2394
 
 
2395
        if (PRINTOFFSET) {
 
2396
                if (agrep_finalfp != NULL)
 
2397
                        fprintf(agrep_finalfp, "@%d{%d} ", CurrentByteOffset - (i-bp), i-bp);
 
2398
                else {
 
2399
                        char s[32];
 
2400
                        int outindex;
 
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];
 
2405
                        }
 
2406
                        if (s[outindex] != '\0') {
 
2407
                                OUTPUT_OVERFLOW;
 
2408
                                return -1;
 
2409
                        }
 
2410
                        agrep_outpointer += outindex;
 
2411
                }
 
2412
                PRINTED = 1;
 
2413
        }
 
2414
        if (PRINTRECORD) {
 
2415
        if (agrep_finalfp != NULL)
 
2416
                while(bp <= i) fputc(buffer[bp++], agrep_finalfp);
 
2417
        else {
 
2418
                if (i - bp + 1 + agrep_outpointer >= agrep_outlen) {
 
2419
                        OUTPUT_OVERFLOW;
 
2420
                        return -1;
 
2421
                }
 
2422
                while(bp <= i) agrep_outbuffer[agrep_outpointer ++] = buffer[bp++];
 
2423
        }
 
2424
        }
 
2425
        else if (PRINTED) {
 
2426
                if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);
 
2427
                else agrep_outbuffer[agrep_outpointer ++] = '\n';
 
2428
                PRINTED = 0;
 
2429
        }
 
2430
 
 
2431
        return 0;
 
2432
}
 
2433
 
 
2434
/*
 
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.
 
2440
 */
 
2441
int
 
2442
agrep_init(argc, argv, initialfd, pattern_len, pattern_buffer)
 
2443
        int     argc;
 
2444
        char    *argv[];
 
2445
        int     initialfd;
 
2446
        int     pattern_len;
 
2447
        CHAR    *pattern_buffer;
 
2448
{
 
2449
        int i, j, seenlsq = 0;
 
2450
        char c, *p;
 
2451
        int filetype;
 
2452
        char **original_argv = argv;
 
2453
        char *home;
 
2454
        int quitwhile;
 
2455
        int NOOUTTAIL=OFF;
 
2456
 
 
2457
        initial_value();
 
2458
        if (pattern_len < 1) {
 
2459
                fprintf(stderr, "agrep_init: pattern length %d too small\n", pattern_len);
 
2460
                errno = 3;
 
2461
                return -1;
 
2462
        }
 
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.
 
2469
 
 
2470
                           This bug is regarded as COMPILER-UNSPECIFIC.
 
2471
 
 
2472
                           For sure, the problem SHOULD BE FIXED somewhere
 
2473
                           else in AGREP, later.
 
2474
 
 
2475
                           [TG] 16.09.96
 
2476
                           Thomas Gries  gries@epo.e-mail.com, gries@ibm.net
 
2477
 
 
2478
                        */
 
2479
 
 
2480
 
 
2481
 
 
2482
 
 
2483
        Pattern[0] = '\0';
 
2484
 
 
2485
        while(--argc > 0 && (*++argv)[0] == '-') { /* argv is incremented automatically here */
 
2486
                p = argv[0]+1;                       /* ptr to first character after '-' */
 
2487
                c = *(argv[0]+1); 
 
2488
                quitwhile = OFF;
 
2489
                while(!quitwhile && (*p != '\0')) {
 
2490
                        c = *p;
 
2491
                        switch(c) {
 
2492
                        case 'z' :
 
2493
                                NOOUTPUTZERO = ON;      /* don't output files with 0 matches */
 
2494
                                PRINT(printf("z\n");
 
2495
                                )
 
2496
                                        break;
 
2497
 
 
2498
                        case 'c' : 
 
2499
                                COUNT = ON;    /* output the # of matches */
 
2500
                                PRINT(printf("c\n");
 
2501
                                )
 
2502
                                        break;
 
2503
 
 
2504
                        case 's' : 
 
2505
                                SILENT = ON;   /* silent mode  */
 
2506
                                PRINT(printf("s\n");
 
2507
                                )
 
2508
                                        break;
 
2509
 
 
2510
                        case 'p' : 
 
2511
                                I = 0;         /* insertion cost is 0 */
 
2512
                                PRINT(printf("p\n");
 
2513
                                )
 
2514
                                        break; 
 
2515
                        case 'P' :
 
2516
                                PRINTPATTERN = 1;       /* print pattern before every matched line */
 
2517
                                PRINT(printf("p\n");
 
2518
                                )
 
2519
                                        break;
 
2520
 
 
2521
                        case 'x' : 
 
2522
                                WHOLELINE = ON;  /* match the whole line */
 
2523
                                PRINT(printf("x\n");
 
2524
                                )
 
2525
                                        if(WORDBOUND) {
 
2526
                                                fprintf(stderr, "%s: illegal option combination (-x and -w)\n", Progname);
 
2527
                                                if (!EXITONERROR) {
 
2528
                                                        errno = AGREP_ERROR;
 
2529
                                                        return -1;
 
2530
                                                }
 
2531
                                                else exit(2);
 
2532
                                        }
 
2533
                                break;
 
2534
 
 
2535
                        case 'b' : 
 
2536
                                BYTECOUNT = ON;
 
2537
                                PRINT(printf("b\n");
 
2538
                                )
 
2539
                                break;
 
2540
 
 
2541
                        case 'q' :
 
2542
                                PRINTOFFSET = ON;
 
2543
                                PRINT(printf("q\n");
 
2544
                                )
 
2545
                                break;
 
2546
 
 
2547
                        case 'u' :
 
2548
                                PRINTRECORD = OFF;
 
2549
                                PRINT(printf("u\n");
 
2550
                                )
 
2551
                                break;
 
2552
 
 
2553
                        case 'X' :
 
2554
                                PRINTNONEXISTENTFILE = ON;
 
2555
                                PRINT(printf("X\n");
 
2556
                                )
 
2557
                                break;
 
2558
 
 
2559
                        case 'g' :
 
2560
                                PRINTFILENUMBER = ON;
 
2561
                                PRINT(printf("g\n");
 
2562
                                )
 
2563
                                break;
 
2564
 
 
2565
                        case 'j' :
 
2566
                                PRINTFILETIME = ON;
 
2567
                                PRINT(printf("@\n");
 
2568
                                )
 
2569
                                break;
 
2570
 
 
2571
                        case 'L' :
 
2572
                                if ( *(p + 1) == '\0') {/* space after -L option */
 
2573
                                        if(argc <= 1) {
 
2574
                                                fprintf(stderr, "%s: the -L option must have an output-limit argument\n", Progname);
 
2575
                                                if (!EXITONERROR) {
 
2576
                                                        errno = AGREP_ERROR;
 
2577
                                                        return -1;
 
2578
                                                }
 
2579
                                                else exit(2);
 
2580
                                        }
 
2581
                                        argv++;
 
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]);
 
2586
                                                if (!EXITONERROR) {
 
2587
                                                        errno = AGREP_ERROR;
 
2588
                                                        return -1;
 
2589
                                                }
 
2590
                                                else exit(2);
 
2591
                                        }
 
2592
                                        argc--;
 
2593
                                } 
 
2594
                                else {
 
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);
 
2599
                                                if (!EXITONERROR) {
 
2600
                                                        errno = AGREP_ERROR;
 
2601
                                                        return -1;
 
2602
                                                }
 
2603
                                                else exit(2);
 
2604
                                        }
 
2605
                                } /* else */
 
2606
                                PRINT(printf("L\n");
 
2607
                                )
 
2608
                                quitwhile = ON;
 
2609
                                break;
 
2610
 
 
2611
                        case 'd' : 
 
2612
                                DELIMITER = ON;  /* user defines delimiter */
 
2613
                                PRINT(printf("d\n");
 
2614
                                )
 
2615
                                if ( *(p + 1) == '\0') {/* space after -d option */
 
2616
                                        if(argc <= 1) {
 
2617
                                                fprintf(stderr, "%s: the -d option must have a delimiter argument\n", Progname);
 
2618
                                                if (!EXITONERROR) {
 
2619
                                                        errno = AGREP_ERROR;
 
2620
                                                        return -1;
 
2621
                                                }
 
2622
                                                else exit(2);
 
2623
                                        }
 
2624
                                        argv++;
 
2625
                                        if ((D_length = strlen(argv[0])) > MaxDelimit) {
 
2626
                                                fprintf(stderr, "%s: delimiter pattern too long (has > %d chars)\n", Progname, MaxDelimit);
 
2627
                                                if (!EXITONERROR) {
 
2628
                                                        errno = AGREP_ERROR;
 
2629
                                                        return -1;
 
2630
                                                }
 
2631
                                                else exit(2);
 
2632
                                        }
 
2633
                                        D_pattern[0] = '<';
 
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))
 
2636
                                                OUTTAIL = ON;
 
2637
                                        argc--;
 
2638
                                        PRINT(printf("space\n");
 
2639
                                        )
 
2640
                                } 
 
2641
                                else {
 
2642
                                        if ((D_length = strlen(p + 1)) > MaxDelimit) {
 
2643
                                                fprintf(stderr, "%s: delimiter pattern too long (has > %d chars)\n", Progname, MaxDelimit);
 
2644
                                                if (!EXITONERROR) {
 
2645
                                                        errno = AGREP_ERROR;
 
2646
                                                        return -1;
 
2647
                                                }
 
2648
                                                else exit(2);
 
2649
                                        }
 
2650
                                        D_pattern[0] = '<';
 
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))
 
2653
                                                OUTTAIL = ON;
 
2654
                                } /* else */
 
2655
                                strcat(D_pattern, ">; ");
 
2656
                                D_length++;   /* to count '<' as one */
 
2657
                                PRINT(printf("D_pattern=%s\n", D_pattern);
 
2658
                                )
 
2659
                                strcpy(original_D_pattern, D_pattern);
 
2660
                                original_D_length = D_length;
 
2661
                                quitwhile = ON;
 
2662
                                break;
 
2663
 
 
2664
                        case 'H':
 
2665
                                if (*(p + 1) == '\0') {/* space after - option */
 
2666
                                        if (argc <= 1) {
 
2667
                                                fprintf(stderr, "%s: a directory name must follow the -H option\n", Progname);
 
2668
                                                if (!EXITONERROR) {
 
2669
                                                        errno = AGREP_ERROR;
 
2670
                                                        return agrep_usage();
 
2671
                                                }
 
2672
                                                else exit(2);
 
2673
                                        }
 
2674
                                        argv ++;
 
2675
                                        strcpy(COMP_DIR, argv[0]);
 
2676
                                        argc --;
 
2677
                                }
 
2678
                                else {
 
2679
                                        strcpy(COMP_DIR, p+1);
 
2680
                                }
 
2681
                                quitwhile = ON;
 
2682
                                break;
 
2683
 
 
2684
                        case 'e' : 
 
2685
                                if ( *(p + 1) == '\0') {/* space after -e option */
 
2686
                                        if(argc <= 1) {
 
2687
                                                fprintf(stderr, "%s: the -e option must have a pattern argument\n", Progname);
 
2688
                                                if (!EXITONERROR) {
 
2689
                                                        errno = AGREP_ERROR;
 
2690
                                                        return -1;
 
2691
                                                }
 
2692
                                                else exit(2);
 
2693
                                        }
 
2694
                                        argv++;
 
2695
                                        if(argv[0][0] == '-') { /* not strictly necessary but no harm done */
 
2696
                                                Pattern[0] = '\\';
 
2697
                                                strcat(Pattern, (argv)[0]);
 
2698
                                        }
 
2699
                                        else strcat(Pattern, argv[0]);
 
2700
                                        argc--;
 
2701
                                }
 
2702
                                else {
 
2703
                                        if (*(p+1) == '-') {    /* not strictly necessary but no harm done */
 
2704
                                                Pattern[0] = '\\';
 
2705
                                                strcat(Pattern, p+1);
 
2706
                                        }
 
2707
                                        else strcat (Pattern, p+1);
 
2708
                                } /* else */
 
2709
 
 
2710
                                PRINT(printf("Pattern=%s\n", Pattern);
 
2711
                                )
 
2712
                                pattern_index = abs(argv - original_argv);
 
2713
                                quitwhile = ON;
 
2714
                                break;
 
2715
 
 
2716
                        case 'k' : 
 
2717
                                CONSTANT = ON;
 
2718
                                if ( *(p + 1) == '\0') {/* space after -e option */
 
2719
                                        if(argc <= 1) {
 
2720
                                                fprintf(stderr, "%s: the -k option must have a pattern argument\n", Progname);
 
2721
                                                if (!EXITONERROR) {
 
2722
                                                        errno = AGREP_ERROR;
 
2723
                                                        return -1;
 
2724
                                                }
 
2725
                                                else exit(2);
 
2726
                                        }
 
2727
                                        argv++;
 
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);
 
2731
                                                if (!EXITONERROR) {
 
2732
                                                        errno = AGREP_ERROR;
 
2733
                                                        return -1;
 
2734
                                                }
 
2735
                                                else exit(2);
 
2736
                                        }
 
2737
                                        argc--;
 
2738
                                }
 
2739
                                else {
 
2740
                                        if((argc > 1) && (argv[1][0] == '-')) {
 
2741
                                                fprintf(stderr, "%s: -k should be the last option in the command\n", Progname);
 
2742
                                                if (!EXITONERROR) {
 
2743
                                                        errno = AGREP_ERROR;
 
2744
                                                        return -1;
 
2745
                                                }
 
2746
                                                else exit(2);
 
2747
                                        }
 
2748
                                        strcat (Pattern, p+1);
 
2749
                                } /* else */
 
2750
 
 
2751
                                pattern_index = abs(argv - original_argv);
 
2752
                                quitwhile = ON;
 
2753
                                break;
 
2754
 
 
2755
                        case 'f' : 
 
2756
                                if (PAT_FILE == ON) {
 
2757
                                        fprintf(stderr, "%s: multiple -f options\n", Progname);
 
2758
                                        if (multifd >= 0) close(multifd);
 
2759
                                        if (!EXITONERROR) {
 
2760
                                                errno = AGREP_ERROR;
 
2761
                                                return -1;
 
2762
                                        }
 
2763
                                        else exit(2);
 
2764
                                }
 
2765
                                if (PAT_BUFFER == ON) {
 
2766
                                        fprintf(stderr, "%s: -f and -m are incompatible\n", Progname);
 
2767
                                        if (multibuf != NULL) free(multibuf);
 
2768
                                        multibuf = NULL;
 
2769
                                        multilen = 0;
 
2770
                                        if (!EXITONERROR) {
 
2771
                                                errno = AGREP_ERROR;
 
2772
                                                return -1;
 
2773
                                        }
 
2774
                                        else exit(2);
 
2775
                                }
 
2776
                                PAT_FILE = ON;
 
2777
                                PRINT(printf("f\n");
 
2778
                                )
 
2779
                                argv++;
 
2780
                                argc--;
 
2781
 
 
2782
                                if (argv[0] == NULL) {
 
2783
                                        /* A -f option with a NULL file name is a NO-OP: stupid, but simplifies glimpse :-) */
 
2784
                                        PAT_FILE = OFF;
 
2785
                                        quitwhile = ON;
 
2786
                                        break;
 
2787
                                }
 
2788
 
 
2789
                                if((multifd = open(argv[0], O_RDONLY)) < 0) {
 
2790
                                        PAT_FILE = OFF;
 
2791
                                        fprintf(stderr, "%s: can't open pattern file for reading: %s\n", Progname, argv[0]);
 
2792
                                        if (!EXITONERROR) {
 
2793
                                                errno = AGREP_ERROR;
 
2794
                                                return -1;
 
2795
                                        }
 
2796
                                        else exit(2);
 
2797
                                }
 
2798
                                PRINT(printf("file=%s\n", argv[0]);
 
2799
                                )
 
2800
                                strcpy(PAT_FILE_NAME, argv[0]);
 
2801
                                if (prepf(multifd, NULL, 0) <= -1) {
 
2802
                                        close(multifd);
 
2803
                                        PAT_FILE = OFF;
 
2804
                                        fprintf(stderr, "%s: error in processing pattern file: %s\n", Progname, argv[0]);
 
2805
                                        if (!EXITONERROR) {
 
2806
                                                errno = AGREP_ERROR;
 
2807
                                                return -1;
 
2808
                                        }
 
2809
                                        else exit(2);
 
2810
                                }
 
2811
                                quitwhile = ON;
 
2812
                                break;
 
2813
 
 
2814
                        case 'm' :
 
2815
                                if (PAT_BUFFER == ON) {
 
2816
                                        fprintf(stderr, "%s: multiple -m options\n", Progname);
 
2817
                                        if (multibuf != NULL) free(multibuf);
 
2818
                                        multibuf = NULL;
 
2819
                                        multilen = 0;
 
2820
                                        if (!EXITONERROR) {
 
2821
                                                errno = AGREP_ERROR;
 
2822
                                                return -1;
 
2823
                                        }
 
2824
                                        else exit(2);
 
2825
                                }
 
2826
                                if (PAT_FILE == ON) {
 
2827
                                        fprintf(stderr, "%s: -f and -m are incompatible\n", Progname);
 
2828
                                        if (multifd >= 0) close(multifd);
 
2829
                                        if (!EXITONERROR) {
 
2830
                                                errno = AGREP_ERROR;
 
2831
                                                return -1;
 
2832
                                        }
 
2833
                                        else exit(2);
 
2834
                                }
 
2835
                                PAT_BUFFER = ON;
 
2836
                                PRINT(printf("m\n");
 
2837
                                )
 
2838
                                argv ++;
 
2839
                                argc --;
 
2840
 
 
2841
 
 
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 :-) */
 
2844
                                        PAT_BUFFER = OFF;
 
2845
                                        if (multibuf != NULL) free(multibuf);
 
2846
                                        multilen = 0;
 
2847
                                        multibuf = NULL;
 
2848
                                }
 
2849
                                else {
 
2850
                                        multibuf = (char *)malloc(multilen + 2);
 
2851
                                        strcpy(multibuf, argv[0]);
 
2852
                                        PRINT(printf("patterns=%s\n", multibuf);
 
2853
                                        )
 
2854
                                        if (prepf(-1, multibuf, multilen) <= -1) {
 
2855
                                                free(multibuf);
 
2856
                                                multibuf = NULL;
 
2857
                                                multilen = 0;
 
2858
                                                PAT_BUFFER = OFF;
 
2859
                                                fprintf(stderr, "%s: error in processing pattern buffer\n", Progname);
 
2860
                                                if (!EXITONERROR) {
 
2861
                                                        errno = AGREP_ERROR;
 
2862
                                                        return -1;
 
2863
                                                }
 
2864
                                                else exit(2);
 
2865
                                        }
 
2866
                                }
 
2867
                                quitwhile = ON;
 
2868
                                break;
 
2869
 
 
2870
                        case 'h' : 
 
2871
                                NOFILENAME = ON;
 
2872
                                PRINT(printf("h\n");
 
2873
                                )
 
2874
                                        break;
 
2875
 
 
2876
                        case 'i' : 
 
2877
                                NOUPPER = ON;
 
2878
                                PRINT(printf("i\n");
 
2879
                                )
 
2880
                                        break;
 
2881
 
 
2882
                        case 'l' : 
 
2883
                                FILENAMEONLY = ON;
 
2884
                                PRINT(printf("l\n");
 
2885
                                )
 
2886
                                        break;
 
2887
 
 
2888
                        case 'n' : 
 
2889
                                LINENUM = ON;  /* output prefixed by line no*/
 
2890
                                PRINT(printf("n\n");
 
2891
                                )
 
2892
                                        break;
 
2893
 
 
2894
                        case 'r' : 
 
2895
                                RECURSIVE = ON;
 
2896
                                PRINT(printf("r\n");
 
2897
                                )
 
2898
                                        break;
 
2899
                        
 
2900
                        case 'V' :
 
2901
                                printf("\nThis is agrep version %s, %s.\n\n", AGREP_VERSION, AGREP_DATE);
 
2902
                                return 0;
 
2903
 
 
2904
                        case 'v' : 
 
2905
                                INVERSE = ON;  /* output no-matched lines */
 
2906
                                PRINT(printf("v\n");
 
2907
                                )
 
2908
                                        break;
 
2909
 
 
2910
                        case 't' : 
 
2911
                                OUTTAIL = ON;  /* output from tail of delimiter */
 
2912
                                PRINT(printf("t\n");
 
2913
                                )
 
2914
                                        break;
 
2915
 
 
2916
                        case 'o' : 
 
2917
                                NOOUTTAIL = ON;  /* output from front of delimiter */
 
2918
                                PRINT(printf("t\n");
 
2919
                                )
 
2920
                                        break;
 
2921
 
 
2922
                        case 'B' : 
 
2923
                                BESTMATCH = ON;
 
2924
                                PRINT(printf("B\n");
 
2925
                                )
 
2926
                                        break;
 
2927
 
 
2928
                        case 'w' : 
 
2929
                                WORDBOUND = ON;/* match to words */
 
2930
                                PRINT(printf("w\n");
 
2931
                                )
 
2932
                                        if(WHOLELINE) {
 
2933
                                                fprintf(stderr, "%s: illegal option combination (-w and -x)\n", Progname);
 
2934
                                                if (!EXITONERROR) {
 
2935
                                                        errno = AGREP_ERROR;
 
2936
                                                        return -1;
 
2937
                                                }
 
2938
                                                else exit(2);
 
2939
                                        }
 
2940
                                break;
 
2941
 
 
2942
                        case 'y' : 
 
2943
                                NOPROMPT = ON;
 
2944
                                PRINT(printf("y\n");
 
2945
                                )
 
2946
                                        break;
 
2947
 
 
2948
                        case 'I' : 
 
2949
                                I = atoi(p + 1);  /* Insertion Cost */
 
2950
                                JUMP = ON;
 
2951
                                quitwhile = ON;
 
2952
                                break;
 
2953
 
 
2954
                        case 'S' : 
 
2955
                                S = atoi(p + 1);  /* Substitution Cost */
 
2956
                                JUMP = ON;
 
2957
                                quitwhile = ON;
 
2958
                                break;
 
2959
 
 
2960
                        case 'D' : 
 
2961
                                DD = atoi(p + 1); /* Deletion Cost */
 
2962
                                JUMP = ON;
 
2963
                                quitwhile = ON;
 
2964
                                break;
 
2965
 
 
2966
                        case 'G' : 
 
2967
                                FILEOUT = ON; 
 
2968
                                COUNT = ON;
 
2969
                                break;
 
2970
 
 
2971
                        case 'A':
 
2972
                                ALWAYSFILENAME = ON;
 
2973
                                break;
 
2974
 
 
2975
                        case 'O':
 
2976
                                POST_FILTER = ON;
 
2977
                                break;
 
2978
 
 
2979
                        case 'M':
 
2980
                                MULTI_OUTPUT = ON;
 
2981
                                break;
 
2982
 
 
2983
                        case 'Z': break;        /* no-op: used by glimpse */
 
2984
                        
 
2985
                        default  : 
 
2986
                                if (isdigit(c)) {
 
2987
                                        APPROX = ON;
 
2988
                                        D = atoi(p);
 
2989
                                        if (D > MaxError) {
 
2990
                                                fprintf(stderr,"%s: the maximum number of errors is %d\n", Progname, MaxError);
 
2991
                                                if (!EXITONERROR) {
 
2992
                                                        errno = AGREP_ERROR;
 
2993
                                                        return -1;
 
2994
                                                }
 
2995
                                                else exit(2);
 
2996
                                        }
 
2997
                                        quitwhile = ON; /* note that even a number should occur at the end of a group of options, as f & e */
 
2998
                                }
 
2999
                                else {
 
3000
                                        fprintf(stderr, "%s: illegal option  -%c\n",Progname, c);
 
3001
                                        return agrep_usage();
 
3002
                                }
 
3003
 
 
3004
                        } /* switch(c) */
 
3005
                        p ++;
 
3006
                }
 
3007
        } /* while (--argc > 0 && (*++argv)[0] == '-') */
 
3008
 
 
3009
        if (NOOUTTAIL == ON) OUTTAIL = OFF;
 
3010
 
 
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);
 
3015
                }
 
3016
                else strncpy(COMP_DIR, home, MAX_LINE_LEN);
 
3017
        }
 
3018
 
 
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 */
 
3029
 
 
3030
        if (FILENAMEONLY && NOFILENAME) {
 
3031
                fprintf(stderr, "%s: -h and -l options are mutually exclusive\n",Progname);
 
3032
        }
 
3033
        if (COUNT && (FILENAMEONLY || NOFILENAME)) {
 
3034
                FILENAMEONLY = OFF; 
 
3035
                if(!FILEOUT) NOFILENAME = OFF;
 
3036
        }
 
3037
 
 
3038
        if (SILENT) {
 
3039
                FILEOUT = 0;
 
3040
                NOFILENAME = 1;
 
3041
                PRINTRECORD = 0;
 
3042
                FILENAMEONLY = 0;
 
3043
                PRINTFILETIME = 0;
 
3044
                BYTECOUNT = 0;
 
3045
                PRINTOFFSET = 0;
 
3046
        }
 
3047
 
 
3048
        if (!(PAT_FILE || PAT_BUFFER) && Pattern[0] == '\0') { /* Pattern not set with -e option */
 
3049
                if (argc <= 0) {
 
3050
                        agrep_usage();
 
3051
                        return 0;
 
3052
                }
 
3053
                strcpy(Pattern, *argv); 
 
3054
                pattern_index = abs(argv - original_argv);
 
3055
                argc--;
 
3056
                argv++;
 
3057
        }
 
3058
        /* if multi-pattern search, just ignore any specified pattern altogether: treat it as a filename */
 
3059
 
 
3060
        if (copied_from_argv) {
 
3061
                for (i=0; i<Numfiles; i++) free(Textfiles[i]);
 
3062
                if (Textfiles != NULL) free(Textfiles);
 
3063
        }
 
3064
        copied_from_argv = 0;
 
3065
        Numfiles = 0; Textfiles = NULL;
 
3066
        execfd = agrep_initialfd;
 
3067
        if (argc <= 0)  /* check Pattern against stdin */
 
3068
                execfd = 0;
 
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__);
 
3072
                        if (!EXITONERROR) {
 
3073
                                errno = AGREP_ERROR;
 
3074
                                return 0;
 
3075
                        }
 
3076
                        else exit(2);
 
3077
                }
 
3078
                copied_from_argv = 1;   /* should I free Textfiles next time? */
 
3079
                while (argc--)
 
3080
                {       /* one or more filenames on command line -- put the valid filenames in a array of strings */
 
3081
 
 
3082
                        if (!glimpse_call && ((filetype = check_file(*argv)) == NOSUCHFILE) && !PRINTNONEXISTENTFILE) {
 
3083
                                fprintf(stderr,"%s: '%s' no such file or directory\n",Progname,*argv);
 
3084
                                argv++;
 
3085
                        }
 
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__);
 
3089
                                        if (!EXITONERROR) {
 
3090
                                                errno = AGREP_ERROR;
 
3091
                                                return 0;
 
3092
                                        }
 
3093
                                        else exit(2);
 
3094
                                }
 
3095
                                strcpy(Textfiles[Numfiles++], *argv++);
 
3096
                        } /* else */
 
3097
                } /* while (argc--) */
 
3098
                if (Numfiles <= 0) return 0;
 
3099
                /* If user specified wrong filenames, then we quit rather than taking input from stdin! */
 
3100
        } /* else */
 
3101
 
 
3102
        M = strlen(Pattern);
 
3103
        if (M<=0) return 0;
 
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");
 
3107
                        break;
 
3108
                }
 
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 - */
 
3115
                        M ++;
 
3116
                        i++;
 
3117
                }
 
3118
                else if (Pattern[i] == ']') seenlsq = 0;
 
3119
        }
 
3120
 
 
3121
        if (M > pattern_len - 1) {
 
3122
                fprintf(stderr, "%s: pattern '%s' does not fit in specified buffer\n", Progname, Pattern);
 
3123
                errno = 3;
 
3124
                return 0;
 
3125
        }
 
3126
        if (pattern_buffer != Pattern)  /* not from mem/file-agrep() */
 
3127
                strncpy(pattern_buffer, Pattern, M+1); /* copy \0 */
 
3128
        return M;
 
3129
}
 
3130
 
 
3131
/*
 
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.
 
3135
 */
 
3136
 
 
3137
int
 
3138
memagrep_init(argc, argv, pattern_len, pattern_buffer)
 
3139
        int     argc;
 
3140
        char    *argv[];
 
3141
        int     pattern_len;
 
3142
        char    *pattern_buffer;
 
3143
{
 
3144
        return (agrep_init(argc, argv, -1, pattern_len, pattern_buffer));
 
3145
}
 
3146
 
 
3147
int
 
3148
fileagrep_init(argc, argv, pattern_len, pattern_buffer)
 
3149
        int     argc;
 
3150
        char    *argv[];
 
3151
        int     pattern_len;
 
3152
        char    *pattern_buffer;
 
3153
{
 
3154
        return (agrep_init(argc, argv, 3, pattern_len, pattern_buffer));
 
3155
}
 
3156
 
 
3157
/* returns -1 on error, num of matches (>=0) otherwise */
 
3158
int
 
3159
agrep_search(pattern_len, pattern_buffer, initialfd, input_len, input, output_len, output)
 
3160
int pattern_len; 
 
3161
CHAR *pattern_buffer;
 
3162
int initialfd;
 
3163
int input_len;
 
3164
void *input;
 
3165
int output_len;
 
3166
void *output;
 
3167
{
 
3168
        int     i;
 
3169
        int     filetype;
 
3170
        int     ret;
 
3171
        int     pattern_has_changed = 1;
 
3172
 
 
3173
        if ((multifd == -1) && (multibuf == NULL) && (pattern_len < 1)) {
 
3174
                fprintf(stderr, "%s: pattern length %d too small\n", Progname, pattern_len);
 
3175
                errno = 3;
 
3176
                return -1;
 
3177
        }
 
3178
        if (pattern_len >= MAXPAT) {
 
3179
                fprintf(stderr, "%s: pattern '%s' too long\n", Progname, pattern_buffer);
 
3180
                errno = 3;
 
3181
                return -1;
 
3182
        }
 
3183
 
 
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;
 
3189
                }
 
3190
                else {
 
3191
                        pattern_has_changed = 0;
 
3192
                }
 
3193
        }
 
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';
 
3198
 
 
3199
        }
 
3200
        if (!pattern_has_changed) {
 
3201
                reinit_value_partial();
 
3202
        }
 
3203
        else {
 
3204
                reinit_value();
 
3205
                if (pattern_buffer != Pattern)  /* not from mem/file-agrep() */
 
3206
                        strncpy(Pattern, pattern_buffer, pattern_len+1); /* copy \0 */
 
3207
                M = strlen(Pattern);
 
3208
        }
 
3209
 
 
3210
        if (output == NULL) {
 
3211
                fprintf(stderr, "%s: invalid output descriptor\n", Progname);
 
3212
                return -1;
 
3213
        }
 
3214
        if (output_len <= 0) {
 
3215
                agrep_finalfp = (FILE *)output;
 
3216
                agrep_outlen = 0;
 
3217
                agrep_outbuffer = NULL;
 
3218
                agrep_outpointer = 0;
 
3219
        }
 
3220
        else {
 
3221
                agrep_finalfp = NULL;
 
3222
                agrep_outlen = output_len;
 
3223
                agrep_outbuffer = (CHAR *)output;
 
3224
                agrep_outpointer = 0;
 
3225
        }
 
3226
 
 
3227
        agrep_initialfd = initialfd;
 
3228
        execfd = initialfd;
 
3229
        if (initialfd == -1) {
 
3230
                agrep_inbuffer = (CHAR *)input;
 
3231
                agrep_inlen = input_len;
 
3232
                agrep_inpointer = 0;
 
3233
        }
 
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);
 
3239
                }
 
3240
                copied_from_argv = 0;
 
3241
                Numfiles = 0; Textfiles = NULL;
 
3242
#if     0
 
3243
                if (!(Textfiles = (CHAR **)malloc(input_len * sizeof(CHAR *) ))) {
 
3244
                        fprintf(stderr, "%s: malloc failure in %s:%d\n", Progname, __FILE__, __LINE__);
 
3245
                        if (!EXITONERROR) {
 
3246
                                errno = AGREP_ERROR;
 
3247
                                return 0;
 
3248
                        }
 
3249
                        else exit(2);
 
3250
                }
 
3251
#else   /*0*/
 
3252
                Textfiles = (CHAR **)input;
 
3253
#endif  /*0*/
 
3254
 
 
3255
                while (input_len --)
 
3256
                {       /* one or more filenames on command line -- put the valid filenames in a array of strings */
 
3257
 
 
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 *));
 
3261
                        } 
 
3262
                        else { /* file is ascii*/
 
3263
#if     0
 
3264
                                if (!(Textfiles[Numfiles] = (CHAR *)malloc((strlen(*(char **)input)+2)))) {
 
3265
                                        fprintf(stderr, "%s: malloc failure in %s:%d\n", Progname, __FILE__, __LINE__);
 
3266
                                        if (!EXITONERROR) {
 
3267
                                                errno = AGREP_ERROR;
 
3268
                                                return 0;
 
3269
                                        }
 
3270
                                        else exit(2);
 
3271
                                }
 
3272
                                strcpy(Textfiles[Numfiles++], *((char **)input));
 
3273
#else   /*0*/
 
3274
                                Textfiles[Numfiles++] = *((CHAR **)input);
 
3275
#endif  /*0*/
 
3276
                                input = (void *)(((long)input) + sizeof(char *));
 
3277
                        } /* else */
 
3278
                } /* while (argc--) */
 
3279
                if (Numfiles <= 0) return 0;
 
3280
                /* If user specified wrong filenames, then we quit rather than taking input from stdin! */
 
3281
        }
 
3282
        else if (Numfiles <= 0) execfd = 0;
 
3283
        /* the old set of files (agrep_init) are used */
 
3284
 
 
3285
#if     0
 
3286
        printf("agrep_search: pat=%s buf=%s\n", Pattern, ((agrep_initialfd == -1) && (agrep_inlen < 20)) ? agrep_inbuffer : NULL);
 
3287
#endif  /*0*/
 
3288
 
 
3289
        if (pattern_has_changed) {
 
3290
                if (-1 == checksg(Pattern, D, 1)) return -1;       /* check if the pattern is simple */
 
3291
                strcpy(OldPattern, Pattern);
 
3292
 
 
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;
 
3298
                }
 
3299
                else {  /* sgrep too can handle delimiters */
 
3300
                        if (DELIMITER) {
 
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;
 
3307
                                }
 
3308
                                /* printf("sgrep's delim=%s,%d tc_delim=%s,%d\n", D_pattern, D_length, tc_D_pattern, tc_D_length); */
 
3309
                        }
 
3310
                        M = strlen(OldPattern);
 
3311
                }
 
3312
        }
 
3313
 
 
3314
        if (AParse)  {  /* boolean converted to multi-pattern search */
 
3315
                int prepf_ret= 0;
 
3316
                if (pattern_has_changed)
 
3317
                        prepf_ret= prepf(-1, multibuf, multilen);
 
3318
                if (prepf_ret  <= -1) {
 
3319
                        if (AComplexBoolean) destroy_tree(AParse);
 
3320
                        AParse = 0;
 
3321
                        PAT_BUFFER = 0;
 
3322
                        if (multibuf != NULL) free(multibuf);   /* this was allocated for arbit booleans, not multipattern search */
 
3323
                        multibuf = NULL;
 
3324
                        multilen = 0;
 
3325
                        /* Cannot free multifd here since that is always allocated for multipattern search */
 
3326
                        return -1;
 
3327
                }
 
3328
        }
 
3329
        if (Numfiles > 1) FNAME = ON;
 
3330
        if (NOFILENAME) FNAME = 0;
 
3331
        if (ALWAYSFILENAME) FNAME = ON; /* used by glimpse ONLY: 15/dec/93 */
 
3332
 
 
3333
        if (agrep_initialfd == -1) ret = exec(execfd, NULL);
 
3334
        else if(RECURSIVE) ret = (recursive(Numfiles, Textfiles));
 
3335
        else  ret = (exec(execfd, Textfiles));
 
3336
        return ret;
 
3337
}
 
3338
 
 
3339
/*
 
3340
 * User need not bother about initialfd.
 
3341
 * Both functions return -1 on error, 0 otherwise.
 
3342
 */
 
3343
 
 
3344
int
 
3345
memagrep_search(pattern_len, pattern_buffer, input_len, input_buffer, output_len, output)
 
3346
int pattern_len;
 
3347
char *pattern_buffer;
 
3348
int input_len;
 
3349
char *input_buffer;
 
3350
int output_len;
 
3351
void *output;
 
3352
{
 
3353
        return(agrep_search(pattern_len, pattern_buffer, -1, input_len, input_buffer, output_len, output));
 
3354
}
 
3355
 
 
3356
int
 
3357
fileagrep_search(pattern_len, pattern_buffer, file_num, file_buffer, output_len, output)
 
3358
int pattern_len;
 
3359
char *pattern_buffer;
 
3360
int file_num;
 
3361
char **file_buffer;
 
3362
int output_len;
 
3363
void *output;
 
3364
{
 
3365
        return(agrep_search(pattern_len, pattern_buffer, 3, file_num, file_buffer, output_len, output));
 
3366
}
 
3367
 
 
3368
/*
 
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).
 
3375
 *
 
3376
 * Both functions return -1 on error, 0 otherwise.
 
3377
 *
 
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.
 
3383
 */
 
3384
 
 
3385
int
 
3386
memagrep(argc, argv, input_len, input_buffer, output_len, output)
 
3387
int argc; 
 
3388
char *argv[];
 
3389
int input_len;
 
3390
char *input_buffer;
 
3391
int output_len;
 
3392
void *output;
 
3393
{
 
3394
        int     ret;
 
3395
 
 
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);
 
3400
}
 
3401
 
 
3402
int
 
3403
fileagrep(argc, argv, output_len, output)
 
3404
int argc; 
 
3405
char *argv[];
 
3406
int output_len;
 
3407
void *output;
 
3408
{
 
3409
        int     ret;
 
3410
 
 
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);
 
3415
}
 
3416
 
 
3417
/*
 
3418
 * RETURNS: total number of matched lines in all files that were searched.
 
3419
 *
 
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.
 
3424
 *
 
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.
 
3430
 *
 
3431
 * - bgopal on 15/Nov/93.
 
3432
 */
 
3433
int
 
3434
exec(fd, file_list)
 
3435
int fd;
 
3436
char **file_list;
 
3437
 
3438
        int i;
 
3439
        char c[8];
 
3440
        int ret = 0;    /* no error */
 
3441
 
 
3442
        if ((Numfiles > 1) && (NOFILENAME == OFF)) FNAME = ON;
 
3443
        if ((-1 == compat())) return -1; /* check compatibility between options */
 
3444
 
 
3445
        if (fd <= 0) {
 
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);
 
3450
                        if (!EXITONERROR) {
 
3451
                                errno = AGREP_ERROR;
 
3452
                                return -1;
 
3453
                        }
 
3454
                        else exit(2);  
 
3455
                }
 
3456
                if(PAT_FILE || PAT_BUFFER) mgrep(fd, AParse);
 
3457
                else {
 
3458
                        if(SGREP) ret = sgrep(OldPattern, strlen(OldPattern), fd, D, 0);
 
3459
                        else      ret = bitap(old_D_pat, Pattern, fd, M, D);
 
3460
                }
 
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));
 
3466
                                else {
 
3467
                                        char s[32];
 
3468
                                        int  outindex;
 
3469
 
 
3470
                                        sprintf(s, "%d\n", total_line-(num_of_matched - prev_num_of_matched));
 
3471
 
 
3472
                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
3473
                                                        (s[outindex] != '\0'); outindex++) {
 
3474
                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
3475
                                        }
 
3476
                                        if (s[outindex] != '\0') {
 
3477
                                                OUTPUT_OVERFLOW;
 
3478
                                                return -1;
 
3479
                                        }
 
3480
                                        agrep_outpointer += outindex;
 
3481
                                }
 
3482
                        }
 
3483
                        else {
 
3484
                                if (agrep_finalfp != NULL)
 
3485
                                        fprintf(agrep_finalfp, "%d\n", (num_of_matched - prev_num_of_matched));
 
3486
                                else {
 
3487
                                        char s[32];
 
3488
                                        int  outindex;
 
3489
 
 
3490
                                        sprintf(s, "%d\n", (num_of_matched - prev_num_of_matched));
 
3491
 
 
3492
                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
3493
                                                        (s[outindex] != '\0'); outindex++) {
 
3494
                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
3495
                                        }
 
3496
                                        if (s[outindex] != '\0') {
 
3497
                                                OUTPUT_OVERFLOW;
 
3498
                                                return -1;
 
3499
                                        }
 
3500
                                        agrep_outpointer += outindex;
 
3501
                                }
 
3502
                        }
 
3503
                }
 
3504
        }
 
3505
        else {
 
3506
                /* fd > 0 => Numfiles > 0 */
 
3507
 
 
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]);
 
3514
                        }
 
3515
                        if (!SetCurrentFileTime) {
 
3516
                                if (PRINTFILETIME) CurrentFileTime = aget_file_time(NULL, file_list[i]);
 
3517
                        }
 
3518
                        TCOMPRESSED = ON;
 
3519
                        if (!tuncompressible_filename(file_list[i], strlen(file_list[i]))) TCOMPRESSED = OFF;
 
3520
                        NEW_FILE = ON;
 
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]);
 
3524
                        } 
 
3525
                        else { 
 
3526
                                if(PAT_FILE || PAT_BUFFER) mgrep(fd, AParse);
 
3527
                                else {
 
3528
                                        if(SGREP) ret = sgrep(OldPattern, strlen(OldPattern), fd, D, i);
 
3529
                                        else      ret = bitap(old_D_pat, Pattern, fd, M, D);
 
3530
                                }
 
3531
                                if (ret <= -1) {
 
3532
                                        close(fd);
 
3533
                                        return -1;
 
3534
                                }
 
3535
                                if (num_of_matched - prev_num_of_matched > 0) {
 
3536
                                        NOMATCH = OFF;
 
3537
                                        files_matched ++;
 
3538
                                }
 
3539
 
 
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)) {
 
3543
 
 
3544
                                                        if (agrep_finalfp != NULL)
 
3545
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
3546
                                                        else {
 
3547
                                                                int outindex;
 
3548
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
3549
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
3550
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
3551
                                                                }
 
3552
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
3553
                                                                        OUTPUT_OVERFLOW;
 
3554
                                                                        close(fd);
 
3555
                                                                        return -1;
 
3556
                                                                }
 
3557
                                                                agrep_outpointer += outindex;
 
3558
                                                        }
 
3559
                                                        if (PRINTFILETIME) {
 
3560
                                                                char *s = aprint_file_time(CurrentFileTime);
 
3561
                                                                if (agrep_finalfp != NULL)
 
3562
                                                                        fprintf(agrep_finalfp, "%s", s);
 
3563
                                                                else {
 
3564
                                                                        int outindex;
 
3565
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
3566
                                                                                        (s[outindex] != '\0'); outindex++) {
 
3567
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
3568
                                                                        }
 
3569
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
3570
                                                                                OUTPUT_OVERFLOW;
 
3571
                                                                                close(fd);
 
3572
                                                                                return -1;
 
3573
                                                                        }
 
3574
                                                                        agrep_outpointer += outindex;
 
3575
                                                                }
 
3576
                                                        }
 
3577
                                                        if (agrep_finalfp != NULL)
 
3578
                                                                fprintf(agrep_finalfp, ": %d\n", total_line - (num_of_matched - prev_num_of_matched));
 
3579
                                                        else {
 
3580
                                                                char s[32];
 
3581
                                                                int  outindex;
 
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];
 
3586
                                                                }
 
3587
                                                                if (s[outindex] != '\0') {
 
3588
                                                                        OUTPUT_OVERFLOW;
 
3589
                                                                        close(fd);
 
3590
                                                                        return -1;
 
3591
                                                                }
 
3592
                                                                agrep_outpointer += outindex;
 
3593
                                                        }
 
3594
 
 
3595
                                                        NEW_FILE = OFF;
 
3596
                                                }
 
3597
                                                else if (!FNAME) {
 
3598
                                                        if (agrep_finalfp != NULL)
 
3599
                                                                fprintf(agrep_finalfp, "%d\n", total_line - (num_of_matched - prev_num_of_matched));
 
3600
                                                        else {
 
3601
                                                                char s[32];
 
3602
                                                                int  outindex;
 
3603
 
 
3604
                                                                sprintf(s, "%d\n", total_line - (num_of_matched - prev_num_of_matched));
 
3605
 
 
3606
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
3607
                                                                                (s[outindex] != '\0'); outindex++) {
 
3608
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
3609
                                                                }
 
3610
                                                                if (s[outindex] != '\0') {
 
3611
                                                                        OUTPUT_OVERFLOW;
 
3612
                                                                        close(fd);
 
3613
                                                                        return -1;
 
3614
                                                                }
 
3615
                                                                agrep_outpointer += outindex;
 
3616
                                                        }
 
3617
                                                }
 
3618
                                        }
 
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)) {
 
3622
 
 
3623
                                                        if (agrep_finalfp != NULL)
 
3624
                                                                fprintf(agrep_finalfp, "%s", CurrentFileName);
 
3625
                                                        else {
 
3626
                                                                int outindex;
 
3627
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
3628
                                                                                (CurrentFileName[outindex] != '\0'); outindex++) {
 
3629
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
3630
                                                                }
 
3631
                                                                if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
3632
                                                                        OUTPUT_OVERFLOW;
 
3633
                                                                        close(fd);
 
3634
                                                                        return -1;
 
3635
                                                                }
 
3636
                                                                agrep_outpointer += outindex;
 
3637
                                                        }
 
3638
                                                        if (PRINTFILETIME) {
 
3639
                                                                char *s = aprint_file_time(CurrentFileTime);
 
3640
                                                                if (agrep_finalfp != NULL)
 
3641
                                                                        fprintf(agrep_finalfp, "%s", s);
 
3642
                                                                else {
 
3643
                                                                        int outindex;
 
3644
                                                                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
3645
                                                                                        (s[outindex] != '\0'); outindex++) {
 
3646
                                                                                agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
3647
                                                                        }
 
3648
                                                                        if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
3649
                                                                                OUTPUT_OVERFLOW;
 
3650
                                                                                close(fd);
 
3651
                                                                                return -1;
 
3652
                                                                        }
 
3653
                                                                        agrep_outpointer += outindex;
 
3654
                                                                }
 
3655
                                                        }
 
3656
                                                        if (agrep_finalfp != NULL)
 
3657
                                                                fprintf(agrep_finalfp, ": %d\n", (num_of_matched - prev_num_of_matched));
 
3658
                                                        else {
 
3659
                                                                char s[32];
 
3660
                                                                int  outindex;
 
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];
 
3665
                                                                }
 
3666
                                                                if (s[outindex] != '\0') {
 
3667
                                                                        OUTPUT_OVERFLOW;
 
3668
                                                                        close(fd);
 
3669
                                                                        return -1;
 
3670
                                                                }
 
3671
                                                                agrep_outpointer += outindex;
 
3672
                                                        }
 
3673
 
 
3674
                                                        NEW_FILE = OFF;
 
3675
                                                }
 
3676
                                                else if (!FNAME) {
 
3677
                                                        if (agrep_finalfp != NULL)
 
3678
                                                                fprintf(agrep_finalfp, "%d\n", (num_of_matched - prev_num_of_matched));
 
3679
                                                        else {
 
3680
                                                                char s[32];
 
3681
                                                                int  outindex;
 
3682
 
 
3683
                                                                sprintf(s, "%d\n", (num_of_matched - prev_num_of_matched));
 
3684
 
 
3685
                                                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
3686
                                                                                (s[outindex] != '\0'); outindex++) {
 
3687
                                                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
3688
                                                                }
 
3689
                                                                if (s[outindex] != '\0') {
 
3690
                                                                        OUTPUT_OVERFLOW;
 
3691
                                                                        close(fd);
 
3692
                                                                        return -1;
 
3693
                                                                }
 
3694
                                                                agrep_outpointer += outindex;
 
3695
                                                        }
 
3696
                                                }
 
3697
                                        }
 
3698
                                }  /* if COUNT */
 
3699
                                if(FILEOUT && (num_of_matched - prev_num_of_matched)) {
 
3700
                                        if (-1 == file_out(file_list[i])) {
 
3701
                                                close(fd);
 
3702
                                                return -1;
 
3703
                                        }
 
3704
                                }
 
3705
                        } /* else */
 
3706
                        if (glimpse_clientdied) {
 
3707
                                close(fd);
 
3708
                                return -1;
 
3709
                        }
 
3710
                        if (agrep_finalfp != NULL) fflush(agrep_finalfp);
 
3711
                        if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
 
3712
                            ((LIMITTOTALFILE > 0) && (LIMITTOTALFILE <= files_matched))) {
 
3713
                                close(fd);
 
3714
                                break;  /* done */
 
3715
                        }
 
3716
                }  /* for i < Numfiles */
 
3717
 
 
3718
                if(NOMATCH && BESTMATCH) {
 
3719
                        if(WORDBOUND || WHOLELINE || INVERSE) { 
 
3720
                                SGREP = 0;      
 
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;
 
3724
                        }
 
3725
                        COUNT=ON; 
 
3726
                        D=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]);
 
3734
                                        NEW_FILE = ON;
 
3735
                                        if ((fd = my_open(Textfiles[i], O_RDONLY)) > 0) {
 
3736
                                                if(PAT_FILE || PAT_BUFFER) mgrep(fd, AParse);
 
3737
                                                else {
 
3738
                                                        if(SGREP) ret = sgrep(OldPattern,strlen(OldPattern),fd,D, i);
 
3739
                                                        else ret = bitap(old_D_pat,Pattern,fd,M,D);
 
3740
                                                }
 
3741
                                                if (ret <= -1) return -1;
 
3742
                                        }
 
3743
                                        /* else don't have to process PRINTNONEXISTENTFILE since must print only once */
 
3744
                                        if (glimpse_clientdied) {
 
3745
                                                close(fd);
 
3746
                                                return -1;
 
3747
                                        }
 
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)) {
 
3752
                                                close(fd);
 
3753
                                                break;
 
3754
                                        }
 
3755
                                }  /* for i < Numfiles */
 
3756
                                D++;
 
3757
                        } /* while */
 
3758
 
 
3759
                        if(num_of_matched - prev_num_of_matched > 0) {
 
3760
                                D--; 
 
3761
                                errno = D;      /* #of errors if proper return */
 
3762
                                COUNT = 0;
 
3763
 
 
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);
 
3768
 
 
3769
                                fflush(stderr);
 
3770
 
 
3771
                                if(NOPROMPT) fprintf(stderr, "\n");
 
3772
                                else {
 
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)");
 
3775
                                        c[0] = 'y';
 
3776
                                        if (!glimpse_isserver && (fgets(c, 4, stdin) == NULL)) goto CONT;
 
3777
                                        if(c[0] != 'y') goto CONT;
 
3778
                                }
 
3779
 
 
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]);
 
3786
                                        NEW_FILE = ON;
 
3787
                                        if ((fd = my_open(Textfiles[i], O_RDONLY)) > 0) {
 
3788
                                                if(PAT_FILE || PAT_BUFFER) mgrep(fd, AParse);
 
3789
                                                else {
 
3790
                                                        if(SGREP) ret = sgrep(OldPattern,strlen(OldPattern),fd,D, i);
 
3791
                                                        else ret = bitap(old_D_pat,Pattern,fd,M,D);
 
3792
                                                }
 
3793
                                                if (ret <= -1) {
 
3794
                                                        close(fd);
 
3795
                                                        return -1;
 
3796
                                                }
 
3797
                                        }
 
3798
                                        /* else don't have to process PRINTNONEXISTENTFILE since must print only once */
 
3799
                                        if (glimpse_clientdied) {
 
3800
                                                close(fd);
 
3801
                                                return -1;
 
3802
                                        }
 
3803
                                        if (agrep_finalfp != NULL) fflush(agrep_finalfp);
 
3804
                                        if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
 
3805
                                            ((LIMITTOTALFILE > 0) && (LIMITTOTALFILE <= files_matched))) {
 
3806
                                                close(fd);
 
3807
                                                break;  /* done */
 
3808
                                        }
 
3809
                                }  /* for i < Numfiles */
 
3810
                                NOMATCH = 0;
 
3811
                        }
 
3812
                }
 
3813
        }
 
3814
CONT:
 
3815
        if(EATFIRST) {
 
3816
                if (agrep_finalfp != NULL) fprintf(agrep_finalfp, "\n");
 
3817
                else if (agrep_outpointer >= agrep_outlen) {
 
3818
                        OUTPUT_OVERFLOW;
 
3819
                        return -1;
 
3820
                }
 
3821
                else agrep_outbuffer[agrep_outpointer++] = '\n';
 
3822
                EATFIRST = OFF;
 
3823
        }
 
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);
 
3828
 
 
3829
} /* end of exec() */
 
3830
 
 
3831
 
 
3832
/* Just output the contents of the file fname onto the std output */
 
3833
int
 
3834
file_out(fname)
 
3835
char *fname;
 
3836
{
 
3837
        int num_read;
 
3838
        int fd;
 
3839
        int i, len;
 
3840
        CHAR buf[SIZE+2];
 
3841
        if(FNAME) {
 
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);
 
3851
                }
 
3852
                else {
 
3853
                        if (1+len+1+len+1+len+1+agrep_outpointer >= agrep_outlen) {
 
3854
                                OUTPUT_OVERFLOW;
 
3855
                                return -1;
 
3856
                        }
 
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';
 
3864
                }
 
3865
        }
 
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) {
 
3871
                        close(fd);
 
3872
                        return -1;
 
3873
                }
 
3874
        }
 
3875
        else {
 
3876
                if ((num_read = fill_buf(fd, agrep_outbuffer + agrep_outpointer, agrep_outlen - agrep_outpointer)) > 0)
 
3877
                        agrep_outpointer += num_read;
 
3878
        }
 
3879
        close(fd);
 
3880
        return 0;
 
3881
}
 
3882
 
 
3883
int
 
3884
output(buffer, i1, i2, j)  
 
3885
register CHAR *buffer; 
 
3886
int i1, i2, j;
 
3887
{
 
3888
        int PRINTED = 0;
 
3889
        register CHAR *bp, *outend;
 
3890
        if(i1 > i2) return 0;
 
3891
        num_of_matched++;
 
3892
        if(COUNT)  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;
 
3896
                i2 = i2 + D_length;
 
3897
        }
 
3898
        if(DELIMITER) j = j+1;
 
3899
        if(FIRSTOUTPUT) {
 
3900
                if (buffer[i1] == '\n')  {
 
3901
                        i1++;
 
3902
                        EATFIRST = ON;
 
3903
                }
 
3904
                FIRSTOUTPUT = 0;
 
3905
        }
 
3906
        if(TRUNCATE) {
 
3907
                fprintf(stderr, "WARNING!  some lines have been truncated in output record #%d\n", num_of_matched-1);
 
3908
        }
 
3909
 
 
3910
        /* Why do we have to do this? */
 
3911
        while ((buffer[i1] == '\n') && (i1 <= i2)) {
 
3912
                if (agrep_finalfp != NULL) fprintf(agrep_finalfp, "\n");
 
3913
                else {
 
3914
                        if (agrep_outpointer < agrep_outlen)
 
3915
                                agrep_outbuffer[agrep_outpointer ++] = '\n';
 
3916
                        else {
 
3917
                                OUTPUT_OVERFLOW;
 
3918
                                return -1;
 
3919
                        }
 
3920
                }
 
3921
                i1++;
 
3922
        }
 
3923
 
 
3924
        if(FNAME && (NEW_FILE || !POST_FILTER)) {
 
3925
                char    nextchar = (POST_FILTER == ON)?'\n':' ';
 
3926
                char    *prevstring = (POST_FILTER == ON)?"\n":"";
 
3927
 
 
3928
                if (agrep_finalfp != NULL)
 
3929
                        fprintf(agrep_finalfp, "%s%s", prevstring, CurrentFileName);
 
3930
                else {
 
3931
                        int outindex;
 
3932
                        if (prevstring[0] != '\0') {
 
3933
                                if(agrep_outpointer + 1 >= agrep_outlen) {
 
3934
                                        OUTPUT_OVERFLOW;
 
3935
                                        return -1;
 
3936
                                }
 
3937
                                else agrep_outbuffer[agrep_outpointer ++] = prevstring[0];
 
3938
                        }
 
3939
                        for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
3940
                                        (CurrentFileName[outindex] != '\0'); outindex++) {
 
3941
                                agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
 
3942
                        }
 
3943
                        if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
3944
                                OUTPUT_OVERFLOW;
 
3945
                                return -1;
 
3946
                        }
 
3947
                        agrep_outpointer += outindex;
 
3948
                }
 
3949
                if (PRINTFILETIME) {
 
3950
                        char *s = aprint_file_time(CurrentFileTime);
 
3951
                        if (agrep_finalfp != NULL)
 
3952
                                fprintf(agrep_finalfp, "%s", s);
 
3953
                        else {
 
3954
                                int outindex;
 
3955
                                for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
 
3956
                                                (s[outindex] != '\0'); outindex++) {
 
3957
                                        agrep_outbuffer[agrep_outpointer+outindex] = s[outindex];
 
3958
                                }
 
3959
                                if ((s[outindex] != '\0') || (outindex+agrep_outpointer>=agrep_outlen)) {
 
3960
                                        OUTPUT_OVERFLOW;
 
3961
                                        return -1;
 
3962
                                }
 
3963
                                agrep_outpointer += outindex;
 
3964
                        }
 
3965
                }
 
3966
                if (agrep_finalfp != NULL)
 
3967
                        fprintf(agrep_finalfp, ":%c", nextchar);
 
3968
                else {
 
3969
                        if (agrep_outpointer+2>= agrep_outlen) {
 
3970
                                OUTPUT_OVERFLOW;
 
3971
                                return -1;
 
3972
                        }
 
3973
                        else {
 
3974
                                agrep_outbuffer[agrep_outpointer++] = ':';
 
3975
                                agrep_outbuffer[agrep_outpointer++] = nextchar;
 
3976
                        }
 
3977
                }
 
3978
 
 
3979
                NEW_FILE = OFF;
 
3980
                PRINTED = 1;
 
3981
        }
 
3982
        if(LINENUM) {
 
3983
                if (agrep_finalfp != NULL)
 
3984
                        fprintf(agrep_finalfp, "%d: ", j-1); 
 
3985
                else {
 
3986
                        char s[32];
 
3987
                        int  outindex;
 
3988
 
 
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];
 
3993
                        }
 
3994
                        if (s[outindex] != '\0') {
 
3995
                                OUTPUT_OVERFLOW;
 
3996
                                return -1;
 
3997
                        }
 
3998
                        agrep_outpointer += outindex;
 
3999
                }
 
4000
                PRINTED = 1;
 
4001
        }
 
4002
        if(BYTECOUNT) {
 
4003
                if (agrep_finalfp != NULL)
 
4004
                        fprintf(agrep_finalfp, "%d= ", CurrentByteOffset-1);
 
4005
                else {
 
4006
                        char s[32];
 
4007
                        int  outindex;
 
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];
 
4012
                        }
 
4013
                        if (s[outindex] != '\0') {
 
4014
                                OUTPUT_OVERFLOW;
 
4015
                                return -1;
 
4016
                        }
 
4017
                        agrep_outpointer += outindex;
 
4018
                }
 
4019
                PRINTED = 1;
 
4020
        }
 
4021
 
 
4022
        bp = buffer + i1;
 
4023
        outend = buffer + i2;
 
4024
 
 
4025
        if (PRINTOFFSET) {
 
4026
                if (agrep_finalfp != NULL)
 
4027
                        fprintf(agrep_finalfp, "@%d{%d}\n", CurrentByteOffset - (i2-i1), i2-i1);
 
4028
                else {
 
4029
                        char s[32];
 
4030
                        int outindex;
 
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];
 
4035
                        }
 
4036
                        if (s[outindex] != '\0') {
 
4037
                                OUTPUT_OVERFLOW;
 
4038
                                return -1;
 
4039
                        }
 
4040
                        agrep_outpointer += outindex;
 
4041
                }
 
4042
                PRINTED = 1;
 
4043
        }
 
4044
 
 
4045
        if (PRINTRECORD) {
 
4046
        if (agrep_finalfp != NULL)
 
4047
                while(bp <= outend) fputc(*bp++, agrep_finalfp);
 
4048
        else {
 
4049
                if (outend - bp + 1 + agrep_outpointer >= agrep_outlen) {
 
4050
                        OUTPUT_OVERFLOW;
 
4051
                        return -1;
 
4052
                }
 
4053
                while(bp <= outend) agrep_outbuffer[agrep_outpointer ++] = *bp++;
 
4054
        }
 
4055
        }
 
4056
        else if (PRINTED) {
 
4057
                if (agrep_finalfp != NULL) fputc('\n', agrep_finalfp);
 
4058
                else agrep_outbuffer[agrep_outpointer ++] = '\n';
 
4059
                PRINTED = 0;
 
4060
        }
 
4061
        return 0;
 
4062
}
 
4063
 
 
4064
int
 
4065
agrep_usage()
 
4066
{
 
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");  
 
4085
 
 
4086
        if (!EXITONERROR) {
 
4087
                errno = AGREP_ERROR;
 
4088
                return -1;
 
4089
        }
 
4090
        else exit(2);
 
4091
}