-
Notifications
You must be signed in to change notification settings - Fork 1
/
siv.c
348 lines (290 loc) · 6.54 KB
/
siv.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
/*
* siv
*
* multi-layer regular expression matching
*/
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <dirent.h>
#include <stdarg.h>
#include <utf.h>
#include <fmt.h>
#include <regexp9.h>
#include <bio.h>
#include <sre.h>
#define USAGE "usage: %s [-rlhG] [-t [0-9]] [-e expression]... [expression] [files...]\n"
#define REMAX 10
#define STATIC_DEPTH 32
#define DYNAMIC_DEPTH 128
#define MAX_PATH_LEN 4096
typedef struct {
char *cp;
DIR *dp;
} Rframe; /* recursion stack frame */
char *name;
char path[MAX_PATH_LEN + 1];
Biobuf inb, outb;
Reprog *progarr[REMAX];
Rframe sstack[STATIC_DEPTH];
Rframe *dstack;
Rframe *stack;
/* siv stuff */
char *wp;
size_t wsize;
int depth;
struct dirent *ent;
struct stat buf;
int d; /* depth in directory recursion stack */
int fd;
int t;
int n; /* number of expressions */
int recur;
int locat;
void myerror(char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprint(2, fmt, args);
va_end(args);
exit(1);
}
char* escape(char *s) {
int i, j;
for(i = 0, j = 0; s[i]; ++i, ++j) {
if(s[i] != '\\') {
s[j] = s[i];
continue;
}
switch(s[i + 1]) {
case 'a':
s[j] = '\a';
++i;
break;
case 'b':
s[j] = '\b';
++i;
break;
case 't':
s[j] = '\t';
++i;
break;
case 'n':
s[j] = '\n';
++i;
break;
case 'v':
s[j] = '\v';
++i;
break;
case 'f':
s[j] = '\f';
++i;
break;
case 'r':
s[j] = '\r';
++i;
break;
case '\\':
s[j] = '\\';
++i;
break;
default:
s[j] = s[i];
continue;
}
}
while(i > j)
s[j++] = 0;
return s;
}
void siv(Reprog *rearr[REMAX], Biobuf *inb, Biobuf *outb, int depth, int t, char **wp, size_t *wsize) {
Resub stack[REMAX-2];
char locatbuf[256];
Resub range, target;
Bresub offset;
long start, end;
Reprog *base, **arr;
int locatlen;
size_t wlen;
int i;
--depth; /* sub 1 because stack is only used starting from second regex */
base = *rearr;
arr = rearr + 1;
while((wlen = Bgetre(inb, base, 0, 0, &offset, wp, wsize)) > 0) {
start = offset.s;
end = offset.e;
stack[0] = (Resub){0};
i = 0;
while(i >= 0) {
range = stack[i];
if(depth >= 0 && !strgetre(*wp, arr[i], &range, 1)) {
--i;
continue;
}
if(t != 0 && (i + 1) == t) { /* don't save range if target is at base */
target = range;
start += range.s.sp - *wp;
end -= *wp + wlen - range.e.ep - 1;
}
stack[i].s.sp = range.e.ep;
if(i < depth) {
stack[++i] = range;
continue;
}
if(locat) {
locatlen = sprint(locatbuf, "@@ %s,%li,%li @@", path, start, end);
Bwrite(outb, locatbuf, locatlen);
}
if(t == 0) {
for(char *p = *wp; p - *wp < wlen; ++p) {
if(*p == 0) {
Bprint(outb, "%s: binary file matches\n", name);
break;
}
}
Bwrite(outb, *wp, wlen);
break;
}
Bwrite(outb, target.s.sp, target.e.ep - target.s.sp);
i = t - 1; /* remember that i is offset from actual regexp index */
}
}
}
void cleanup(void) {
register int i;
for(i = 0; i < n; ++i)
free(progarr[i]);
if(dstack)
free(dstack);
free(inb.bbuf - Bungetsize);
free(wp);
Bterm(&inb);
Bterm(&outb);
}
int main(int argc, char *argv[]) {
name = argv[0];
if(argc == 1)
myerror(USAGE, name);
atexit(cleanup);
inb.bbuf = (unsigned char *)(malloc(Bsize)) + Bungetsize;
inb.bsize = Bsize - Bungetsize;
wp = malloc((wsize = 1024));
Binit(&outb, 1, O_WRONLY);
stack = sstack;
dstack = 0;
fd = 0;
t = -2;
n = 0;
recur = 0;
locat = 0;
int greedy = 0;
size_t optind;
for(optind = 1; optind < argc && argv[optind][0] == '-'; ++optind) {
switch(argv[optind][1]) {
case 'e':
if(++optind == argc)
myerror("%s: '-e' requires an argument\n", name);
if(n >= REMAX)
myerror("%s: too expressions given\n", name);
progarr[n++] = regcompnlg(escape(argv[optind]), greedy);
break;
case 'r':
recur = 1;
break;
case 'l':
locat = 1;
break;
case 't':
if(++optind == argc)
myerror("%s: '-t' requires an argument\n", name);
t = atoi(argv[optind]);
if(*argv[optind] != '0' && t == 0)
myerror("%s: '-t' invalid target index '%s'\n", name, argv[optind]);
break;
case 'G': /* make matches greedy by default */
greedy = 1;
break;
case 'h':
myerror(USAGE, name);
default:
myerror("%s: unknown option %s\n%s", name, argv[optind], USAGE);
}
}
if(n == 0 && optind < argc)
progarr[n++] = regcompnlg(escape(argv[optind++]), greedy);
if(t < 0)
t += n > 1 ? n : 2;
if(t > n - 1)
myerror("%s: index %i out of range\n", name, t);
depth = n - 1; /* set max depth for siv */
if(optind == argc) {
Binits(&inb, 0, O_RDONLY, inb.bbuf - Bungetsize, inb.bsize + Bungetsize);
strcpy(path, "<stdin>");
siv(progarr, &inb, &outb, depth, t, &wp, &wsize);
return 0;
}
for(; optind < argc; ++optind) {
strcpy(path, argv[optind]);
if(stat(path, &buf) == -1) {
fprint(2, "%s: %s: could not stat\n", name, path);
continue;
}
if(S_ISREG(buf.st_mode)) {
fd = open(path, O_RDONLY);
Binits(&inb, fd, O_RDONLY, inb.bbuf - Bungetsize, inb.bsize + Bungetsize);
siv(progarr, &inb, &outb, depth, t, &wp, &wsize);
close(fd);
continue;
}
if(!S_ISDIR(buf.st_mode)) {
fprint(2, "%s: %s: not file or directory\n", name, path);
continue;
}
if(!recur) {
fprint(2, "%s: %s: is a directory\n", name, path);
continue;
}
d = 0;
stack[d].cp = path + strlen(path);
if(*(stack[d].cp - 1) != '/')
*(stack[d].cp++) = '/';
stack[d].dp = opendir(path);
while(d > -1) {
while((ent = readdir(stack[d].dp)) != NULL) {
if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
continue;
strcpy(stack[d].cp, ent->d_name);
stat(path, &buf);
if(S_ISDIR(buf.st_mode)) {
++d;
if(dstack == 0 && d >= STATIC_DEPTH) {
dstack = malloc(DYNAMIC_DEPTH * sizeof(Rframe));
memcpy(dstack, sstack, STATIC_DEPTH * sizeof(Rframe));
stack = dstack;
}
if(dstack != 0 && d >= DYNAMIC_DEPTH) {
fprint(2, "%s: max directory recursion depth reached\n", name);
--d;
continue;
}
stack[d].cp = stack[d - 1].cp + strlen(ent->d_name);
stack[d].dp = opendir(path);
*(stack[d].cp++) = '/';
continue;
}
if(S_ISREG(buf.st_mode)) {
fd = open(path, O_RDONLY);
Binits(&inb, fd, O_RDONLY, inb.bbuf - Bungetsize, inb.bsize + Bungetsize);
siv(progarr, &inb, &outb, depth, t, &wp, &wsize);
close(fd);
}
}
closedir(stack[d--].dp);
}
}
return 0;
}