1/* grep.c - print lines what match given regular expression
2 *
3 * Copyright 2013 CE Strake <strake888 at gmail.com>
4 *
5 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html
6 *
7 * TODO: -ABC
8
9USE_GREP(NEWTOY(grep, "C#B#A#ZzEFHabhinorsvwclqe*f*m#x[!wx][!EFw]", TOYFLAG_BIN))
10USE_EGREP(OLDTOY(egrep, grep, TOYFLAG_BIN))
11USE_FGREP(OLDTOY(fgrep, grep, TOYFLAG_BIN))
12
13config GREP
14  bool "grep"
15  default y
16  help
17    usage: grep [-EFivwcloqsHbhn] [-A NUM] [-m MAX] [-e REGEX]... [-f REGFILE] [FILE]...
18
19    Show lines matching regular expressions. If no -e, first argument is
20    regular expression to match. With no files (or "-" filename) read stdin.
21    Returns 0 if matched, 1 if no match found.
22
23    -e  Regex to match. (May be repeated.)
24    -f  File containing regular expressions to match.
25
26    match type:
27    -A  Show NUM lines after     -B  Show NUM lines before match
28    -C  NUM lines context (A+B)  -E  extended regex syntax
29    -F  fixed (literal match)    -i  case insensitive
30    -m  match MAX many lines     -r  recursive (on dir)
31    -v  invert match             -w  whole word (implies -E)
32    -x  whole line               -z  input NUL terminated
33
34    display modes: (default: matched line)
35    -c  count of matching lines  -l  show matching filenames
36    -o  only matching part       -q  quiet (errors only)
37    -s  silent (no error msg)    -Z  output NUL terminated
38
39    output prefix (default: filename if checking more than 1 file)
40    -H  force filename           -b  byte offset of match
41    -h  hide filename            -n  line number of match
42
43config EGREP
44  bool
45  default y
46  depends on GREP
47
48config FGREP
49  bool
50  default y
51  depends on GREP
52*/
53
54#define FOR_grep
55#include "toys.h"
56#include <regex.h>
57
58GLOBALS(
59  long m;
60  struct arg_list *f;
61  struct arg_list *e;
62  long a;
63  long b;
64  long c;
65
66  char indelim, outdelim;
67)
68
69// Emit line with various potential prefixes and delimiter
70static void outline(char *line, char dash, char *name, long lcount, long bcount,
71  int trim)
72{
73  if (name && (toys.optflags&FLAG_H)) printf("%s%c", name, dash);
74  if (!line || (lcount && (toys.optflags&FLAG_n)))
75    printf("%ld%c", lcount, line ? dash : TT.outdelim);
76  if (bcount && (toys.optflags&FLAG_b)) printf("%ld%c", bcount-1, dash);
77  if (line) xprintf("%.*s%c", trim, line, TT.outdelim);
78}
79
80// Show matches in one file
81static void do_grep(int fd, char *name)
82{
83  struct double_list *dlb = 0;
84  FILE *file = xfdopen(fd, "r");
85  long lcount = 0, mcount = 0, offset = 0, after = 0, before = 0;
86  char *bars = 0;
87
88  if (!fd) name = "(standard input)";
89
90  // Loop through lines of input
91  for (;;) {
92    char *line = 0, *start;
93    regmatch_t matches;
94    size_t unused;
95    long len;
96    int mmatch = 0;
97
98    lcount++;
99    if (0 > (len = getdelim(&line, &unused, TT.indelim, file))) break;
100    if (line[len-1] == TT.indelim) line[len-1] = 0;
101
102    start = line;
103
104    // Loop through matches in this line
105    do {
106      int rc = 0, skip = 0;
107
108      // Handle non-regex matches
109      if (toys.optflags & FLAG_F) {
110        struct arg_list *seek, fseek;
111        char *s = 0;
112
113        for (seek = TT.e; seek; seek = seek->next) {
114          if (toys.optflags & FLAG_x) {
115            int i = (toys.optflags & FLAG_i);
116
117            if ((i ? strcasecmp : strcmp)(seek->arg, line)) s = line;
118          } else if (!*seek->arg) {
119            seek = &fseek;
120            fseek.arg = s = line;
121            break;
122          }
123          if (toys.optflags & FLAG_i) s = strnstr(line, seek->arg);
124          else s = strstr(line, seek->arg);
125          if (s) break;
126        }
127
128        if (s) {
129          matches.rm_so = (s-line);
130          skip = matches.rm_eo = (s-line)+strlen(seek->arg);
131        } else rc = 1;
132      } else {
133        rc = regexec((regex_t *)toybuf, start, 1, &matches,
134                     start==line ? 0 : REG_NOTBOL);
135        skip = matches.rm_eo;
136      }
137
138      if (toys.optflags & FLAG_x)
139        if (matches.rm_so || line[matches.rm_eo]) rc = 1;
140
141      if (!rc && (toys.optflags & FLAG_w)) {
142        char c = 0;
143
144        if ((start+matches.rm_so)!=line) {
145          c = start[matches.rm_so-1];
146          if (!isalnum(c) && c != '_') c = 0;
147        }
148        if (!c) {
149          c = start[matches.rm_eo];
150          if (!isalnum(c) && c != '_') c = 0;
151        }
152        if (c) {
153          start += matches.rm_so+1;
154
155          continue;
156        }
157      }
158
159      if (toys.optflags & FLAG_v) {
160        if (toys.optflags & FLAG_o) {
161          if (rc) skip = matches.rm_eo = strlen(start);
162          else if (!matches.rm_so) {
163            start += skip;
164            continue;
165          } else matches.rm_eo = matches.rm_so;
166        } else {
167          if (!rc) break;
168          matches.rm_eo = strlen(start);
169        }
170        matches.rm_so = 0;
171      } else if (rc) break;
172
173      // At least one line we didn't print since match while -ABC active
174      if (bars) {
175        xputs(bars);
176        bars = 0;
177      }
178      mmatch++;
179      toys.exitval = 0;
180      if (toys.optflags & FLAG_q) xexit();
181      if (toys.optflags & FLAG_l) {
182        xprintf("%s%c", name, TT.outdelim);
183        free(line);
184        fclose(file);
185        return;
186      }
187      if (toys.optflags & FLAG_o)
188        if (matches.rm_eo == matches.rm_so)
189          break;
190
191      if (!(toys.optflags & FLAG_c)) {
192        long bcount = 1 + offset + (start-line) +
193          ((toys.optflags & FLAG_o) ? matches.rm_so : 0);
194
195        if (!(toys.optflags & FLAG_o)) {
196          while (dlb) {
197            struct double_list *dl = dlist_pop(&dlb);
198
199            outline(dl->data, '-', name, lcount-before, 0, -1);
200            free(dl->data);
201            free(dl);
202            before--;
203          }
204
205          outline(line, ':', name, lcount, bcount, -1);
206          if (TT.a) after = TT.a+1;
207        } else outline(start+matches.rm_so, ':', name, lcount, bcount,
208                       matches.rm_eo-matches.rm_so);
209      }
210
211      start += skip;
212      if (!(toys.optflags & FLAG_o)) break;
213    } while (*start);
214    offset += len;
215
216    if (mmatch) mcount++;
217    else {
218      int discard = (after || TT.b);
219
220      if (after && --after) {
221        outline(line, '-', name, lcount, 0, -1);
222        discard = 0;
223      }
224      if (discard && TT.b) {
225        dlist_add(&dlb, line);
226        line = 0;
227        if (++before>TT.b) {
228          struct double_list *dl;
229
230          dl = dlist_pop(&dlb);
231          free(dl->data);
232          free(dl);
233          before--;
234        } else discard = 0;
235      }
236      // If we discarded a line while displaying context, show bars before next
237      // line (but don't show them now in case that was last match in file)
238      if (discard && mcount) bars = "--";
239    }
240    free(line);
241
242    if ((toys.optflags & FLAG_m) && mcount >= TT.m) break;
243  }
244
245  if (toys.optflags & FLAG_c) outline(0, ':', name, mcount, 0, -1);
246
247  // loopfiles will also close the fd, but this frees an (opaque) struct.
248  fclose(file);
249}
250
251static void parse_regex(void)
252{
253  struct arg_list *al, *new, *list = NULL;
254  long len = 0;
255  char *s, *ss;
256
257  // Add all -f lines to -e list. (Yes, this is leaking allocation context for
258  // exit to free. Not supporting nofork for this command any time soon.)
259  al = TT.f ? TT.f : TT.e;
260  while (al) {
261    if (TT.f) s = ss = xreadfile(al->arg, 0, 0);
262    else s = ss = al->arg;
263
264    // Split lines at \n, add individual lines to new list.
265    do {
266      ss = strchr(s, '\n');
267      if (ss) *(ss++) = 0;
268      new = xmalloc(sizeof(struct arg_list));
269      new->next = list;
270      new->arg = s;
271      list = new;
272      s = ss;
273    } while (ss && *s);
274
275    // Advance, when we run out of -f switch to -e.
276    al = al->next;
277    if (!al && TT.f) {
278      TT.f = 0;
279      al = TT.e;
280    }
281  }
282  TT.e = list;
283
284  if (!(toys.optflags & FLAG_F)) {
285    char *regstr;
286    int i;
287
288    // Convert strings to one big regex
289    for (al = TT.e; al; al = al->next)
290      len += strlen(al->arg)+1+!(toys.optflags & FLAG_E);
291
292    regstr = s = xmalloc(len);
293    for (al = TT.e; al; al = al->next) {
294      s = stpcpy(s, al->arg);
295      if (!(toys.optflags & FLAG_E)) *(s++) = '\\';
296      *(s++) = '|';
297    }
298    *(s-=(1+!(toys.optflags & FLAG_E))) = 0;
299
300    i = regcomp((regex_t *)toybuf, regstr,
301                ((toys.optflags & FLAG_E) ? REG_EXTENDED : 0) |
302                ((toys.optflags & FLAG_i) ? REG_ICASE    : 0));
303
304    if (i) {
305      regerror(i, (regex_t *)toybuf, toybuf+sizeof(regex_t),
306               sizeof(toybuf)-sizeof(regex_t));
307      error_exit("bad REGEX: %s", toybuf);
308    }
309  }
310}
311
312static int do_grep_r(struct dirtree *new)
313{
314  char *name;
315
316  if (!dirtree_notdotdot(new)) return 0;
317  if (S_ISDIR(new->st.st_mode)) return DIRTREE_RECURSE;
318
319  // "grep -r onefile" doesn't show filenames, but "grep -r onedir" should.
320  if (new->parent && !(toys.optflags & FLAG_h)) toys.optflags |= FLAG_H;
321
322  name = dirtree_path(new, 0);
323  do_grep(openat(dirtree_parentfd(new), new->name, 0), name);
324  free(name);
325
326  return 0;
327}
328
329void grep_main(void)
330{
331  char **ss = toys.optargs;
332
333  if (!TT.a) TT.a = TT.c;
334  if (!TT.b) TT.b = TT.c;
335
336  TT.indelim = '\n' * !(toys.optflags&FLAG_z);
337  TT.outdelim = '\n' * !(toys.optflags&FLAG_Z);
338
339  // Handle egrep and fgrep
340  if (*toys.which->name == 'e') toys.optflags |= FLAG_E;
341  if (*toys.which->name == 'f') toys.optflags |= FLAG_F;
342
343  if (!TT.e && !TT.f) {
344    if (!*ss) error_exit("no REGEX");
345    TT.e = xzalloc(sizeof(struct arg_list));
346    TT.e->arg = *(ss++);
347    toys.optc--;
348  }
349
350  parse_regex();
351
352  if (!(toys.optflags & FLAG_h) && toys.optc>1) toys.optflags |= FLAG_H;
353
354  toys.exitval = 1;
355  if (toys.optflags & FLAG_s) {
356    close(2);
357    xopen_stdio("/dev/null", O_RDWR);
358  }
359
360  if (toys.optflags & FLAG_r) {
361    // Iterate through -r arguments. Use "." as default if none provided.
362    for (ss = *ss ? ss : (char *[]){".", 0}; *ss; ss++) {
363      if (!strcmp(*ss, "-")) do_grep(0, *ss);
364      else dirtree_read(*ss, do_grep_r);
365    }
366  } else loopfiles_rw(ss, O_RDONLY|WARN_ONLY, 0, do_grep);
367}
368