1
/* Copyright (c) 1991 Sun Wu and Udi Manber. All Rights Reserved. */
1
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal. All Rights Reserved. */
4
5
extern unsigned D_endpos, endposition, Init1, wildmask;
5
6
extern Mask[], Bit[], Init[], NO_ERR_MASK;
6
extern int AND, SIMPLEPATTERN, REGEX, NOUPPER, D_length;
7
extern int AND, REGEX, NOUPPER, D_length;
7
8
extern unsigned char Progname[];
9
extern int agrep_initialfd;
10
extern int EXITONERROR;
10
unsigned char *Pattern; int D;
15
unsigned char *Pattern;
12
struct term { int flag; unsigned char class[WORD];
16
int i, j, k, l, M, OR=0, EVEN = 0, base, No_error;
19
for(i=0; i<WORD; i++) position[i].class[0] = '\0';
20
for(i=0; i<WORD; i++) position[i].flag = 0;
21
wildmask = NO_ERR_MASK = endposition = 0;
25
for(i=0; i<M; i++) if(isalpha(Pattern[i]))
26
if (isupper(Pattern[i])) Pattern[i] = tolower(Pattern[i]);
20
unsigned char class[WORD];
25
int i, j, k, l, M, OR=0, EVEN = 0, base, No_error;
28
fprintf(stderr, "maskgen: len=%d, pat=%s, D=%d\n", strlen(Pattern), Pattern, D);
30
for(i=0; i<WORD; i++) position[i].class[0] = '\0';
31
for(i=0; i<WORD; i++) position[i].flag = 0;
32
wildmask = NO_ERR_MASK = endposition = 0;
34
if ((M = strlen(Pattern)) <= 0) return 0;
36
for(i=0; i<M; i++) if(isalpha((int)Pattern[i]))
37
if (isupper((int)Pattern[i])) Pattern[i] = tolower((int)Pattern[i]);
29
40
for(i=0; i<M; i++) printf(" %d", Pattern[i]);
32
for (i=0, j=1; i< M; i++)
36
case WILDCD : if(REGEX) {
37
position[j].class[0] = '.';
38
position[j].class[1] = '.';
39
position[j++].class[2] = '\0';
42
wildmask = wildmask | Bit[j-1]; break;
47
case LANGLE : No_error = ON; EVEN++;
49
case RANGLE : No_error = OFF; EVEN--;
51
fprintf(stderr, "%s: illegal pattern, unmatched '<', '>'\n", Progname);
55
case LRANGE : if(No_error == ON) NO_ERR_MASK = NO_ERR_MASK | Bit[j];
57
if (Pattern[i] == NOTSYM) { position[j].flag = Compl; i++; }
59
while (Pattern[i] != RRANGE && i < M)
61
if(Pattern[i] == HYPHEN)
62
{ position[j].class[k-1] = Pattern[i+1]; i=i+2; }
64
position[j].class[k] = position[j].class[k+1] = Pattern[i];
69
fprintf(stderr, "%s: illegal pattern, unmatched '[', ']'\n",Progname);
72
position[j].class[k] = '\0';
74
case RRANGE : fprintf(stderr, "%s: illegal pattern, unmatched '[', ']'\n", Progname);
77
case ORPAT : if(REGEX == ON || AND == ON) {
78
fprintf(stderr, "illegal pattern \n");
82
position[j].flag = 2; position[j].class[0] = '\0';
83
endposition = endposition | Bit[j++]; break;
84
case ANDPAT : position[j].flag = 2; position[j].class[0] = '\0';
85
if(j > D_length) AND = ON;
86
if(OR || (REGEX == ON && j>D_length)) {
87
fprintf(stderr, "illegal pattern \n");
90
endposition = endposition | Bit[j++]; break;
92
case ' ' : if (Pattern[i-1] == ORPAT || Pattern[i-1] == ANDPAT) break;
93
if(No_error == ON) NO_ERR_MASK = NO_ERR_MASK | Bit[j];
95
position[j].class[0] = position[j].class[1] = Pattern[i];
96
position[j++].class[2] = '\0'; break;
98
case '\n' : NO_ERR_MASK = NO_ERR_MASK | Bit[j];
99
position[j].class[0] = position[j].class[1] = '\n';
100
position[j++].class[2] = '\0';
102
case WORDB : NO_ERR_MASK = NO_ERR_MASK | Bit[j];
103
position[j].class[0] = 1;
104
position[j].class[1] = 47;
105
position[j].class[2] = 58;
106
position[j].class[3] = 64;
107
position[j].class[4] = 91;
108
position[j].class[5] = 96;
109
position[j].class[6] = 123;
110
position[j].class[7] = 127;
111
position[j++].class[8] = '\0';
113
case NNLINE : NO_ERR_MASK |= Bit[j];
114
position[j].class[0] = position[j].class[1] = '\n';
115
position[j].class[2] = position[j].class[3] = NNLINE;
116
position[j++].class[4] = '\0';
118
default : if(No_error == ON) NO_ERR_MASK = NO_ERR_MASK | Bit[j];
119
position[j].flag = 0;
120
position[j].class[0] = position[j].class[1] = Pattern[i];
121
position[j++].class[2] = '\0';
124
fprintf(stderr, "%s: pattern too long\n", Progname);
129
fprintf(stderr, "%s: illegal pattern, unmatched '<', '>'\n", Progname);
134
wildmask = (wildmask >> base);
135
endposition = (endposition >> base);
136
NO_ERR_MASK = (NO_ERR_MASK >> 1) & (~Bit[1]);
137
NO_ERR_MASK = ~NO_ERR_MASK >> (base-1);
138
for (i=1; i<= WORD - M ; i++) Init[0] = Init[0] | Bit[i];
139
Init[0] = Init[0] | endposition;
140
/* not necessary for INit[i], i>0, */
141
/* but at every begining of the matching process append one
142
no-match character to initialize the error vectors */
143
endposition = ( endposition << 1 ) + 1;
144
Init1 = (Init[0] | wildmask | endposition) ;
145
D_endpos = ( endposition >> ( M - D_length ) ) << ( M - D_length);
146
endposition = endposition ^ D_endpos;
43
for (i=0, j=1; i< M; i++)
49
position[j].class[0] = '.';
50
position[j].class[1] = '.';
51
position[j++].class[2] = '\0';
54
wildmask = wildmask | Bit[j-1];
72
fprintf(stderr, "%s: unmatched '<', '>' (use \\<, \\> to search for <, >)\n", Progname);
81
if(No_error == ON) NO_ERR_MASK = NO_ERR_MASK | Bit[j];
83
if (Pattern[i] == NOTSYM) {
84
position[j].flag = Compl;
88
while (Pattern[i] != RRANGE && i < M)
90
if(Pattern[i] == HYPHEN)
92
position[j].class[k-1] = Pattern[i+1];
96
position[j].class[k] = position[j].class[k+1] = Pattern[i];
102
fprintf(stderr, "%s: unmatched '[', ']' (use \\[, \\] to search for [, ])\n", Progname);
109
position[j].class[k] = '\0';
113
fprintf(stderr, "%s: unmatched '[', ']' (use \\[, \\] to search for [, ])\n", Progname);
121
if(REGEX == ON || AND == ON) {
122
fprintf(stderr, "illegal pattern: cannot handle OR (',') and AND (';')/regular-expressions simultaneously\n");
130
position[j].flag = 2;
131
position[j].class[0] = '\0';
132
endposition = endposition | Bit[j++];
135
position[j].flag = 2;
136
position[j].class[0] = '\0';
137
if(j > D_length) AND = ON;
138
if(OR || (REGEX == ON && j>D_length)) {
139
fprintf(stderr, "illegal pattern: cannot handle AND (';') and OR (',')/regular-expressions simultaneously\n");
146
endposition = endposition | Bit[j++];
149
case ' ' : if (Pattern[i-1] == ORPAT || Pattern[i-1] == ANDPAT) break;
150
if(No_error == ON) NO_ERR_MASK = NO_ERR_MASK | Bit[j];
151
position[j].flag = 0;
152
position[j].class[0] = position[j].class[1] = Pattern[i];
153
position[j++].class[2] = '\0'; break;
156
NO_ERR_MASK = NO_ERR_MASK | Bit[j];
157
position[j].class[0] = position[j].class[1] = '\n';
158
position[j++].class[2] = '\0';
161
NO_ERR_MASK = NO_ERR_MASK | Bit[j];
162
position[j].class[0] = 1;
163
position[j].class[1] = 47;
164
position[j].class[2] = 58;
165
position[j].class[3] = 64;
166
position[j].class[4] = 91;
167
position[j].class[5] = 96;
168
position[j].class[6] = 123;
169
position[j].class[7] = 127;
170
position[j++].class[8] = '\0';
173
NO_ERR_MASK |= Bit[j];
174
position[j].class[0] = position[j].class[1] = '\n';
175
position[j].class[2] = position[j].class[3] = NNLINE;
176
position[j++].class[4] = '\0';
179
if(No_error == ON) NO_ERR_MASK = NO_ERR_MASK | Bit[j];
180
position[j].flag = 0;
181
position[j].class[0] = position[j].class[1] = Pattern[i];
182
position[j++].class[2] = '\0';
185
fprintf(stderr, "%s: pattern too long (has > %d chars)\n", Progname, WORD);
194
fprintf(stderr, "%s: unmatched '<', '>' (use \\<, \\> to search for <, >)\n", Progname);
203
wildmask = (wildmask >> base);
204
endposition = (endposition >> base);
205
NO_ERR_MASK = (NO_ERR_MASK >> 1) & (~Bit[1]);
206
NO_ERR_MASK = ~NO_ERR_MASK >> (base-1);
207
for (i=1; i<= WORD - M ; i++) Init[0] = Init[0] | Bit[i];
208
Init[0] = Init[0] | endposition;
209
/* not necessary for INit[i], i>0, */
210
/* but at every begining of the matching process append one
211
no-match character to initialize the error vectors */
212
endposition = ( endposition << 1 ) + 1;
213
Init1 = (Init[0] | wildmask | endposition) ;
214
D_endpos = ( endposition >> ( M - D_length ) ) << ( M - D_length);
215
endposition = endposition ^ D_endpos;
148
217
printf("endposition: %o\n", endposition);
149
218
printf("no_err_mask: %o\n", NO_ERR_MASK);
151
for(c=0, i=0; i < MAXSYM; c++, i++)
153
for (k=1, l=0; k<=M ; k++, l=0) {
154
while (position[k].class[l] != '\0') {
155
if (position[k].class[l] == NOCARE && (c != '\n' || REGEX) )
156
{ Mask[c] = Mask[c] | Bit[base + k]; break; }
157
if (c >= position[k].class[l] && c <= position[k].class[l+1])
158
{ Mask[c] = Mask[c] | Bit[base + k]; break; }
160
if (position[k].flag == Compl) Mask[c] = Mask[c] ^ Bit[base+k];
163
if(NOUPPER) for(c='A'; c<='Z'; c=c+1) if (isupper(c))
164
Mask[c] = Mask[tolower(c)];
220
for(c=0, i=0; i < MAXSYM; c++, i++)
222
for (k=1, l=0; k<=M ; k++, l=0) {
223
while (position[k].class[l] != '\0') {
224
if (position[k].class[l] == NOCARE && (c != '\n' || REGEX) )
226
Mask[c] = Mask[c] | Bit[base + k];
229
if (c >= position[k].class[l] && c <= position[k].class[l+1])
231
Mask[c] = Mask[c] | Bit[base + k];
236
if (position[k].flag == Compl) Mask[c] = Mask[c] ^ Bit[base+k];
239
if(NOUPPER) for(i=0; i<MAXSYM; i++)
240
if (isupper(i)) Mask[i] = Mask[tolower(i)];