-
Notifications
You must be signed in to change notification settings - Fork 50
/
checksg.c
165 lines (154 loc) · 3.8 KB
/
checksg.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
/* Copyright (c) 1994 Sun Wu, Udi Manber, Burra Gopal. All Rights Reserved. */
#include "agrep.h"
#include "checkfil.h"
/* TG 29.04.04 */
#include <errno.h>
extern int errno;
extern CHAR Progname[MAXNAME];
extern int SGREP, PAT_FILE, PAT_BUFFER, EXITONERROR, SIMPLEPATTERN,
CONSTANT, D, NOUPPER, JUMP, I, LINENUM, INVERSE, WORDBOUND, WHOLELINE,
SILENT, DNA, BESTMATCH;
/* Make it an interface routine that tells you whether mgrep can be used
for the pattern or not: must sneak and access global variable D though... */
int
checksg(Pattern, D, set)
CHAR *Pattern;
int D;
int set; /* should I set flags SGREP and DNA? not if called from glimpse via library */
{
char c;
int i, m;
int NOTSGREP = 0;
if (set) SGREP = OFF;
m = strlen(Pattern);
#if DEBUG
fprintf(stderr, "checksg: len=%d, pat=%s, pat[len]=%d\n", m, Pattern, Pattern[m]);
#endif
if(!(PAT_FILE || PAT_BUFFER) && (m <= D)) {
fprintf(stderr, "%s: size of pattern '%s' must be > #of errors %d\n", Progname, Pattern, D);
if (!EXITONERROR) {
errno = AGREP_ERROR;
return -1;
}
else exit(2);
}
SIMPLEPATTERN = ON;
for (i=0; i < m; i++)
{
switch(Pattern[i])
{
case ';' :
SIMPLEPATTERN = OFF;
goto outoffor;
case ',' :
SIMPLEPATTERN = OFF;
goto outoffor;
case '.' :
SIMPLEPATTERN = OFF;
goto outoffor;
case '*' :
SIMPLEPATTERN = OFF;
goto outoffor;
case '-' :
SIMPLEPATTERN = OFF;
goto outoffor;
case '[' :
SIMPLEPATTERN = OFF;
goto outoffor;
case ']' :
SIMPLEPATTERN = OFF;
goto outoffor;
case '(' :
SIMPLEPATTERN = OFF;
goto outoffor;
case ')' :
SIMPLEPATTERN = OFF;
goto outoffor;
case '<' :
SIMPLEPATTERN = OFF;
goto outoffor;
case '>' :
SIMPLEPATTERN = OFF;
goto outoffor;
case '^' :
NOTSGREP = 1;
if(D > 0) SIMPLEPATTERN = OFF;
goto outoffor;
case '$' :
NOTSGREP = 1;
if(D > 0) SIMPLEPATTERN = OFF;
goto outoffor;
case '|' :
SIMPLEPATTERN = OFF;
goto outoffor;
case '#' :
SIMPLEPATTERN = OFF;
goto outoffor;
case '{':
SIMPLEPATTERN = OFF;
goto outoffor;
case '}':
SIMPLEPATTERN = OFF;
goto outoffor;
case '~':
SIMPLEPATTERN = OFF;
goto outoffor;
case '\\' :
{ /* Should I DO the left shift Pattern including Pattern[m] which is '\0', or just ignore the next character after '\\'????? */
if (set) { /* preprocess and maskgen figure out what to do */
i++; /* in addition to for loop ++ */
}
else { /* maskgen won't be called if we can help it, so shift it to make it verbatim */
/*
int j;
for (j=i; j<m; j++) Pattern[j] = Pattern[j+1];
m --;
*/
i++;
}
break;
}
default :
break;
}
}
outoffor:
if (CONSTANT) SIMPLEPATTERN = ON;
if (SIMPLEPATTERN == OFF) return 0;
if (BESTMATCH) return 0; /* can have errors, not simple */
if (!set && (D>0)) return 0; /* errors, not simple */
if (NOUPPER && (D>0)) return 0; /* errors, not simple */
if (JUMP == ON) return 0; /* I, S, D costs, not simple */
if (I == 0) return 0; /* I has 0 cost not 1, not simple */
if (LINENUM) return 0; /* can't use mgrep, so not simple */
if (WORDBOUND && (D > 0)) return 0; /* errors, not simple */
if (WHOLELINE && (D > 0)) return 0; /* errors, not simple */
if (SILENT) return 1; /* dont care output, so dont care pat */
if (set) {
if (!NOTSGREP || CONSTANT) SGREP = ON;
if (m >= 16) DNA = ON;
for(i=0; i<m; i++) {
c = Pattern[i];
if(c == 'a' || c == 'c' || c == 't' || c == 'g' ) ;
else DNA = OFF;
}
}
#if 0
/* Ditch this: sgrep does it internally anyway */
if (SGREP) { /* => set MUST be on */
for (i=0; i < m; i++)
{
switch(Pattern[i])
{
case '\\' :
for (j=i; j<m; j++) Pattern[j] = Pattern[j+1];
m --;
break;
default :
break;
}
}
}
#endif /*0*/
return 1; /* remains simple */
}