-
Notifications
You must be signed in to change notification settings - Fork 5
/
find.c
562 lines (493 loc) · 11.1 KB
/
find.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
/*
* LEVEE, or Captain Video; A vi clone
*
* Copyright (c) 1982-2007 David L Parsons
* All rights reserved.
*
* Redistribution and use in source and binary forms, without or
* without modification, are permitted provided that the above
* copyright notice and this paragraph are duplicated in all such
* forms and that any documentation, advertising materials, and
* other materials related to such distribution and use acknowledge
* that the software was developed by David L Parsons (orc@pell.portland.or.us).
* My name may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE.
*/
#include "levee.h"
#include "extern.h"
#include "grep.h"
#include <ctype.h>
#include <stdlib.h>
int amatch(char*,char*,char*);
int locate(char*,char*);
void patsize(char**);
static int arg; /* arguments inside of a RE */
int
REmatch(char *pattern, int start, int end)
{
char *endp = &core[end];
if (!*pattern)
return -1;
arg = 0;
while (start <= end && !amatch(pattern, &core[start], endp))
start++;
return start;
}
int
omatch(char *pattern, char **cp, char *endp)
{
register int flag;
extern int ignorecase;
switch (*pattern) {
case LEND:
return (**cp == EOL);
case LSTART:
return (*cp == core) || (*(*cp-1) == EOL);
case TOKENB:
return (*cp == core) || !isalnum(*(*cp-1));
case TOKENE:
return !isalnum(**cp);
case LITCHAR:
if (ignorecase)
flag = (toupper(**cp) == toupper(*(pattern+1)));
else
flag = (**cp == *(pattern+1));
break;
case ANY:
flag = (**cp != EOL);
break;
case CCL:
flag = locate(pattern,*cp);
break;
case NCCL:
flag = !locate(pattern,*cp);
break;
case ARGSTART:
RE_start[arg] = (*cp)-core;
return TRUE;
case ARGEND:
RE_size[arg] = ((*cp)-core) - RE_start[arg];
++arg;
return TRUE;
default:
return TRUE;
}
if (*cp <= endp && flag) {
(*cp)++;
return TRUE;
}
return FALSE;
}
int
amatch(char *pattern,char *start,char *endp)
{
int sarg = arg; /* save old arg match count for errors */
while (*pattern) {
if (*pattern == CLOSURE) {
/* Find the longest closure possible and work backwards trying
* to match the rest of the pattern.
*/
char *oldstart = start;
++pattern; /* skip over the closure token */
while (start <= endp && omatch(pattern,&start,endp))
;
/* start points at the character that failed the search.
* Try to match the rest of the pattern against it, working
* back down the line if failure
*/
patsize(&pattern);
while (start >= oldstart)
if (amatch(pattern,start--,endp))
return TRUE;
arg = sarg;
return FALSE;
}
else {
if (!omatch(pattern,&start,endp)) {
arg = sarg;
return FALSE;
}
patsize(&pattern);
}
}
lastp = start-core;
return TRUE;
}
/* increment pattern by the size of the token being scanned
*/
void
patsize(char **pattern)
{
register int count;
switch (**pattern) {
case LITCHAR:
*pattern += 2;
break;
case CCL:
case NCCL:
count = *(++*pattern) & 0xff;
*pattern += 1+count;
break;
default:
(*pattern)++;
break;
}
}
int
locate(char *pattern,register char *linep)
/* locate: find a character in a closure */
{
register char *p = 1+pattern;
register int count;
if ((count = (*p++)&0xff) == 0)
return FALSE;
while (count--)
if (*p++ == *linep)
return TRUE;
return FALSE;
}
char *p;
void
concatch(char c)
/* add a character to the pattern */
{
if (p < &pattern[MAXPAT-1])
*p++ = c;
}
char
esc(char **s)
{
if (**s != ESCAPE || *(1+*s) == 0)
return **s;
++(*s);
switch (**s) {
case 't': return TAB;
case 'n': return EOL;
}
return **s;
}
char *
dodash(char *src)
/* parse the innards of a [] */
{
int k;
char *start = src;
char cs[128];
fillchar(cs,sizeof(cs),FALSE);
while (*src && *src != CCLEND) {
if (*src == DASH && src>start && src[1] != CCLEND && src[-1]<src[1]) {
for ( k = src[-1]; k <= src[1]; k++)
cs[k] = TRUE;
src++;
}
else
cs[(unsigned int)esc(&src)] = TRUE;
src++;
}
for (k=0; k < sizeof(cs); k++)
if (cs[k])
concatch((char)k);
return src;
}
char *
badccl(char *src)
/* a [] was encountered. is it a CCL (match one of the included
* characters); or is it a NCCL (match all but the included characters)?
*/
{
char *jstart;
if (*src == NEGATE) {
concatch(NCCL);
++src;
}
else
concatch(CCL);
jstart = p;
concatch(0); /* this will be the length of the pattern */
src = dodash(src);
*jstart = (p-jstart)-1;
return src;
}
/* patterns that cannot be closed */
char badclose[] = { LSTART, LEND, CLOSURE, 0 };
char *
makepat(char *string,int delim)
/* make up the pattern string for find -- ripped from 'Software Tools' */
{
char *cp = 0, *oldcp;
char *start = string;
int inarg = FALSE;
for(arg=0;arg<9;++arg)
RE_start[arg] = RE_size[arg] = (-1);
arg = 0;
p = pattern;
while ((*string != delim) && (*string != 0)) {
oldcp = cp;
cp = p;
if ((*string == LSTART) && (string == start))
concatch(LSTART);
else if ((*string == LEND) && (string[1] == delim || string[1] == 0))
concatch(LEND);
else if (!magic) /* ^ & $ are significant no matter what */
goto normal;
else if (*string == ANY)
concatch(ANY);
else if (*string == CCL)
string = badccl(1+string);
else if ((*string == CLOSURE) && (p > pattern)) {
cp = oldcp;
if (strchr(badclose, *cp) || p >= &pattern[MAXPAT-1])
return NULL;
moveright(cp,1+cp,(int)(p-cp));
*cp = CLOSURE;
p++;
}
else if (*string == ESCAPE) {
if (string[1] == ARGSTART || string[1] == ARGEND) {
if (string[1] == ARGEND)
if (!inarg)
goto normal;
if (string[1] == ARGSTART) {
if (inarg)
goto normal;
if (++arg > 9)
return NULL;
}
inarg = !inarg;
}
else if (string[1] != TOKENB && string[1] != TOKENE)
goto normal;
++string;
concatch(*string);
}
else {
normal:concatch(LITCHAR);
concatch(esc(&string));
}
if (*string)
string++;
}
if (inarg)
concatch(ARGEND);
if (p > pattern) { /* new pattern was created */
strncpy(lastpatt,start,(int)(string-start));
lastpatt[string-start] = 0;
concatch(0);
if (p-pattern >= MAXPAT)
return NULL;
}
return (*string == delim)?(string+1):(string);
}
int
findfwd(char *pattern, int start, int endp)
/* look for a regular expression forward */
{
int ep;
while (start < endp) {
ep = fseekeol(start);
if ((start = REmatch(pattern, start, ep)) <= ep)
return start;
}
return ERR_NOMATCH;
}
int
findback(char *pattern, int start, int endp)
/* look for a regular expression backwards */
{
int ep,i;
while (start > endp) {
ep = bseekeol(start);
if ((i = REmatch(pattern, ep, start)) <= start)
return i;
start = ep-1;
}
return ERR_NOMATCH;
}
char *
expr_errstring(int errno)
{
switch (errno) {
case ERR_NOMATCH: return "Pattern not found";
case ERR_PATTERN: return "Unreadable pattern";
case ERR_RANGE: return "Out of range";
case ERR_UNDEF: return "Marker unset";
case ERR_EXPR: return "Impossible address";
default: return "error?";
}
}
bool s_wrapped = 0;
int
search(int start, char **bufp)
/* get a token for find & find it in the buffer
*/
{
int pos;
char marker = **bufp;
bool forwd = (lsearch = marker) == '/';
unless ( *bufp = makepat(1+(*bufp), marker) )
return ERR_PATTERN;
do {
if (forwd) {
pos = findfwd(pattern, start+1, bufmax-1);
if ((pos == ERR_NOMATCH) && wrapscan) {
s_wrapped = 1;
pos = findfwd(pattern, 0, start-1);
}
}
else {
pos = findback(pattern, start-1, 0);
if ((pos == ERR_NOMATCH) && wrapscan) {
s_wrapped = 1;
pos = findback(pattern, bufmax-1, start+1);
}
}
unless ( pos > ERR ) {
logit(("search: error %s", expr_errstring(pos)));
return pos;
}
start = pos;
} while ( --count > 0);
return start;
}
static int
address_fragment(int start, char **bufp, int offset)
/* parse part of an address (a /pattern/, ?pattern?, [0-9]*, 0, $) */
{
int addr;
count = s_wrapped = 0;
switch (**bufp) {
case '/': /* search forward */
if ( offset < 0 )
return ERR_RANGE;
addr = search(start, bufp);
break;
case '?': /* search backwards */
if ( offset > 0 )
return ERR_RANGE;
addr = search(start, bufp);
break;
addr = search(start, bufp);
break;
case '0': /* line # or offset */
case '1': case '2': case '3':
case '4': case '5': case '6':
case '7': case '8': case '9':
/* fabricate a count */
count = strtol(*bufp, bufp, 10);
if ( offset ) {
if ( count )
addr = nextline((offset>0), start, count);
}
else
addr = to_index(count);
break;
case '$': /* jump to end of file */
if ( offset )
return ERR_EXPR;
addr = bufmax-1;
++(*bufp);
break;
case '.' :
if ( offset )
return ERR_EXPR;
addr = start;
++(*bufp);
break;
default:
return ERR_UNKNOWN;
}
return addr;
}
int
findparse(int start, char **bufp, int offset) /* driver for ?, /, && : lineranges */
{
int addr;
char c;
c = **bufp;
if ( c == '`' || c == '\'' ) {
unless ( (addr = lvgetcontext( (*bufp)[1], c == '\'')) > ERR )
return ERR_UNDEF;
(*bufp) += 2;
}
else
addr = address_fragment(start, bufp, offset);
if ( addr > ERR )
start = addr;
else if ( addr != ERR_UNKNOWN )
return addr;
c = **bufp;
logit(("initial fragment: addr=%d, c=%c", addr, c));
while ( c == '+' || c == '-' ) {
++(*bufp);
offset = (c == '+') ? 1 : -1;
addr = address_fragment(start, bufp, offset);
if ( addr > ERR )
start = addr;
else if ( addr == ERR_UNKNOWN ) {
logit(("after+ ERR_UNKNOWN (next char %d)", **bufp));
return nextline( (offset > 0), start, 1);
}
else
return addr;
c = **bufp;
}
return addr;
}
int
nextline(bool advance, int dest, int count)
{
int start = dest;
int ocount= count;
if (advance) {
do {
dest = fseekeol(dest) + 1;
count--;
} while (count>0 && dest<bufmax);
}
else {
do {
dest = bseekeol(dest) - 1;
count--;
} while (count>0 && dest>=0);
dest = bseekeol(dest);
}
logit(("nextline: advance=%d, start=%d, count=%d -> dest=%d",
advance, start, ocount, dest));
return(dest);
}
int
fseekeol(int origin)
{
#if 0
char *res = memchr(core+origin, EOL, bufmax-origin);
if ( res )
return res-core;
else
return bufmax;
#endif
return(origin + scan(bufmax-origin-1,'=',EOL,&core[origin]));
}
int
bseekeol(int origin)
{
return(origin + scan(-origin,'=',EOL,&core[origin-1]));
}
/* get something from the context table */
int
lvgetcontext(int c, bool begline)
{
int i;
if (c == '\'')
c = '`';
if (c >= '`' && c <= 'z')
i = contexts[c-'`'];
else
i = -1;
if (begline && i>=0)
return(bseekeol(i));
return(i);
}