5 void append_grep_pattern(struct grep_opt *opt, const char *pat,
6 const char *origin, int no, enum grep_pat_token t)
8 struct grep_pat *p = xcalloc(1, sizeof(*p));
13 *opt->pattern_tail = p;
14 opt->pattern_tail = &p->next;
18 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
20 int err = regcomp(&p->regexp, p->pattern, opt->regflags);
25 sprintf(where, "In '%s' at %d, ",
28 sprintf(where, "%s, ", p->origin);
31 regerror(err, &p->regexp, errbuf, 1024);
33 die("%s'%s': %s", where, p->pattern, errbuf);
37 static struct grep_expr *compile_pattern_expr(struct grep_pat **);
38 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
45 case GREP_PATTERN: /* atom */
46 x = xcalloc(1, sizeof (struct grep_expr));
47 x->node = GREP_NODE_ATOM;
53 x = compile_pattern_expr(list);
56 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
57 die("unmatched parenthesis");
58 *list = (*list)->next;
65 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
74 die("--not not followed by pattern expression");
76 x = xcalloc(1, sizeof (struct grep_expr));
77 x->node = GREP_NODE_NOT;
78 x->u.unary = compile_pattern_not(list);
80 die("--not followed by non pattern expression");
83 return compile_pattern_atom(list);
87 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
90 struct grep_expr *x, *y, *z;
92 x = compile_pattern_not(list);
94 if (p && p->token == GREP_AND) {
96 die("--and not followed by pattern expression");
98 y = compile_pattern_and(list);
100 die("--and not followed by pattern expression");
101 z = xcalloc(1, sizeof (struct grep_expr));
102 z->node = GREP_NODE_AND;
103 z->u.binary.left = x;
104 z->u.binary.right = y;
110 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
113 struct grep_expr *x, *y, *z;
115 x = compile_pattern_and(list);
117 if (x && p && p->token != GREP_CLOSE_PAREN) {
118 y = compile_pattern_or(list);
120 die("not a pattern expression %s", p->pattern);
121 z = xcalloc(1, sizeof (struct grep_expr));
122 z->node = GREP_NODE_OR;
123 z->u.binary.left = x;
124 z->u.binary.right = y;
130 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
132 return compile_pattern_or(list);
135 void compile_grep_patterns(struct grep_opt *opt)
142 /* First compile regexps */
143 for (p = opt->pattern_list; p; p = p->next) {
144 if (p->token == GREP_PATTERN)
145 compile_regexp(p, opt);
153 /* Then bundle them up in an expression.
154 * A classic recursive descent parser would do.
156 p = opt->pattern_list;
157 opt->pattern_expression = compile_pattern_expr(&p);
159 die("incomplete pattern expression: %s", p->pattern);
162 static char *end_of_line(char *cp, unsigned long *left)
164 unsigned long l = *left;
165 while (l && *cp != '\n') {
173 static int word_char(char ch)
175 return isalnum(ch) || ch == '_';
178 static void show_line(struct grep_opt *opt, const char *bol, const char *eol,
179 const char *name, unsigned lno, char sign)
182 printf("%s%c", name, sign);
184 printf("%d%c", lno, sign);
185 printf("%.*s\n", (int)(eol-bol), bol);
189 * NEEDSWORK: share code with diff.c
191 #define FIRST_FEW_BYTES 8000
192 static int buffer_is_binary(const char *ptr, unsigned long size)
194 if (FIRST_FEW_BYTES < size)
195 size = FIRST_FEW_BYTES;
196 return !!memchr(ptr, 0, size);
199 static int fixmatch(const char *pattern, char *line, regmatch_t *match)
201 char *hit = strstr(line, pattern);
203 match->rm_so = match->rm_eo = -1;
207 match->rm_so = hit - line;
208 match->rm_eo = match->rm_so + strlen(pattern);
213 static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol)
217 regmatch_t pmatch[10];
221 regex_t *exp = &p->regexp;
222 hit = !regexec(exp, bol, ARRAY_SIZE(pmatch),
226 hit = !fixmatch(p->pattern, bol, pmatch);
229 if (hit && opt->word_regexp) {
230 if ((pmatch[0].rm_so < 0) ||
231 (eol - bol) <= pmatch[0].rm_so ||
232 (pmatch[0].rm_eo < 0) ||
233 (eol - bol) < pmatch[0].rm_eo)
234 die("regexp returned nonsense");
236 /* Match beginning must be either beginning of the
237 * line, or at word boundary (i.e. the last char must
238 * not be a word char). Similarly, match end must be
239 * either end of the line, or at word boundary
240 * (i.e. the next char must not be a word char).
242 if ( ((pmatch[0].rm_so == 0 && at_true_bol) ||
243 !word_char(bol[pmatch[0].rm_so-1])) &&
244 ((pmatch[0].rm_eo == (eol-bol)) ||
245 !word_char(bol[pmatch[0].rm_eo])) )
250 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
251 /* There could be more than one match on the
252 * line, and the first match might not be
253 * strict word match. But later ones could be!
255 bol = pmatch[0].rm_so + bol + 1;
263 static int match_expr_eval(struct grep_opt *opt,
265 char *bol, char *eol)
269 return match_one_pattern(opt, x->u.atom, bol, eol);
272 return !match_expr_eval(opt, x->u.unary, bol, eol);
274 return (match_expr_eval(opt, x->u.binary.left, bol, eol) &&
275 match_expr_eval(opt, x->u.binary.right, bol, eol));
277 return (match_expr_eval(opt, x->u.binary.left, bol, eol) ||
278 match_expr_eval(opt, x->u.binary.right, bol, eol));
280 die("Unexpected node type (internal error) %d\n", x->node);
283 static int match_expr(struct grep_opt *opt, char *bol, char *eol)
285 struct grep_expr *x = opt->pattern_expression;
286 return match_expr_eval(opt, x, bol, eol);
289 static int match_line(struct grep_opt *opt, char *bol, char *eol)
293 return match_expr(opt, bol, eol);
294 for (p = opt->pattern_list; p; p = p->next) {
295 if (match_one_pattern(opt, p, bol, eol))
301 int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size)
304 unsigned long left = size;
306 struct pre_context_line {
309 } *prev = NULL, *pcl;
310 unsigned last_hit = 0;
311 unsigned last_shown = 0;
312 int binary_match_only = 0;
313 const char *hunk_mark = "";
316 if (buffer_is_binary(buf, size)) {
317 switch (opt->binary) {
318 case GREP_BINARY_DEFAULT:
319 binary_match_only = 1;
321 case GREP_BINARY_NOMATCH:
322 return 0; /* Assume unmatch */
329 if (opt->pre_context)
330 prev = xcalloc(opt->pre_context, sizeof(*prev));
331 if (opt->pre_context || opt->post_context)
338 eol = end_of_line(bol, &left);
342 hit = match_line(opt, bol, eol);
345 /* "grep -v -e foo -e bla" should list lines
346 * that do not have either, so inversion should
351 if (opt->unmatch_name_only) {
358 if (opt->status_only)
360 if (binary_match_only) {
361 printf("Binary file %s matches\n", name);
364 if (opt->name_only) {
365 printf("%s\n", name);
368 /* Hit at this line. If we haven't shown the
369 * pre-context lines, we would need to show them.
370 * When asked to do "count", this still show
371 * the context which is nonsense, but the user
372 * deserves to get that ;-).
374 if (opt->pre_context) {
376 if (opt->pre_context < lno)
377 from = lno - opt->pre_context;
380 if (from <= last_shown)
381 from = last_shown + 1;
382 if (last_shown && from != last_shown + 1)
385 pcl = &prev[lno-from-1];
386 show_line(opt, pcl->bol, pcl->eol,
392 if (last_shown && lno != last_shown + 1)
395 show_line(opt, bol, eol, name, lno, ':');
396 last_shown = last_hit = lno;
399 lno <= last_hit + opt->post_context) {
400 /* If the last hit is within the post context,
401 * we need to show this line.
403 if (last_shown && lno != last_shown + 1)
405 show_line(opt, bol, eol, name, lno, '-');
408 if (opt->pre_context) {
409 memmove(prev+1, prev,
410 (opt->pre_context-1) * sizeof(*prev));
423 if (opt->status_only)
425 if (opt->unmatch_name_only) {
426 /* We did not see any hit, so we want to show this */
427 printf("%s\n", name);
432 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
433 * which feels mostly useless but sometimes useful. Maybe
434 * make it another option? For now suppress them.
436 if (opt->count && count)
437 printf("%s:%u\n", name, count);