-
Notifications
You must be signed in to change notification settings - Fork 50
/
agrep.c
3970 lines (3555 loc) · 112 KB
/
agrep.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
AGREP APPROXIMATE PATTERN - GREP.
Copyright (c) 1994-1997 Sun Wu, Udi Manber, Burra Gopal, Tom Gries (for OS/2)
All Rights Reserved.
[fix] 3.40 TG 08.11.2004 option -By does not work together with -n. to allow this, I commented LINENUM out
[fix] 3.35 TG 11.12.97 agrep.c: -f function did not work because of a simple
problem in the commandline parser: the patternfile preprocessing
prepf() was done before definition of the codepage. Oops.
[fix] 3.34 newmgrep.c: input buffer was used before its start address
[chg] 3.33 TG 02.03.97 when no target filename(s) were given:
AGREP displays an error message now
instead of reading from stdin.
Solves the following problem:
When one uses "AGREP <needle> *" and there are
no files in that subdirectory,
the 3.32 has waited for stdin (=haystack, target).
[new] verbose option -V5 dumps codepage table
[chg] compiled with emx 0.9c
[chg] TG 18.02.97 new homepage, see AGREPHLP.C.
I keep the same version number.
[chg] 3.32 TG 15.01.97 new links, helppage revised
[new] 3.31 TG 16.12.96 new option -i0
[fix] 3.29 TG 14.10.96 see version.h
[new] 3.21 TG 07.10.96 Verbose option -V
[new] 3.20 TG 06.10.96 multi-codepage support
now implemented CP437 and CP850
[new] 3.19 TG 05.10.96 environment variable AGREPOPTS
[new] 3.14 TG 25.09.96 new option -i#:
let letters match letters, digits match digits,
others match others
[new] 3.12 dynamic metasymbol assignment (in preparation)
[chg] returncode = total number of hits
[new] 3.11 TG 23.09.96 option -ia maps all ISO characters to nearest ASCII
[chg] 3.10 TG 22.09.96 handling and check for metasymbols in searchstring
[fix] 3.09 TG 22.09.96 BITAP.C: type CHAR instead of type char
[new] AGREP.C: Grand Total
[fix] 3.08 TG 16.09.96
- A dummy printf("") statement is now used to avoid the
following memory(?) problem:
now and then, when using the delimiter option and
on big files and piping AGREP's output to a file..
Example: AGREP -d "/AN " -i "next;block" infile > outfile
..AGREP has crashed with SYS3175 (access violation).
- provisonally un-commenting the diagnostic error message
for OUTPUT_OVERFLOW (in AGRPEP.H)
[chg] 3.07 [TG]
- improved performance for CP850 upper to lower translation
(now using a look-up table)
- improved help screens
[chg] 3.04 R.M. Thomas [MT] & Th. Gries [TG] July/August 1996:
- conditional compiling using the __EMX__ flag
- EMX: use of _wildcard() function to expand wildcards
- using constants AGREP_VERSION, AGREP_DATE in agrep.h
[ini] 3.x bgopal: (1993-1994)
- Added a library interface and removed some bugs.
- Also selectively modified many routines to work
with our text-compression algo.
*/
/* definition of an environmentvariable which value is prepended to
to value of the actual command line. [TG]
Example:
SET AGREPOPTS=-i to let AGREP search case-insensitive
*/
unsigned char metasymb[16]; /* we define 16 metasymbols */
#include "agrep.h"
#include "version.h"
#include "codepage.h"
#include "checkfil.h"
/* TG 29.04.04 */
#include <errno.h>
char AGREPOPT_STR[MAX_LINE_LEN];
extern char CODEPAGE_STR[MAX_LINE_LEN]; /* holds the selected codepage identifier */
extern int CODEPAGE; /* corresponding number */
extern char CP_MAPPING; /* -i option = case(in)sensitive search ?
is one of 'i', 'a', '#' or 0 */
extern struct CODEPAGE_struct CP[CODEPAGES][CPSIZE];
extern unsigned char LUT[256];
#define PRINT(s)
extern char **environ;
extern int errno;
int pattern_index; /* index in argv where the pattern is */
int glimpse_isserver=0; /* so that there is no user interaction */
int glimpse_call = 0; /* So that usage message is not printed twice */
int glimpse_clientdied=0; /* to quit search if glimpseserver's client dies */
int agrep_initialfd; /* Where does input come from? File/Memory? */
CHAR *agrep_inbuffer;
int agrep_inlen;
int agrep_inpointer;
FILE *agrep_finalfp; /* Where does output go to? File/Memory? */
CHAR *agrep_outbuffer;
int agrep_outlen;
int agrep_outpointer;
int execfd; /* used by exec called within agrep_search, set in agrep_init */
int multifd = -1; /* fd for multipattern search used in ^^ , set in ^^^^^^^^ */
extern char *pat_spool;
#if DOTCOMPRESSED
extern char *tc_pat_spool;
#endif /* DOTCOMPRESSED */
char *multibuf=NULL; /* buffer to put the multiple patterns in */
int multilen = 0; /* length of the multibuf: not the #of multi-patterns! */
extern int pos_cnt; /* to re-initialize it to 0 for reg-exp search */
unsigned Mask[MAXSYM];
unsigned Init1, NO_ERR_MASK, Init[MaxError];
unsigned Bit[WORD+1];
CHAR buffer[BlockSize+Maxline+1]; /* should not be used anywhere: 10/18/93 */
unsigned Next[MaxNext], Next1[MaxNext];
unsigned wildmask, endposition, D_endpos;
int LIMITOUTPUT; /* maximum number of matches we are going to allow */
int LIMITPERFILE; /* maximum number of matches per file we are going to allow */
int LIMITTOTALFILE; /* maximum number of files we are going to allow */
int EXITONERROR; /* return -1 or exit on error? */
int REGEX, FASTREGEX, RE_ERR, FNAME, WHOLELINE, SIMPLEPATTERN;
int COUNT, HEAD, TAIL, LINENUM, INVERSE, I, S, DD, AND, SGREP, JUMP;
int NOOUTPUTZERO;
int Num_Pat, PSIZE, prev_num_of_matched, num_of_matched, files_matched;
int SILENT, NOPROMPT, BESTMATCH, NOUPPER, ISO2ASCII;
int NOMATCH, TRUNCATE, FIRST_IN_RE, FIRSTOUTPUT;
int WORDBOUND, DELIMITER, D_length, tc_D_length, original_D_length;
int EATFIRST, OUTTAIL;
int BYTECOUNT;
int PRINTOFFSET;
int PRINTRECORD;
int VERBOSE=1; /* Verbose default: AGREP shows the Grand Total */
int FILEOUT;
int DNA;
int APPROX;
int PAT_FILE; /* multiple patterns from a given file */
char PAT_FILE_NAME[MAX_LINE_LEN];
int PAT_BUFFER; /* multiple patterns from a given buffer */
int CONSTANT;
int RECURSIVE;
int total_line; /* used in mgrep */
int D;
int M;
int TCOMPRESSED;
int EASYSEARCH; /* 1 used only for compressed files: LITTLE/BIG */
int ALWAYSFILENAME = OFF;
int POST_FILTER = OFF;
int NEW_FILE = OFF; /* only when post-filter is used */
int PRINTFILENUMBER = OFF;
int PRINTPATTERN = OFF;
int MULTI_OUTPUT = OFF; /* should mgrep print the matched line multiple times for each matched pattern or just once? */
/* 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) */
/* Stuff to handle complicated boolean patterns */
int AComplexBoolean = 0;
ParseTree *AParse = NULL;
int anum_terminals = 0;
ParseTree aterminals[MAXNUM_PAT];
char amatched_terminals[MAXNUM_PAT];
#if MEASURE_TIMES
/* timing variables */
int OUTFILTER_ms;
int FILTERALGO_ms;
int INFILTER_ms;
#endif /*MEASURE_TIMES*/
CHAR **Textfiles = NULL; /* array of filenames to be searched */
int Numfiles = 0; /* indicates how many files in Textfiles */
int copied_from_argv = 0; /* were filenames copied from argv (should I free 'em)? */
CHAR old_D_pat[MaxDelimit * 2] = "\n"; /* to hold original D_pattern */
CHAR original_old_D_pat[MaxDelimit * 2] = "\n";
CHAR Pattern[MAXPAT], OldPattern[MAXPAT];
CHAR CurrentFileName[MAX_LINE_LEN];
int SetCurrentFileName = 0; /* dirty glimpse trick to make filters work: output seems to come from another file */
int CurrentByteOffset;
int SetCurrentByteOffset = 0;
CHAR Progname[MAXNAME];
/* string which delimits records -- defaults to newline */
CHAR D_pattern[MaxDelimit * 2] = "\n; ";
CHAR tc_D_pattern[MaxDelimit * 2] = "\n";
CHAR original_D_pattern[MaxDelimit * 2] = "\n; ";
char COMP_DIR[MAX_LINE_LEN];
char FREQ_FILE[MAX_LINE_LEN], HASH_FILE[MAX_LINE_LEN], STRING_FILE[MAX_LINE_LEN]; /* interfacing with tcompress */
int NOFILENAME, /* Boolean flag, set for -h option */
FILENAMEONLY; /* Boolean flag, set for -l option */
extern int init();
int table[WORD][WORD];
CHAR *agrep_saved_pattern = NULL; /* to prevent multiple prepfs for each boolean search: crd@hplb.hpl.hp.com */
#ifdef _WIN32
#include <direct.h>
#include <io.h>
int agrep_usage(); /* agrep.c */
int exec(); /* agrep.c */
int exponen(); /* agrep.c */
int r_output(); /* agrep.c */
int file_out(); /* agrep.c */
void agrep_online_help(); /* agrephlp.c */
int bitap(); /* bitap.c */
int fill_buf(); /* bitap.c */
int check_file(); /* checkfil.c */
int checksg(); /* checksg.c */
int get_current_codepage(); /* codepage.c */
int compat(); /* compat.c */
int maskgen(); /* maskgen.c */
int mgrep(); /* newmgrep.c */
int prepf(); /* newmgrep.c */
int preprocess(); /* preproce.c */
void destroy_tree(); /* putils.c */
int recursive(); /* recursiv.c */
int sgrep(); /* sgrep.c */
int initialize_common(); /* dummyfil.c */
int tuncompressible_filename(); /* dummyfil.c */
int quick_tcompress(); /* dummyfil.c */
#endif
/* Called when multipattern search and pattern has not changed */
void reinit_value_partial()
{
num_of_matched = prev_num_of_matched = 0;
errno = 0;
FIRST_IN_RE = ON;
}
/* This must be called before every agrep_search to reset agrep globals */
void reinit_value()
{
int i, j;
/* Added on 7th Oct 1994 */
if (AParse) {
if (AComplexBoolean) destroy_tree(AParse);
AComplexBoolean = 0;
AParse = 0;
PAT_BUFFER = 0;
if (multibuf != NULL) free(multibuf); /* this was allocated for arbit booleans, not multipattern search */
multibuf = NULL;
multilen = 0;
/* Cannot free multifd here since that is always allocated for multipattern search */
}
for (i=0; i<anum_terminals; i++) {
free(aterminals[i].data.leaf.value);
memset(&aterminals[i], '\0', sizeof(ParseTree));
}
anum_terminals = 0;
Bit[WORD] = 1;
for (i = WORD - 1; i > 0 ; i--) Bit[i] = Bit[i+1] << 1;
for (i=0; i< MAXSYM; i++) Mask[i] = 0;
/* bg: new things added on Mar 13 94 */
Init1 = 0;
NO_ERR_MASK = 0;
memset(Init, '\0', MaxError * sizeof(unsigned));
memset(Next, '\0', MaxNext * sizeof(unsigned));
memset(Next1, '\0', MaxNext * sizeof(unsigned));
wildmask = endposition = D_endpos = 0;
for (i=0; i<WORD; i++)
for (j=0; j<WORD; j++)
table[i][j] = 0;
strcpy(D_pattern, original_D_pattern);
D_length = original_D_length;
strcpy(old_D_pat, original_old_D_pat);
/* Changed on Dec 26th: bg */
FASTREGEX = REGEX = 0;
HEAD = TAIL = ON; /* were off initially */
RE_ERR = 0;
AND = 0;
M = 0;
pos_cnt = 0; /* added 31 Jan 95 */
reinit_value_partial();
}
/* This must be called before every agrep_init to reset agrep options */
void initial_value()
{
SetCurrentFileName = 0; /* 16/9/94 */
SetCurrentByteOffset = 0; /* 23/9/94 */
/* courtesy: crd@hplb.hpl.hp.com */
if (agrep_saved_pattern) {
free(agrep_saved_pattern);
agrep_saved_pattern= NULL;
}
/* bg: new stuff on 17/Feb/94 */
if (multifd != -1) close(multifd);
multifd = -1;
if (multibuf != NULL) free(multibuf);
multibuf = NULL;
multilen = 0;
if (pat_spool != NULL) free(pat_spool);
pat_spool = NULL;
#if DOTCOMPRESSED
if (tc_pat_spool != NULL) free(tc_pat_spool);
tc_pat_spool = NULL;
#endif /* DOTCOMPRESSED */
LIMITOUTPUT = 0; /* means infinity = current semantics */
LIMITPERFILE = 0; /* means infinity = current semantics */
LIMITTOTALFILE = 0; /* means infinity = current semantics */
EASYSEARCH = 1;
DNA = APPROX = PAT_FILE = PAT_BUFFER = CONSTANT = total_line = D = TCOMPRESSED = 0;
PAT_FILE_NAME[0] = '\0';
EXITONERROR = NOFILENAME = FILENAMEONLY = FILEOUT = ALWAYSFILENAME = NEW_FILE = POST_FILTER = 0;
original_old_D_pat[0] = old_D_pat[0] = '\n';
original_old_D_pat[1] = old_D_pat[1] = '\0';
original_D_pattern[0] = D_pattern[0] = '\n';
original_D_pattern[1] = D_pattern[1] = ';';
original_D_pattern[2] = D_pattern[2] = ' ';
original_D_pattern[3] = D_pattern[3] = '\0';
strcpy(tc_D_pattern, "\n");
tc_D_length = 1;
/* the functions agrep_init and agrep_search take care of Textfiles and Numfiles */
agrep_inpointer = 0;
agrep_outpointer = 0;
agrep_outlen = 0;
#if MEASURE_TIMES
OUTFILTER_ms = FILTERALGO_ms = INFILTER_ms = 0;
#endif /*MEASURE_TIMES*/
MULTI_OUTPUT = 0;
PRINTPATTERN = 0;
PRINTFILENUMBER = 0;
JUMP = FNAME = BESTMATCH = NOPROMPT = NOUPPER = ISO2ASCII = 0 ;
RECURSIVE = 0;
COUNT = LINENUM = WHOLELINE = SGREP = 0;
NOOUTPUTZERO = 0;
EATFIRST = INVERSE = TRUNCATE = OUTTAIL = 0;
NOMATCH = FIRSTOUTPUT = ON; /* were off initally */
I = DD = S = 1; /* were off initially */
original_D_length = D_length = 2; /* was 0 initially */
SILENT = Num_Pat = PSIZE = SIMPLEPATTERN = prev_num_of_matched = num_of_matched = files_matched = 0;
WORDBOUND = DELIMITER = 0;
COMP_DIR[0] = '\0';
FREQ_FILE[0] = '\0';
HASH_FILE[0] = '\0';
STRING_FILE[0] = '\0';
BYTECOUNT = OFF;
PRINTOFFSET = OFF;
PRINTRECORD = ON;
glimpse_clientdied = 0; /* added 15th Feb 95 */
/* Pattern, OldPattern, execfd, Numfiles are set in agrep_init: so no need to initialize */
reinit_value();
}
void compute_next(M, Next, Next1)
int M;
unsigned *Next, *Next1;
{
int i, j=0, n, k, temp;
int mid, pp;
int MM, base;
unsigned V[WORD];
base = WORD - M;
temp = Bit[base];
Bit[base] = 0;
for (i=0; i<WORD; i++) V[i] = 0;
for (i=1; i<M; i++)
{
j=0;
while (table[i][j] > 0 && j < 10) {
V[i] = V[i] | Bit[base + table[i][j++]];
}
}
Bit[base]=temp;
if(M <= SHORTREG)
{
k = exponen(M);
pp = 2*k;
for(i=k; i<pp ; i++)
{
n = i;
Next[i]= (k>>1);
for(j=M; j>=1; j--)
{
if(n & Bit[WORD]) Next[i] = Next[i] | V[j];
n = (n>>1);
}
}
return;
}
if(M > MAXREG) fprintf(stderr, "%s: regular expression too long\n", Progname);
MM = M;
if(M & 1) M=M+1;
k = exponen(M/2);
pp = 2*k;
mid = MM/2;
for(i=k; i<pp ; i++)
{
n = i;
Next[i]= (Bit[base]>>1);
for(j=MM; j>mid ; j--)
{
if(n & Bit[WORD]) Next[i] = Next[i] | V[j-mid];
n = (n>>1);
}
n=i-k;
Next1[i-k] = 0;
for(j = 0; j<mid; j++)
{
if(n & Bit[WORD]) Next1[i-k] = Next1[i-k] | V[MM-j];
n = (n>>1);
}
}
return;
}
int exponen(m)
int m;
{
int i, ex;
ex= 1;
for (i=0; i<m; i++) ex <<= 1; /* was ex *= 2 */
return(ex);
}
int re1(Text, M, D)
int Text, M, D;
{
register unsigned i, c, r0, r1, r2, r3, CMask, Newline, Init0, r_NO_ERR;
register unsigned end;
register unsigned hh, LL=0, k; /* Lower part */
int FIRST_TIME=ON, num_read , j=0, base;
unsigned A[MaxRerror+1], B[MaxRerror+1];
unsigned Next[MaxNext], Next1[MaxNext];
CHAR *buffer;
int FIRST_LOOP = 1;
r_NO_ERR = NO_ERR_MASK;
if(M > 30) {
fprintf(stderr, "%s: regular expression too long\n", Progname);
if (!EXITONERROR){
errno = AGREP_ERROR;
return -1;
}
else exit(2);
}
base = WORD - M;
hh = M/2;
for(i=WORD, j=0; j < hh ; i--, j++) LL = LL | Bit[i];
if(FIRST_IN_RE) compute_next(M, Next, Next1);
/*SUN: try: change to memory allocation */
FIRST_IN_RE = 0;
Newline = '\n';
Init[0] = Bit[base];
if(HEAD) Init[0] = Init[0] | Bit[base+1];
for(i=1; i<= D; i++) Init[i] = Init[i-1] | Next[Init[i-1]>>hh] | Next1[Init[i-1]&LL];
Init1 = Init[0] | 1;
Init0 = Init[0];
r2 = r3 = Init[0];
for(k=0; k<= D; k++) {
A[k] = B[k] = Init[k];
}
if ( D == 0 )
{
#if AGREP_POINTER
if (Text != -1)
{
#endif /*AGREP_POINTER*/
alloc_buf(Text, &buffer, BlockSize+Maxline+1);
while ((num_read = fill_buf(Text, buffer + Maxline, BlockSize)) > 0)
{
i=Maxline;
end = num_read + Maxline;
if((num_read < BlockSize) && buffer[end-1] != '\n') buffer[end] = '\n';
if(FIRST_LOOP) { /* if first time in the loop add a newline */
buffer[i-1] = '\n'; /* in front the text. */
i--;
CurrentByteOffset --;
FIRST_LOOP = 0;
}
/* RE1_PROCESS_WHEN_DZERO: the while-loop below */
while ( i < end )
{
c = buffer[i++];
CurrentByteOffset ++;
CMask = Mask[c];
if(c != Newline)
{
if(CMask != 0) {
r1 = Init1 & r3;
r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
}
else {
r2 = r3 & Init1;
}
}
else {
j++;
if (DELIMITER) CurrentByteOffset -= D_length;
else CurrentByteOffset -= 1;
r1 = Init1 & r3; /* match against endofline */
r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
if(TAIL) r2 = (Next[r2>>hh] | Next1[r2&LL]) | r2; /* epsilon move */
if(( r2 & 1 ) ^ INVERSE) {
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
num_of_matched++;
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s\n", CurrentFileName);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(Text, buffer);
return -1;
}
else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
agrep_outpointer += outindex;
}
free_buf(Text, buffer);
NEW_FILE = OFF;
return 0;
}
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
free_buf(Text, buffer);
return 0; /* done */
}
}
r3 = Init0;
r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | Init0;
/* match begin of line */
if (DELIMITER) CurrentByteOffset += 1*D_length;
else CurrentByteOffset += 1*1;
}
c = buffer[i++];
CurrentByteOffset ++;
CMask = Mask[c];
if(c != Newline)
{
if(CMask != 0) {
r1 = Init1 & r2;
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
}
else r3 = r2 & Init1;
} /* if(NOT Newline) */
else {
j++;
if (DELIMITER) CurrentByteOffset -= D_length;
else CurrentByteOffset -= 1;
r1 = Init1 & r2; /* match against endofline */
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
if(TAIL) r3 = ( Next[r3>>hh] | Next1[r3&LL] ) | r3;
/* epsilon move */
if(( r3 & 1 ) ^ INVERSE) {
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
num_of_matched++;
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s\n", CurrentFileName);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(Text, buffer);
return -1;
}
else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
agrep_outpointer += outindex;
}
free_buf(Text, buffer);
NEW_FILE = OFF;
return 0;
}
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
free_buf(Text, buffer);
return 0; /* done */
}
}
r2 = Init0;
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | Init0;
/* match begin of line */
if (DELIMITER) CurrentByteOffset += 1*D_length;
else CurrentByteOffset += 1*1;
}
} /* while i < end ... */
strncpy(buffer, buffer+num_read, Maxline);
} /* end while fill_buf()... */
free_buf(Text, buffer);
return 0;
#if AGREP_POINTER
}
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' */
num_read = agrep_inlen;
buffer = (CHAR *)agrep_inbuffer;
end = num_read;
/* buffer[end-1] = '\n';*/ /* at end of the text. */
/* buffer[0] = '\n';*/ /* in front of the text. */
i = 0;
/* An exact copy of the above RE1_PROCESS_WHEN_DZERO: the while-loop below */
while ( i < end )
{
c = buffer[i++];
CurrentByteOffset ++;
CMask = Mask[c];
if(c != Newline)
{
if(CMask != 0) {
r1 = Init1 & r3;
r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
}
else {
r2 = r3 & Init1;
}
}
else {
j++;
if (DELIMITER) CurrentByteOffset -= D_length;
else CurrentByteOffset -= 1;
r1 = Init1 & r3; /* match against endofline */
r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | r1;
if(TAIL) r2 = (Next[r2>>hh] | Next1[r2&LL]) | r2; /* epsilon move */
if(( r2 & 1 ) ^ INVERSE) {
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
num_of_matched++;
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s\n", CurrentFileName);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(Text, buffer);
return -1;
}
else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
agrep_outpointer += outindex;
}
free_buf(Text, buffer);
NEW_FILE = OFF;
return 0;
}
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
free_buf(Text, buffer);
return 0; /* done */
}
}
r3 = Init0;
r2 = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | Init0;
/* match begin of line */
if (DELIMITER) CurrentByteOffset += 1*D_length;
else CurrentByteOffset += 1*1;
}
c = buffer[i++];
CurrentByteOffset ++;
CMask = Mask[c];
if(c != Newline)
{
if(CMask != 0) {
r1 = Init1 & r2;
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
}
else r3 = r2 & Init1;
} /* if(NOT Newline) */
else {
j++;
if (DELIMITER) CurrentByteOffset -= D_length;
else CurrentByteOffset -= 1;
r1 = Init1 & r2; /* match against endofline */
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
if(TAIL) r3 = ( Next[r3>>hh] | Next1[r3&LL] ) | r3;
/* epsilon move */
if(( r3 & 1 ) ^ INVERSE) {
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
num_of_matched++;
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s\n", CurrentFileName);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(Text, buffer);
return -1;
}
else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
agrep_outpointer += outindex;
}
free_buf(Text, buffer);
NEW_FILE = OFF;
return 0;
}
if (-1 == r_output(buffer, i-1, end, j)) {free_buf(Text, buffer); return -1;}
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
free_buf(Text, buffer);
return 0; /* done */
}
}
r2 = Init0;
r3 = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | Init0;
/* match begin of line */
if (DELIMITER) CurrentByteOffset += 1*D_length;
else CurrentByteOffset += 1*1;
}
} /* while i < end ... */
return 0;
}
#endif /*AGREP_POINTER*/
} /* end if (D == 0) */
#if AGREP_POINTER
if (Text != -1)
{
#endif /*AGREP_POINTER*/
while ((num_read = fill_buf(Text, buffer + Maxline, BlockSize)) > 0)
{
i=Maxline;
end = Maxline + num_read;
if((num_read < BlockSize) && buffer[end-1] != '\n') buffer[end] = '\n';
if(FIRST_TIME) { /* if first time in the loop add a newline */
buffer[i-1] = '\n'; /* in front the text. */
i--;
CurrentByteOffset --;
FIRST_TIME = 0;
}
/* RE1_PROCESS_WHEN_DNOTZERO: the while loop below */
while (i < end )
{
c = buffer[i];
CMask = Mask[c];
if(c != Newline)
{
if(CMask != 0) {
r2 = B[0];
r1 = Init1 & r2;
A[0] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
r3 = B[1];
r1 = Init1 & r3;
r0 = r2 | A[0]; /* A[0] | B[0] */
A[1] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | (( r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 1) goto Nextcharfile;
r2 = B[2];
r1 = Init1 & r2;
r0 = r3 | A[1];
A[2] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 2) goto Nextcharfile;
r3 = B[3];
r1 = Init1 & r3;
r0 = r2 | A[2];
A[3] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 3) goto Nextcharfile;
r2 = B[4];
r1 = Init1 & r2;
r0 = r3 | A[3];
A[4] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 4) goto Nextcharfile;
} /* if(CMask) */
else {
r2 = B[0];
A[0] = r2 & Init1;
r3 = B[1];
r1 = Init1 & r3;
r0 = r2 | A[0];
A[1] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 1) goto Nextcharfile;
r2 = B[2];
r1 = Init1 & r2;
r0 = r3 | A[1];
A[2] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 2) goto Nextcharfile;
r3 = B[3];
r1 = Init1 & r3;
r0 = r2 | A[2];
A[3] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 3) goto Nextcharfile;
r2 = B[4];
r1 = Init1 & r2;
r0 = r3 | A[3];
A[4] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 4) goto Nextcharfile;
}
}
else {
j++;
if (DELIMITER) CurrentByteOffset -= D_length;
else CurrentByteOffset -= 1;
r1 = Init1 & B[D]; /* match against endofline */
A[D] = ((Next[B[D]>>hh] | Next1[B[D]&LL]) & CMask) | r1;
if(TAIL) A[D] = ( Next[A[D]>>hh] | Next1[A[D]&LL] ) | A[D];
/* epsilon move */
if(( A[D] & 1 ) ^ INVERSE) {
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
num_of_matched++;
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s\n", CurrentFileName);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(Text, buffer);
return -1;
}
else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
agrep_outpointer += outindex;
}
free_buf(Text, buffer);
NEW_FILE = OFF;
return 0;
}
if (-1 == r_output(buffer, i, end, j)) {free_buf(Text, buffer); return -1;}
if (((LIMITOUTPUT > 0) && (LIMITOUTPUT <= num_of_matched)) ||
((LIMITPERFILE > 0) && (LIMITPERFILE <= num_of_matched - prev_num_of_matched))) {
free_buf(Text, buffer);
return 0; /* done */
}
}
for(k=0; k<=D; k++) B[k] = Init[0];
r1 = Init1 & B[0];
A[0] = (( Next[B[0]>>hh] | Next1[B[0]&LL]) & CMask) | r1;
for(k=1; k<=D; k++) {
r3 = B[k];
r1 = Init1 & r3;
r2 = A[k-1] | B[k-1];
A[k] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((B[k-1] | Next[r2>>hh] | Next1[r2&LL]) & r_NO_ERR) | r1;
}
if (DELIMITER) CurrentByteOffset += 1*D_length;
else CurrentByteOffset += 1*1;
}
Nextcharfile:
i=i+1;
CurrentByteOffset ++;
c = buffer[i];
CMask = Mask[c];
if(c != Newline)
{
if(CMask != 0) {
r2 = A[0];
r1 = Init1 & r2;
B[0] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | r1;
r3 = A[1];
r1 = Init1 & r3;
r0 = B[0] | r2;
B[1] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((r2 | Next[r0>>hh] | Next1[r0&LL]) & r_NO_ERR) | r1 ;
if(D == 1) goto Nextchar1file;
r2 = A[2];
r1 = Init1 & r2;
r0 = B[1] | r3;
B[2] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 2) goto Nextchar1file;
r3 = A[3];
r1 = Init1 & r3;
r0 = B[2] | r2;
B[3] = ((Next[r3>>hh] | Next1[r3&LL]) & CMask) | ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 3) goto Nextchar1file;
r2 = A[4];
r1 = Init1 & r2;
r0 = B[3] | r3;
B[4] = ((Next[r2>>hh] | Next1[r2&LL]) & CMask) | ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 4) goto Nextchar1file;
} /* if(CMask) */
else {
r2 = A[0];
B[0] = r2 & Init1;
r3 = A[1];
r1 = Init1 & r3;
r0 = B[0] | r2;
B[1] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 1) goto Nextchar1file;
r2 = A[2];
r1 = Init1 & r2;
r0 = B[1] | r3;
B[2] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 2) goto Nextchar1file;
r3 = A[3];
r1 = Init1 & r3;
r0 = B[2] | r2;
B[3] = ((r2 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 3) goto Nextchar1file;
r2 = A[4];
r1 = Init1 & r2;
r0 = B[3] | r3;
B[4] = ((r3 | Next[r0>>hh] | Next1[r0&LL])&r_NO_ERR) | r1 ;
if(D == 4) goto Nextchar1file;
}
} /* if(NOT Newline) */
else {
j++;
if (DELIMITER) CurrentByteOffset -= D_length;
else CurrentByteOffset -= 1;
r1 = Init1 & A[D]; /* match against endofline */
B[D] = ((Next[A[D]>>hh] | Next1[A[D]&LL]) & CMask) | r1;
if(TAIL) B[D] = ( Next[B[D]>>hh] | Next1[B[D]&LL] ) | B[D];
/* epsilon move */
if(( B[D] & 1 ) ^ INVERSE) {
if(FILENAMEONLY && (NEW_FILE || !POST_FILTER)) {
num_of_matched++;
if (agrep_finalfp != NULL)
fprintf(agrep_finalfp, "%s\n", CurrentFileName);
else {
int outindex;
for(outindex=0; (outindex+agrep_outpointer<agrep_outlen) &&
(CurrentFileName[outindex] != '\0'); outindex++) {
agrep_outbuffer[agrep_outpointer+outindex] = CurrentFileName[outindex];
}
if ((CurrentFileName[outindex] != '\0') || (outindex+agrep_outpointer+1>=agrep_outlen)) {
OUTPUT_OVERFLOW;
free_buf(Text, buffer);
return -1;
}
else agrep_outbuffer[agrep_outpointer+outindex++] = '\n';
agrep_outpointer += outindex;
}
free_buf(Text, buffer);
NEW_FILE = OFF;
return 0;
}
if (-1 == r_output(buffer, i, end, j)) {free_buf(Text, buffer); return -1;}