forked from oe5hpm/openBCM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grep.cpp
372 lines (338 loc) · 8.98 KB
/
grep.cpp
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
/***************************************************************
BayCom(R) Packet-Radio fuer IBM PC
OpenBCM-Mailbox
---------------------------------------------------
Suchroutinen mit Moeglichkeit regulaerer Ausdruecke
---------------------------------------------------
Copyright (C) Florian Radlherr
Taubenbergstr. 32
83627 Warngau
Alle Rechte vorbehalten / All Rights Reserved
Parts adoped from: "grep.c for VMS" Copyright (C) 1980, DECUS
* General permission to copy or modify, but not for profit, is
* hereby granted, provided that the above copyright notice is
* included and reference made to the fact that reproduction
* privileges were granted by DECUS.
***************************************************************/
//19980414 OE3DZW fixed endless loop when reg-expression contains "**"
#include "baycom.h"
/*---------------------------------------------------------------------------*/
CRegEx::CRegEx()
//****************************************************************************
//
//****************************************************************************
{
m_options = 256;
}
/*---------------------------------------------------------------------------*/
int CRegEx::regex_compile (char *source)
//*************************************************************************
//
// Compile the pattern
//
//*************************************************************************
{
char *s = source; // Source string pointer
char *lp = NULL; // Last pattern pointer
int c; // Current character
char *spp; // Save beginning of pattern
char *pp = m_pattern + 2;
char nextstore;
int quote = 0;
m_pattern[0] = QUICK;
m_pattern[1] = CASESENSE;
if (strstr(s, "**") || strstr(s, "*+")) goto error; //quick fix for ugly bug
if (s[0] == '"')
{
s++;
quote = 1;
}
while ((c = *s++) != 0)
{
if (c == '*' || c == '+') // STAR, PLUS are special.
{
if (! lp) goto error;
switch (pp[-1])
{
case BOL:
case EOL:
case STAR:
case PLUS: goto error;
}
pp[0] = pp[1] = ENDPAT;
pp += 2;
spp = pp; // Save pattern end
while (--spp > lp) spp[0] = spp[-1]; // Move pattern down one byte
spp[0] = (c == '*') ? STAR : PLUS;
continue;
}
// All the rest.
nextstore = 0;
lp = pp; // Remember start
switch (c)
{
case '^': nextstore = BOL; break;
case '$': nextstore = EOL; break;
case '.': nextstore = ANY; break;
case '[':
if (s[0] == '^')
{
++s;
*pp++ = NCLASS;
}
else *pp++ = CLASS;
spp = pp++; // spare for byte count
while ((c = *s++) != ']')
{
if (! c) goto error;
if (c == '\\') // Store quoted char
{
if ((c = *s++) == 0) goto error;
else *pp++ = c;
}
else if (c == '-' && (pp - spp) > 1 && s[0] != ']' && s[0])
{
c = pp[-1]; // Range start
pp[-1] = RANGE; // Range signal
*pp++ = c; // Re-store start
*pp++ = s[0];
s++;
}
else *pp++ = c;
}
spp[0] = (char) (pp - spp);
break;
case '"':
if (quote) while (*s) s++;
else goto def;
break;
case '\\':
if (*s) c = *s++;
default:
def: *pp++ = CHAR;
nextstore = c;
}
if (nextstore) *pp++ = nextstore;
}
*pp++ = ENDPAT;
*pp++ = 0; // Terminate string
pp = m_pattern + 2;
while (pp[0])
{
if (pp[0] > CHAR && pp[0] < ENDPAT) m_pattern[0] = REGEX;
pp++;
}
if (strchr(source, '\\')) m_pattern[0] = REGEX;
if (m_pattern[0] == QUICK || (m_options & o_q))
{
strcpy(m_pattern + 2, source + quote);
pp = strchr(m_pattern, '"');
if (pp && quote) pp[0] = 0;
m_pattern[0] = QUICK;
}
if (m_options & o_i)
{
m_pattern[1] = TOUPPER;
strupr(m_pattern);
}
if (m_options & o_r)
{
putf("RegEx: \"");
pp = m_pattern;
while (*pp)
{
if (*pp > ' ') putf("%c", pp[0]);
else putf("(%d)", pp[0]);
pp++;
}
putf("\"\n");
}
return 1;
error:
return 0;
}
/*---------------------------------------------------------------------------*/
char *CRegEx::pmatch (char *begin, char *line, char *pattern)
//*************************************************************************
//
//
//
//*************************************************************************
{
char *l = line; // Current line pointer
char *p = pattern; // Current pattern pointer
char c; // Current character
char *e; // End for STAR and PLUS match
int op; // Pattern operation
int n; // Class counter
char *are; // Start of STAR match
while ((op = *p++) != ENDPAT)
{
c = l[0];
switch (op)
{
case CHAR:
if (c != *p++) goto notmatch;
l++;
break;
case BOL:
if (begin != l) goto notmatch;
break;
case EOL:
if (c != LF) goto notmatch;
break;
case ANY:
l++;
if (c == LF) goto notmatch;
break;
case CLASS:
l++;
n = *p++;
do
{
if (p[0] == RANGE)
{
p += 3;
n -= 2;
if (c >= p[-2] && c <= p[-1]) break;
}
else if (c == *p++) break;
}
while (--n > 1);
if (n <= 1) goto notmatch;
p += n - 2;
break;
case NCLASS:
l++;
n = *p++;
do
{
if (p[0] == RANGE)
{
n -= 2;
if (c >= p[1] && c <= p[2]) goto notmatch;
p += 3;
}
else if (c == *p++) goto notmatch;
}
while (--n > 1);
break;
case PLUS: // One or more ...
l = pmatch(begin, l, p);
if (! l) goto notmatch; // Gotta have a match
case STAR: // Zero or more ...
are = l; // Remember line start
while (l[0] && (e = pmatch(begin, l, p)) != NULL)
l = e; // Get longest match
while (*p++ != ENDPAT); // Skip over pattern
while (l >= are) // Try to match rest
{
e = pmatch(begin, l, p);
if (e) return e;
--l; // Nope, try earlier
}
goto notmatch; // Nothing else worked
}
}
return l;
notmatch:
return NULL;
}
/*---------------------------------------------------------------------------*/
int CRegEx::regex_match (char *pattern, char *line)
//****************************************************************************
//
//****************************************************************************
{
regex_compile(pattern);
return regex_match(line);
}
/*---------------------------------------------------------------------------*/
int CRegEx::regex_match (char *line)
//*************************************************************************
//
// Match the current line, return 1 if it does.
//
//*************************************************************************
{
static char lbuf[LMAX];
char *lbufp = lbuf;
lbuf[0] = 1;
strcpy(lbuf + 1, line);
if (m_pattern[1] == TOUPPER) strupr(lbuf + 1);
if (m_pattern[0] == QUICK) return strstr(lbuf + 1, m_pattern + 2) != NULL;
else
{
while (lbufp[0])
{
if (pmatch(lbuf + 1, lbufp, m_pattern + 2)) return 1;
lbufp++;
}
}
return 0;
}
/*---------------------------------------------------------------------------*/
int CRegEx::grepfile (FILE * fp)
//*************************************************************************
//
// Scan the file for the pattern in pbuf[]
//
//*************************************************************************
{
unsigned int lno = 0, count = 0, n;
char obuf[LMAX];
while (fgets(obuf, LMAX - 2, fp))
{
++lno;
n = regex_match(obuf);
if ((n && ! (m_options & o_v)) || (! n && (m_options & o_v)))
{
++count;
if (! (m_options & o_c))
{
if (m_options & o_n)
putf("%d:", lno);
putf("%s", obuf);
}
}
if ((lno % 50) == 0)
{
waitfor(e_ticsfull);
if (testabbruch()) break;
}
}
if ((m_options & o_c) && ! (m_options & o_x))
putf(ms(m_matchinglines), count);
return count;
}
/*---------------------------------------------------------------------------*/
int grep (char *name, char *pattern, bitfeld options)
//*************************************************************************
//
//
//
//*************************************************************************
{
FILE *f;
CRegEx cr;
int matches = 0;
cr.m_options = options;
if (name[0])
{
if (cr.regex_compile(pattern))
{
f = s_fopen(name, "srt");
if (f)
{
setvbuf(f, NULL, _IOFBF, 4096);
matches = cr.grepfile(f);
s_fclose(f);
}
else
putf(ms(m_filenotopen), name);
}
else
putf(ms(m_badregex));
}
return matches;
}