-
Notifications
You must be signed in to change notification settings - Fork 50
/
preproce.c
397 lines (344 loc) · 9.43 KB
/
preproce.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
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal. All Rights Reserved. */
/* substitute metachar with special symbol */
/* if regular expression, then set flag REGEX */
/* if REGEX and MULTIPAT then report error message, */
/* -w only for single word pattern. If WORDBOUND & MULTIWORD error */
/* process start of line, endof line symbol, */
/* process -w WORDBOUND option, append special symbol at begin&end of */
/* process -d option before this routine */
/* the delimiter pattern is in D_pattern (need to end with '; ') */
/* if '-t' (suggestion: how about -B) the pattern is passed to sgrep */
/* and doesn't go here */
/* in that case, -d is ignored? or not necessary */
/* upon return, Pattern contains the pattern to be processed by maskgen */
/* D_pattern contains transformed D_pattern */
#include "agrep.h"
/* TG 29.04.04 */
#include <errno.h>
extern int PAT_FILE, PAT_BUFFER;
extern ParseTree *AParse;
extern int WHOLELINE, REGEX, FASTREGEX, RE_ERR, DELIMITER, TAIL, WORDBOUND;
extern int HEAD;
extern CHAR Progname[];
extern int D_length, tc_D_length;
extern CHAR tc_D_pattern[MaxDelimit * 2];
extern int table[WORD][WORD];
extern int agrep_initialfd;
extern int EXITONERROR;
extern int errno;
extern int multifd;
extern char *multibuf;
extern int multilen;
extern int anum_terminals;
extern ParseTree aterminals[MAXNUM_PAT];
extern char FREQ_FILE[MAX_LINE_LEN], HASH_FILE[MAX_LINE_LEN], STRING_FILE[MAX_LINE_LEN]; /* interfacing with tcompress */
extern int AComplexBoolean;
#ifdef _WIN32
int asplit_pattern(); /* asplit.c */
int asplit_terminal(); /* asplit.c */
int init(); /* follow.c */
void destroy_tree(); /* putils.c */
int quick_tcompress(); /* preproce.c */
#endif
int
preprocess(D_pattern, Pattern) /* need two parameters */
CHAR D_pattern[], Pattern[];
{
CHAR temp[Maxline], *r_pat, *old_pat; /* r_pat for r.e. */
CHAR old_D_pat[MaxDelimit*2];
int i, j=0, rp=0, m, t=0, num_pos, ANDON = 0;
int d_end ;
int IN_RANGE=0;
int ret1, ret2;
#if DEBUG
fprintf(stderr, "preprocess: m=%d, pat=%s, PAT_FILE=%d, PAT_BUFFER=%d\n", strlen(Pattern), Pattern, PAT_FILE, PAT_BUFFER);
#endif
if ((m = strlen(Pattern)) <= 0) return 0;
if (PAT_FILE || PAT_BUFFER) return 0;
REGEX = OFF;
FASTREGEX = OFF;
old_pat = Pattern; /* to remember the starting position */
/* Check if pattern is a concatenation of ands OR ors of simple patterns */
multibuf = (char *)malloc(m * 2 + 2); /* worst case: a,a,a,a,a,a */
if (multibuf == NULL) goto normal_processing;
/* if (WORDBOUND) goto normal_processing; */
multilen = 0;
AParse = 0;
ret1 = ret2 = 0;
if (((ret1 = asplit_pattern(Pattern, m, aterminals, &anum_terminals, &AParse)) <= 0) || /* can change the pattern if simple boolean with {} */
((ret2 = asplit_terminal(0, anum_terminals, multibuf, &multilen)) <= 0) ||
((ret2 == 1) && !(aterminals[0].op & NOTPAT))) { /* must do normal processing */
if (AComplexBoolean && (AParse != NULL)) destroy_tree(AParse); /* so that direct exec invocations don't use AParse by mistake! */
#if DEBUG
fprintf(stderr, "preprocess: split_pat = %d, split_term = %d, #terms = %d\n", ret1, ret2, anum_terminals);
#endif /*DEBUG*/
/*
if (ret2 == 1) {
strcpy(Pattern, aterminals[0].data.leaf.value);
m = strlen(Pattern);
}
*/
m = strlen(Pattern);
AParse = 0;
free(multibuf);
multibuf = NULL;
multilen = 0;
goto normal_processing;
}
/* This is quick processing */
if (AParse != 0) { /* successfully converted to ANDPAT/ORPAT */
PAT_BUFFER = 1;
/* printf("preprocess(): converted= %d, patterns= %s", AParse, multibuf); */
/* Now I have to process the delimiter if any */
if (DELIMITER) {
/* D_pattern is "<PAT>; ", D_length is 1 + length of string PAT: see agrep.c/'d' */
preprocess_delimiter(D_pattern+1, D_length - 1, D_pattern, &D_length);
/* D_pattern is the exact stuff we want to match, D_length is its strlen */
if ((tc_D_length = quick_tcompress(FREQ_FILE, HASH_FILE, D_pattern, D_length, tc_D_pattern, MaxDelimit*2, TC_EASYSEARCH)) <= 0) {
strcpy(tc_D_pattern, D_pattern);
tc_D_length = D_length;
}
/* printf("mgrep's delim=%s,%d tc_delim=%s,%d\n", D_pattern, D_length, tc_D_pattern, tc_D_length); */
}
return 0;
}
/* else either unknown character, one simple pattern or none at all */
normal_processing:
for(i=0; i< m; i++) {
if(Pattern[i] == '\\') i++;
else if(Pattern[i] == '|' || Pattern[i] == '*') REGEX = ON;
}
r_pat = (CHAR *) malloc(strlen(Pattern)+2*strlen(D_pattern) + 8); /* bug-report, From: Chris Dalton <crd@hplb.hpl.hp.com> */
strcpy(temp, D_pattern);
d_end = t = strlen(temp); /* size of D_pattern, including '; ' */
if (WHOLELINE) {
temp[t++] = LANGLE;
temp[t++] = NNLINE;
temp[t++] = RANGLE;
temp[t] = '\0';
strcat(temp, Pattern);
m = strlen(temp);
temp[m++] = LANGLE;
temp[m++] = '\n';
temp[m++] = RANGLE;
temp[m] = '\0';
}
else {
if (WORDBOUND) {
temp[t++] = LANGLE;
temp[t++] = WORDB;
temp[t++] = RANGLE;
temp[t] = '\0';
}
strcat(temp, Pattern);
m = strlen(temp);
if (WORDBOUND) {
temp[m++] = LANGLE;
temp[m++] = WORDB;
temp[m++] = RANGLE;
}
temp[m] = '\0';
}
/* now temp contains augmented pattern , m it's size */
D_length = 0;
for (i=0, j=0; i< d_end-2; i++) {
switch(temp[i])
{
case '\\' :
i++;
Pattern[j++] = temp[i];
old_D_pat[D_length++] = temp[i];
break;
case '<' :
Pattern[j++] = LANGLE;
break;
case '>' :
Pattern[j++] = RANGLE;
break;
case '^' :
Pattern[j++] = '\n';
old_D_pat[D_length++] = temp[i];
break;
case '$' :
Pattern[j++] = '\n';
old_D_pat[D_length++] = temp[i];
break;
default :
Pattern[j++] = temp[i];
old_D_pat[D_length++] = temp[i];
break;
}
}
if(D_length > MAXDELIM) {
fprintf(stderr, "%s: delimiter pattern too long (has > %d chars)\n", Progname, MAXDELIM);
free(r_pat);
if (!EXITONERROR) {
errno = AGREP_ERROR;
return -1;
}
else exit(2);
}
Pattern[j++] = ANDPAT;
old_D_pat[D_length] = '\0';
strcpy(D_pattern, old_D_pat);
D_length++;
/* Pattern[j++] = ' '; */
Pattern[j] = '\0';
rp = 0;
if(REGEX) {
r_pat[rp++] = '.'; /* if REGEX: always append '.' in front */
r_pat[rp++] = '(';
Pattern[j++] = NOCARE;
HEAD = ON;
}
for (i=d_end; i < m ; i++)
{
switch(temp[i])
{
case '\\':
i++;
Pattern[j++] = temp[i];
r_pat[rp++] = 'o'; /* the symbol doesn't matter */
break;
case '#':
FASTREGEX = ON;
if(REGEX) {
Pattern[j++] = NOCARE;
r_pat[rp++] = '.';
r_pat[rp++] = '*';
break;
}
Pattern[j++] = WILDCD;
break;
case '(':
Pattern[j++] = LPARENT;
r_pat[rp++] = '(';
break;
case ')':
Pattern[j++] = RPARENT;
r_pat[rp++] = ')';
break;
case '[':
Pattern[j++] = LRANGE;
r_pat[rp++] = '[';
IN_RANGE = ON;
break;
case ']':
Pattern[j++] = RRANGE;
r_pat[rp++] = ']';
IN_RANGE = OFF;
break;
case '<':
Pattern[j++] = LANGLE;
break;
case '>':
Pattern[j++] = RANGLE;
break;
case '^':
if (temp[i-1] == '[') Pattern[j++] = NOTSYM;
else Pattern[j++] = '\n';
r_pat[rp++] = '^';
break;
case '$':
Pattern[j++] = '\n';
r_pat[rp++] = '$';
break;
case '.':
Pattern[j++] = NOCARE;
r_pat[rp++] = '.';
break;
case '*':
Pattern[j++] = STAR;
r_pat[rp++] = '*';
break;
case '|':
Pattern[j++] = ORSYM;
r_pat[rp++] = '|';
break;
case ',':
Pattern[j++] = ORPAT;
RE_ERR = ON;
break;
case ';':
if(ANDON) RE_ERR = ON;
Pattern[j++] = ANDPAT;
ANDON = ON;
break;
case '-':
if(IN_RANGE) {
Pattern[j++] = HYPHEN;
r_pat[rp++] = '-';
}
else {
Pattern[j++] = temp[i];
r_pat[rp++] = temp[i];
}
break;
default:
if (temp[i]==NNLINE) {
Pattern[j++] = temp[i];
r_pat[rp++] = 'N';
}
else {
Pattern[j++] = temp[i];
r_pat[rp++] = temp[i];
}
break;
}
}
if(REGEX) { /* append ').' at end of regular expression */
r_pat[rp++] = ')';
r_pat[rp++] = '.';
Pattern[j++] = NOCARE;
TAIL = ON;
}
Pattern[j] = '\0';
m = j;
r_pat[rp] = '\0';
if(REGEX)
{
if(DELIMITER || WORDBOUND) {
fprintf(stderr, "%s: -d or -w option is not supported for this pattern\n", Progname);
free(r_pat);
if (!EXITONERROR) {
errno = AGREP_ERROR;
return -1;
}
else exit(2);
}
if(RE_ERR) {
fprintf(stderr, "%s: illegal regular expression\n", Progname);
free(r_pat);
if (!EXITONERROR) {
errno = AGREP_ERROR;
return -1;
}
else exit(2);
}
while ((*Pattern != NOCARE) && (m-- > 0)) Pattern++; /* point to . */
num_pos = init(r_pat, table);
if(num_pos <= 0) {
fprintf(stderr, "%s: illegal regular expression\n", Progname);
free(r_pat);
if (!EXITONERROR) {
errno = AGREP_ERROR;
return -1;
}
else exit(2);
}
if(num_pos > 30) {
fprintf(stderr, "%s: regular expression too long\n", Progname);
free(r_pat);
if (!EXITONERROR) {
errno = AGREP_ERROR;
return -1;
}
else exit(2);
}
strcpy(old_pat, Pattern); /* do real change to the Pattern to be returned */
free(r_pat);
return 0;
} /* if regex */
free(r_pat);
return 0;
}