• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* grep.c - show lines matching regular expressions
2  *
3  * Copyright 2013 CE Strake <strake888 at gmail.com>
4  *
5  * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html
6  *
7  * Posix doesn't even specify -r, documenting deviations from it is silly.
8 * echo hello | grep -w ''
9 * echo '' | grep -w ''
10 * echo hello | grep -f </dev/null
11 *
12 
13 USE_GREP(NEWTOY(grep, "(line-buffered)(color):;(exclude-dir)*S(exclude)*M(include)*ZzEFHIab(byte-offset)h(no-filename)ino(only-matching)rRsvwcL(files-without-match)l(files-with-matches)q(quiet)(silent)e*f*C#B#A#m#x[!wx][!EF]", TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)|TOYFLAG_LINEBUF))
14 USE_EGREP(OLDTOY(egrep, grep, TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)|TOYFLAG_LINEBUF))
15 USE_FGREP(OLDTOY(fgrep, grep, TOYFLAG_BIN|TOYFLAG_ARGFAIL(2)|TOYFLAG_LINEBUF))
16 
17 config GREP
18   bool "grep"
19   default y
20   help
21     usage: grep [-EFrivwcloqsHbhn] [-ABC NUM] [-m MAX] [-e REGEX]... [-MS PATTERN]... [-f REGFILE] [FILE]...
22 
23     Show lines matching regular expressions. If no -e, first argument is
24     regular expression to match. With no files (or "-" filename) read stdin.
25     Returns 0 if matched, 1 if no match found, 2 for command errors.
26 
27     -e  Regex to match. (May be repeated.)
28     -f  File listing regular expressions to match.
29 
30     file search:
31     -r  Recurse into subdirectories (defaults FILE to ".")
32     -R  Recurse into subdirectories and symlinks to directories
33     -M  Match filename pattern (--include)
34     -S  Skip filename pattern (--exclude)
35     --exclude-dir=PATTERN  Skip directory pattern
36     -I  Ignore binary files
37 
38     match type:
39     -A  Show NUM lines after     -B  Show NUM lines before match
40     -C  NUM lines context (A+B)  -E  extended regex syntax
41     -F  fixed (literal match)    -a  always text (not binary)
42     -i  case insensitive         -m  match MAX many lines
43     -v  invert match             -w  whole word (implies -E)
44     -x  whole line               -z  input NUL terminated
45 
46     display modes: (default: matched line)
47     -L  filenames with no match  -Z  output is NUL terminated
48     -c  count of matching lines  -l  filenames with a match
49     -o  only matching part       -q  quiet (errors only)
50     -s  silent (no error msg)
51 
52     output prefix (default: filename if checking more than 1 file)
53     -H  force filename           -b  byte offset of match
54     -h  hide filename            -n  line number of match
55 
56 config EGREP
57   bool
58   default y
59   depends on GREP
60 
61 config FGREP
62   bool
63   default y
64   depends on GREP
65 */
66 
67 #define FOR_grep
68 #include "toys.h"
69 
70 GLOBALS(
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 
81 struct reg {
82   struct reg *next, *prev;
83   int rc;
84   regex_t r;
85   regmatch_t m;
86 };
87 
numdash(long num,char dash)88 static void numdash(long num, char dash)
89 {
90   printf("%s%ld%s%c", TT.green, num, TT.cyan, dash);
91 }
92 
93 // Emit line with various potential prefixes and delimiter
outline(char * line,char dash,char * name,long lcount,long bcount,unsigned trim)94 static 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     // support embedded NUL bytes in output
107     xputsl(line, trim);
108     xputc(TT.outdelim);
109   }
110 }
111 
112 // Show matches in one file
do_grep(int fd,char * name)113 static 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   // Only run binary file check on lseekable files.
125   if (!FLAG(a) && !lseek(fd, 0, SEEK_CUR)) {
126     char buf[256];
127     int len, i = 0;
128     unsigned wc;
129 
130     // If the first 256 bytes don't parse as utf8, call it binary.
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   // Loop through lines of input
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     // get next line, check and trim delimiter
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     // Prepare for next line
163     start = line;
164     if (TT.reg) for (shoe = (void *)TT.reg; shoe; shoe = shoe->next)
165       shoe->rc = 0;
166 
167     // Loop to handle multiple matches in same line
168     do {
169       regmatch_t *mm = (void *)toybuf;
170 
171       // Handle "fixed" (literal) matches
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             // No need to set fseek.next because this will match every line.
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       // Handle regex matches
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           // Do we need to re-check this regex?
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           // If we got a match, is it a _better_ match?
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       // At least one line we didn't print since match while -ABC active
261       if (bars) {
262         xputs(bars);
263         bars = 0;
264       }
265       matched++;
266       TT.found = 1;
267 
268       // Are we NOT showing the matching text?
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       // Finish off pending line color fragment.
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       // If we discarded a line while displaying context, show bars before next
350       // line (but don't show them now in case that was last match in file)
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   // loopfiles will also close the fd, but this frees an (opaque) struct.
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 
parse_regex(void)371 static void parse_regex(void)
372 {
373   struct arg_list *al, *new, *list = NULL;
374   char *s, *ss;
375 
376   // Add all -f lines to -e list. (Yes, this is leaking allocation context for
377   // exit to free. Not supporting nofork for this command any time soon.)
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     // Advance, when we run out of -f switch to -e.
388     al = al->next;
389     if (!al && TT.f) {
390       TT.f = 0;
391       al = TT.e;
392     }
393     if (!s) continue;
394 
395     // Split lines at \n, add individual lines to new list.
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     // Convert regex list
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 
do_grep_r(struct dirtree * new)422 static 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   // "grep -r onefile" doesn't show filenames, but "grep -r onedir" should.
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 
grep_main(void)456 void 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   // Grep exits with 2 for errors
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   // Handle egrep and fgrep
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     // Iterate through -r arguments. Use "." as default if none provided.
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