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#define FOR_grep
68#include "toys.h"
69
70GLOBALS(
71 long m, A, B, C;
72 struct arg_list *f, *e, *M, *S, *exclude_dir;
73 char *color;
74
75 char *purple, *cyan, *red, *green, *grey;
76 struct double_list *reg;
77 char indelim, outdelim;
78 int found, tried;
79)
80
81struct reg {
82 struct reg *next, *prev;
83 int rc;
84 regex_t r;
85 regmatch_t m;
86};
87
88static void numdash(long num, char dash)
89{
90 printf("%s%ld%s%c", TT.green, num, TT.cyan, dash);
91}
92
93
94static void outline(char *line, char dash, char *name, long lcount, long bcount,
95 unsigned trim)
96{
97 if (!trim && FLAG(o)) return;
98 if (name && FLAG(H)) printf("%s%s%s%c", TT.purple, name, TT.cyan, dash);
99 if (FLAG(c)) {
100 printf("%s%ld", TT.grey, lcount);
101 xputc(TT.outdelim);
102 } else if (lcount && FLAG(n)) numdash(lcount, dash);
103 if (bcount && FLAG(b)) numdash(bcount-1, dash);
104 if (line) {
105 if (FLAG(color)) xputsn(FLAG(o) ? TT.red : TT.grey);
106
107 xputsl(line, trim);
108 xputc(TT.outdelim);
109 }
110}
111
112
113static void do_grep(int fd, char *name)
114{
115 long lcount = 0, mcount = 0, offset = 0, after = 0, before = 0;
116 struct double_list *dlb = 0;
117 char *bars = 0;
118 FILE *file;
119 int bin = 0;
120
121 if (!FLAG(r)) TT.tried++;
122 if (!fd) name = "(standard input)";
123
124
125 if (!FLAG(a) && !lseek(fd, 0, SEEK_CUR)) {
126 char buf[256];
127 int len, i = 0;
128 unsigned wc;
129
130
131 if (0<(len = read(fd, buf, 256))) {
132 lseek(fd, -len, SEEK_CUR);
133 while (i<len) {
134 bin = utf8towc(&wc, buf+i, len-i);
135 if (bin == -2) i = len;
136 if (bin<1) break;
137 i += bin;
138 }
139 bin = i!=len;
140 }
141 if (bin && FLAG(I)) return;
142 }
143
144 if (!(file = fdopen(fd, "r"))) return perror_msg("%s", name);
145
146
147 for (;;) {
148 char *line = 0, *start;
149 struct reg *shoe;
150 size_t ulen;
151 long len;
152 int matched = 0, rc = 1;
153
154
155 lcount++;
156 errno = 0;
157 ulen = len = getdelim(&line, &ulen, TT.indelim, file);
158 if (len == -1 && errno) perror_msg("%s", name);
159 if (len<1) break;
160 if (line[ulen-1] == TT.indelim) line[--ulen] = 0;
161
162
163 start = line;
164 if (TT.reg) for (shoe = (void *)TT.reg; shoe; shoe = shoe->next)
165 shoe->rc = 0;
166
167
168 do {
169 regmatch_t *mm = (void *)toybuf;
170
171
172 if (FLAG(F)) {
173 struct arg_list *seek, fseek;
174 char *s = 0;
175
176 for (seek = TT.e; seek; seek = seek->next) {
177 if (FLAG(x)) {
178 if (!(FLAG(i) ? strcasecmp : strcmp)(seek->arg, line)) s = line;
179 } else if (!*seek->arg) {
180
181 seek = &fseek;
182 fseek.arg = s = line;
183 } else if (FLAG(i)) s = strcasestr(start, seek->arg);
184 else s = strstr(start, seek->arg);
185
186 if (s) break;
187 }
188
189 if (s) {
190 rc = 0;
191 mm->rm_so = (s-start);
192 mm->rm_eo = (s-start)+strlen(seek->arg);
193 } else rc = 1;
194
195
196 } else {
197 int baseline = mm->rm_eo;
198
199 mm->rm_so = mm->rm_eo = INT_MAX;
200 rc = 1;
201 for (shoe = (void *)TT.reg; shoe; shoe = shoe->next) {
202
203
204 if (!shoe->rc) {
205 shoe->m.rm_so -= baseline;
206 shoe->m.rm_eo -= baseline;
207 if (!matched || shoe->m.rm_so<0)
208 shoe->rc = regexec0(&shoe->r, start, ulen-(start-line), 1,
209 &shoe->m, start==line ? 0 : REG_NOTBOL);
210 }
211
212
213 if (!shoe->rc && (shoe->m.rm_so < mm->rm_so ||
214 (shoe->m.rm_so == mm->rm_so && shoe->m.rm_eo >= mm->rm_eo)))
215 {
216 mm = &shoe->m;
217 rc = 0;
218 }
219 }
220 }
221
222 if (!rc && FLAG(o) && !mm->rm_eo && ulen>start-line) {
223 start++;
224 continue;
225 }
226
227 if (!rc && FLAG(x) && (mm->rm_so || ulen-(start-line)!=mm->rm_eo)) rc = 1;
228
229 if (!rc && FLAG(w)) {
230 char c = 0;
231
232 if ((start+mm->rm_so)!=line) {
233 c = start[mm->rm_so-1];
234 if (!isalnum(c) && c != '_') c = 0;
235 }
236 if (!c) {
237 c = start[mm->rm_eo];
238 if (!isalnum(c) && c != '_') c = 0;
239 }
240 if (c) {
241 start += mm->rm_so+1;
242 continue;
243 }
244 }
245
246 if (FLAG(v)) {
247 if (FLAG(o)) {
248 if (rc) mm->rm_eo = ulen-(start-line);
249 else if (!mm->rm_so) {
250 start += mm->rm_eo;
251 continue;
252 } else mm->rm_eo = mm->rm_so;
253 } else {
254 if (!rc) break;
255 mm->rm_eo = ulen-(start-line);
256 }
257 mm->rm_so = 0;
258 } else if (rc) break;
259
260
261 if (bars) {
262 xputs(bars);
263 bars = 0;
264 }
265 matched++;
266 TT.found = 1;
267
268
269 if (FLAG(q)) {
270 toys.exitval = 0;
271 xexit();
272 }
273 if (FLAG(L) || FLAG(l)) {
274 if (FLAG(l)) xprintf("%s%c", name, TT.outdelim);
275 free(line);
276 fclose(file);
277 return;
278 }
279
280 if (!FLAG(c)) {
281 long bcount = 1 + offset + (start-line) + (FLAG(o) ? mm->rm_so : 0);
282
283 if (bin) printf("Binary file %s matches\n", name);
284 else if (FLAG(o))
285 outline(start+mm->rm_so, ':', name, lcount, bcount,
286 mm->rm_eo-mm->rm_so);
287 else {
288 while (dlb) {
289 struct double_list *dl = dlist_pop(&dlb);
290 unsigned *uu = (void *)(dl->data+(strlen(dl->data)|3)+1);
291
292 outline(dl->data, '-', name, lcount-before, uu[0]+1, uu[1]);
293 free(dl->data);
294 free(dl);
295 before--;
296 }
297
298 if (matched==1)
299 outline(FLAG(color) ? 0 : line, ':', name, lcount, bcount, ulen);
300 if (FLAG(color)) {
301 xputsn(TT.grey);
302 if (mm->rm_so) xputsl(start, mm->rm_so);
303 xputsn(TT.red);
304 xputsl(start+mm->rm_so, mm->rm_eo-mm->rm_so);
305 }
306
307 if (TT.A) after = TT.A+1;
308 }
309 }
310
311 start += mm->rm_eo;
312 if (mm->rm_so == mm->rm_eo) break;
313 } while (*start);
314 offset += len;
315
316 if (matched) {
317
318 if (FLAG(color) && !FLAG(o)) {
319 xputsn(TT.grey);
320 if (ulen > start-line) xputsl(start, ulen-(start-line));
321 xputc(TT.outdelim);
322 }
323 mcount++;
324 } else {
325 int discard = (after || TT.B);
326
327 if (after && --after) {
328 outline(line, '-', name, lcount, 0, ulen);
329 discard = 0;
330 }
331 if (discard && TT.B) {
332 unsigned *uu, ul = (ulen|3)+1;
333
334 line = xrealloc(line, ul+8);
335 uu = (void *)(line+ul);
336 uu[0] = offset-len;
337 uu[1] = ulen;
338 dlist_add(&dlb, line);
339 line = 0;
340 if (++before>TT.B) {
341 struct double_list *dl;
342
343 dl = dlist_pop(&dlb);
344 free(dl->data);
345 free(dl);
346 before--;
347 } else discard = 0;
348 }
349
350
351 if (discard && mcount) bars = "--";
352 }
353 free(line);
354
355 if (FLAG(m) && mcount >= TT.m) break;
356 }
357
358 if (FLAG(L)) xprintf("%s%c", name, TT.outdelim);
359 else if (FLAG(c)) outline(0, ':', name, mcount, 0, 1);
360
361
362 fclose(file);
363 while (dlb) {
364 struct double_list *dl = dlist_pop(&dlb);
365
366 free(dl->data);
367 free(dl);
368 }
369}
370
371static void parse_regex(void)
372{
373 struct arg_list *al, *new, *list = NULL;
374 char *s, *ss;
375
376
377
378 al = TT.f ? TT.f : TT.e;
379 while (al) {
380 if (TT.f) {
381 if (!*(s = ss = xreadfile(al->arg, 0, 0))) {
382 free(ss);
383 s = 0;
384 }
385 } else s = ss = al->arg;
386
387
388 al = al->next;
389 if (!al && TT.f) {
390 TT.f = 0;
391 al = TT.e;
392 }
393 if (!s) continue;
394
395
396 do {
397 ss = FLAG(z) ? 0 : strchr(s, '\n');
398 if (ss) *(ss++) = 0;
399 new = xmalloc(sizeof(struct arg_list));
400 new->next = list;
401 new->arg = s;
402 list = new;
403 s = ss;
404 } while (ss && *s);
405 }
406 TT.e = list;
407
408 if (!FLAG(F)) {
409
410 for (al = TT.e; al; al = al->next) {
411 struct reg *shoe;
412
413 if (FLAG(o) && !*al->arg) continue;
414 dlist_add_nomalloc(&TT.reg, (void *)(shoe = xmalloc(sizeof(struct reg))));
415 xregcomp(&shoe->r, al->arg,
416 (REG_EXTENDED*!!FLAG(E))|(REG_ICASE*!!FLAG(i)));
417 }
418 dlist_terminate(TT.reg);
419 }
420}
421
422static int do_grep_r(struct dirtree *new)
423{
424 struct arg_list *al;
425 char *name;
426
427 if (!new->parent) TT.tried++;
428 if (!dirtree_notdotdot(new)) return 0;
429 if (S_ISDIR(new->st.st_mode)) {
430 for (al = TT.exclude_dir; al; al = al->next)
431 if (!fnmatch(al->arg, new->name, 0)) return 0;
432 return DIRTREE_RECURSE|(FLAG(R)?DIRTREE_SYMFOLLOW:0);
433 }
434 if (TT.S || TT.M) {
435 for (al = TT.S; al; al = al->next)
436 if (!fnmatch(al->arg, new->name, 0)) return 0;
437
438 if (TT.M) {
439 for (al = TT.M; al; al = al->next)
440 if (!fnmatch(al->arg, new->name, 0)) break;
441
442 if (!al) return 0;
443 }
444 }
445
446
447 if (new->parent && !FLAG(h)) toys.optflags |= FLAG_H;
448
449 name = dirtree_path(new, 0);
450 do_grep(openat(dirtree_parentfd(new), new->name, 0), name);
451 free(name);
452
453 return 0;
454}
455
456void grep_main(void)
457{
458 char **ss = toys.optargs;
459
460 if (FLAG(color) && (!TT.color || !strcmp(TT.color, "auto")) && !isatty(1))
461 toys.optflags &= ~FLAG_color;
462
463 if (FLAG(color)) {
464 TT.purple = "\e[35m";
465 TT.cyan = "\e[36m";
466 TT.red = "\e[1;31m";
467 TT.green = "\e[32m";
468 TT.grey = "\e[m";
469 } else TT.purple = TT.cyan = TT.red = TT.green = TT.grey = "";
470
471 if (FLAG(R)) toys.optflags |= FLAG_r;
472
473
474 toys.exitval = 2;
475
476 if (!TT.A) TT.A = TT.C;
477 if (!TT.B) TT.B = TT.C;
478
479 TT.indelim = '\n' * !FLAG(z);
480 TT.outdelim = '\n' * !FLAG(Z);
481
482
483 if (*toys.which->name == 'e') toys.optflags |= FLAG_E;
484 if (*toys.which->name == 'f') toys.optflags |= FLAG_F;
485
486 if (!TT.e && !TT.f) {
487 if (!*ss) error_exit("no REGEX");
488 TT.e = xzalloc(sizeof(struct arg_list));
489 TT.e->arg = *(ss++);
490 toys.optc--;
491 }
492
493 parse_regex();
494
495 if (!FLAG(h) && toys.optc>1) toys.optflags |= FLAG_H;
496
497 if (FLAG(s)) {
498 close(2);
499 xopen_stdio("/dev/null", O_RDWR);
500 }
501
502 if (FLAG(r)) {
503
504 for (ss = *ss ? ss : (char *[]){".", 0}; *ss; ss++) {
505 if (!strcmp(*ss, "-")) do_grep(0, *ss);
506 else dirtree_read(*ss, do_grep_r);
507 }
508 } else loopfiles_rw(ss, O_RDONLY|WARN_ONLY, 0, do_grep);
509 if (TT.tried >= toys.optc || (FLAG(q)&&TT.found)) toys.exitval = !TT.found;
510}
511