]>
Commit | Line | Data |
---|---|---|
83b5d2f5 | 1 | #include "cache.h" |
83b5d2f5 | 2 | #include "grep.h" |
60ecac98 | 3 | #include "userdiff.h" |
6bfce93e | 4 | #include "xdiff-interface.h" |
83b5d2f5 | 5 | |
a4d7d2c6 JH |
6 | void append_header_grep_pattern(struct grep_opt *opt, enum grep_header_field field, const char *pat) |
7 | { | |
8 | struct grep_pat *p = xcalloc(1, sizeof(*p)); | |
9 | p->pattern = pat; | |
ed40a095 | 10 | p->patternlen = strlen(pat); |
a4d7d2c6 JH |
11 | p->origin = "header"; |
12 | p->no = 0; | |
13 | p->token = GREP_PATTERN_HEAD; | |
14 | p->field = field; | |
80235ba7 JH |
15 | *opt->header_tail = p; |
16 | opt->header_tail = &p->next; | |
a4d7d2c6 JH |
17 | p->next = NULL; |
18 | } | |
19 | ||
83b5d2f5 JH |
20 | void append_grep_pattern(struct grep_opt *opt, const char *pat, |
21 | const char *origin, int no, enum grep_pat_token t) | |
ed40a095 RS |
22 | { |
23 | append_grep_pat(opt, pat, strlen(pat), origin, no, t); | |
24 | } | |
25 | ||
26 | void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, | |
27 | const char *origin, int no, enum grep_pat_token t) | |
83b5d2f5 JH |
28 | { |
29 | struct grep_pat *p = xcalloc(1, sizeof(*p)); | |
30 | p->pattern = pat; | |
ed40a095 | 31 | p->patternlen = patlen; |
83b5d2f5 JH |
32 | p->origin = origin; |
33 | p->no = no; | |
34 | p->token = t; | |
35 | *opt->pattern_tail = p; | |
36 | opt->pattern_tail = &p->next; | |
37 | p->next = NULL; | |
38 | } | |
39 | ||
5b594f45 FK |
40 | struct grep_opt *grep_opt_dup(const struct grep_opt *opt) |
41 | { | |
42 | struct grep_pat *pat; | |
43 | struct grep_opt *ret = xmalloc(sizeof(struct grep_opt)); | |
44 | *ret = *opt; | |
45 | ||
46 | ret->pattern_list = NULL; | |
47 | ret->pattern_tail = &ret->pattern_list; | |
48 | ||
49 | for(pat = opt->pattern_list; pat != NULL; pat = pat->next) | |
50 | { | |
51 | if(pat->token == GREP_PATTERN_HEAD) | |
52 | append_header_grep_pattern(ret, pat->field, | |
53 | pat->pattern); | |
54 | else | |
ed40a095 RS |
55 | append_grep_pat(ret, pat->pattern, pat->patternlen, |
56 | pat->origin, pat->no, pat->token); | |
5b594f45 FK |
57 | } |
58 | ||
59 | return ret; | |
60 | } | |
61 | ||
a30c148a MK |
62 | static NORETURN void compile_regexp_failed(const struct grep_pat *p, |
63 | const char *error) | |
64 | { | |
65 | char where[1024]; | |
66 | ||
67 | if (p->no) | |
68 | sprintf(where, "In '%s' at %d, ", p->origin, p->no); | |
69 | else if (p->origin) | |
70 | sprintf(where, "%s, ", p->origin); | |
71 | else | |
72 | where[0] = 0; | |
73 | ||
74 | die("%s'%s': %s", where, p->pattern, error); | |
75 | } | |
76 | ||
63e7e9d8 MK |
77 | #ifdef USE_LIBPCRE |
78 | static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt) | |
79 | { | |
80 | const char *error; | |
81 | int erroffset; | |
82 | int options = 0; | |
83 | ||
84 | if (opt->ignore_case) | |
85 | options |= PCRE_CASELESS; | |
86 | ||
87 | p->pcre_regexp = pcre_compile(p->pattern, options, &error, &erroffset, | |
88 | NULL); | |
89 | if (!p->pcre_regexp) | |
90 | compile_regexp_failed(p, error); | |
91 | ||
92 | p->pcre_extra_info = pcre_study(p->pcre_regexp, 0, &error); | |
93 | if (!p->pcre_extra_info && error) | |
94 | die("%s", error); | |
95 | } | |
96 | ||
97 | static int pcrematch(struct grep_pat *p, const char *line, const char *eol, | |
98 | regmatch_t *match, int eflags) | |
99 | { | |
100 | int ovector[30], ret, flags = 0; | |
101 | ||
102 | if (eflags & REG_NOTBOL) | |
103 | flags |= PCRE_NOTBOL; | |
104 | ||
105 | ret = pcre_exec(p->pcre_regexp, p->pcre_extra_info, line, eol - line, | |
106 | 0, flags, ovector, ARRAY_SIZE(ovector)); | |
107 | if (ret < 0 && ret != PCRE_ERROR_NOMATCH) | |
108 | die("pcre_exec failed with error code %d", ret); | |
109 | if (ret > 0) { | |
110 | ret = 0; | |
111 | match->rm_so = ovector[0]; | |
112 | match->rm_eo = ovector[1]; | |
113 | } | |
114 | ||
115 | return ret; | |
116 | } | |
117 | ||
118 | static void free_pcre_regexp(struct grep_pat *p) | |
119 | { | |
120 | pcre_free(p->pcre_regexp); | |
121 | pcre_free(p->pcre_extra_info); | |
122 | } | |
123 | #else /* !USE_LIBPCRE */ | |
124 | static void compile_pcre_regexp(struct grep_pat *p, const struct grep_opt *opt) | |
125 | { | |
126 | die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE"); | |
127 | } | |
128 | ||
129 | static int pcrematch(struct grep_pat *p, const char *line, const char *eol, | |
130 | regmatch_t *match, int eflags) | |
131 | { | |
132 | return 1; | |
133 | } | |
134 | ||
135 | static void free_pcre_regexp(struct grep_pat *p) | |
136 | { | |
137 | } | |
138 | #endif /* !USE_LIBPCRE */ | |
139 | ||
9eceddee FK |
140 | static int is_fixed(const char *s, size_t len) |
141 | { | |
142 | size_t i; | |
143 | ||
144 | /* regcomp cannot accept patterns with NULs so we | |
145 | * consider any pattern containing a NUL fixed. | |
146 | */ | |
147 | if (memchr(s, 0, len)) | |
148 | return 1; | |
149 | ||
150 | for (i = 0; i < len; i++) { | |
151 | if (is_regex_special(s[i])) | |
152 | return 0; | |
153 | } | |
154 | ||
155 | return 1; | |
156 | } | |
157 | ||
83b5d2f5 JH |
158 | static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) |
159 | { | |
c822255c RS |
160 | int err; |
161 | ||
d7eb527d | 162 | p->word_regexp = opt->word_regexp; |
5183bf67 | 163 | p->ignore_case = opt->ignore_case; |
d7eb527d | 164 | |
9eceddee FK |
165 | if (opt->fixed || is_fixed(p->pattern, p->patternlen)) |
166 | p->fixed = 1; | |
167 | else | |
168 | p->fixed = 0; | |
169 | ||
170 | if (p->fixed) { | |
0f871cf5 JH |
171 | if (opt->regflags & REG_ICASE || p->ignore_case) |
172 | p->kws = kwsalloc(tolower_trans_tbl); | |
173 | else | |
9eceddee | 174 | p->kws = kwsalloc(NULL); |
9eceddee FK |
175 | kwsincr(p->kws, p->pattern, p->patternlen); |
176 | kwsprep(p->kws); | |
c822255c | 177 | return; |
9eceddee | 178 | } |
c822255c | 179 | |
63e7e9d8 MK |
180 | if (opt->pcre) { |
181 | compile_pcre_regexp(p, opt); | |
182 | return; | |
183 | } | |
184 | ||
c822255c | 185 | err = regcomp(&p->regexp, p->pattern, opt->regflags); |
83b5d2f5 JH |
186 | if (err) { |
187 | char errbuf[1024]; | |
83b5d2f5 JH |
188 | regerror(err, &p->regexp, errbuf, 1024); |
189 | regfree(&p->regexp); | |
a30c148a | 190 | compile_regexp_failed(p, errbuf); |
83b5d2f5 JH |
191 | } |
192 | } | |
193 | ||
0ab7befa | 194 | static struct grep_expr *compile_pattern_or(struct grep_pat **); |
83b5d2f5 JH |
195 | static struct grep_expr *compile_pattern_atom(struct grep_pat **list) |
196 | { | |
197 | struct grep_pat *p; | |
198 | struct grep_expr *x; | |
199 | ||
200 | p = *list; | |
c922b01f LT |
201 | if (!p) |
202 | return NULL; | |
83b5d2f5 JH |
203 | switch (p->token) { |
204 | case GREP_PATTERN: /* atom */ | |
480c1ca6 JH |
205 | case GREP_PATTERN_HEAD: |
206 | case GREP_PATTERN_BODY: | |
83b5d2f5 JH |
207 | x = xcalloc(1, sizeof (struct grep_expr)); |
208 | x->node = GREP_NODE_ATOM; | |
209 | x->u.atom = p; | |
210 | *list = p->next; | |
211 | return x; | |
212 | case GREP_OPEN_PAREN: | |
213 | *list = p->next; | |
0ab7befa | 214 | x = compile_pattern_or(list); |
83b5d2f5 JH |
215 | if (!*list || (*list)->token != GREP_CLOSE_PAREN) |
216 | die("unmatched parenthesis"); | |
217 | *list = (*list)->next; | |
218 | return x; | |
219 | default: | |
220 | return NULL; | |
221 | } | |
222 | } | |
223 | ||
224 | static struct grep_expr *compile_pattern_not(struct grep_pat **list) | |
225 | { | |
226 | struct grep_pat *p; | |
227 | struct grep_expr *x; | |
228 | ||
229 | p = *list; | |
c922b01f LT |
230 | if (!p) |
231 | return NULL; | |
83b5d2f5 JH |
232 | switch (p->token) { |
233 | case GREP_NOT: | |
234 | if (!p->next) | |
235 | die("--not not followed by pattern expression"); | |
236 | *list = p->next; | |
237 | x = xcalloc(1, sizeof (struct grep_expr)); | |
238 | x->node = GREP_NODE_NOT; | |
239 | x->u.unary = compile_pattern_not(list); | |
240 | if (!x->u.unary) | |
241 | die("--not followed by non pattern expression"); | |
242 | return x; | |
243 | default: | |
244 | return compile_pattern_atom(list); | |
245 | } | |
246 | } | |
247 | ||
248 | static struct grep_expr *compile_pattern_and(struct grep_pat **list) | |
249 | { | |
250 | struct grep_pat *p; | |
251 | struct grep_expr *x, *y, *z; | |
252 | ||
253 | x = compile_pattern_not(list); | |
254 | p = *list; | |
255 | if (p && p->token == GREP_AND) { | |
256 | if (!p->next) | |
257 | die("--and not followed by pattern expression"); | |
258 | *list = p->next; | |
259 | y = compile_pattern_and(list); | |
260 | if (!y) | |
261 | die("--and not followed by pattern expression"); | |
262 | z = xcalloc(1, sizeof (struct grep_expr)); | |
263 | z->node = GREP_NODE_AND; | |
264 | z->u.binary.left = x; | |
265 | z->u.binary.right = y; | |
266 | return z; | |
267 | } | |
268 | return x; | |
269 | } | |
270 | ||
271 | static struct grep_expr *compile_pattern_or(struct grep_pat **list) | |
272 | { | |
273 | struct grep_pat *p; | |
274 | struct grep_expr *x, *y, *z; | |
275 | ||
276 | x = compile_pattern_and(list); | |
277 | p = *list; | |
278 | if (x && p && p->token != GREP_CLOSE_PAREN) { | |
279 | y = compile_pattern_or(list); | |
280 | if (!y) | |
281 | die("not a pattern expression %s", p->pattern); | |
282 | z = xcalloc(1, sizeof (struct grep_expr)); | |
283 | z->node = GREP_NODE_OR; | |
284 | z->u.binary.left = x; | |
285 | z->u.binary.right = y; | |
286 | return z; | |
287 | } | |
288 | return x; | |
289 | } | |
290 | ||
291 | static struct grep_expr *compile_pattern_expr(struct grep_pat **list) | |
292 | { | |
293 | return compile_pattern_or(list); | |
294 | } | |
295 | ||
5aaeb733 JH |
296 | static struct grep_expr *grep_true_expr(void) |
297 | { | |
298 | struct grep_expr *z = xcalloc(1, sizeof(*z)); | |
299 | z->node = GREP_NODE_TRUE; | |
300 | return z; | |
301 | } | |
302 | ||
303 | static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right) | |
304 | { | |
305 | struct grep_expr *z = xcalloc(1, sizeof(*z)); | |
306 | z->node = GREP_NODE_OR; | |
307 | z->u.binary.left = left; | |
308 | z->u.binary.right = right; | |
309 | return z; | |
310 | } | |
311 | ||
95ce9ce2 | 312 | static struct grep_expr *prep_header_patterns(struct grep_opt *opt) |
83b5d2f5 JH |
313 | { |
314 | struct grep_pat *p; | |
95ce9ce2 | 315 | struct grep_expr *header_expr; |
5aaeb733 JH |
316 | struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]); |
317 | enum grep_header_field fld; | |
83b5d2f5 | 318 | |
95ce9ce2 JH |
319 | if (!opt->header_list) |
320 | return NULL; | |
321 | p = opt->header_list; | |
95ce9ce2 JH |
322 | for (p = opt->header_list; p; p = p->next) { |
323 | if (p->token != GREP_PATTERN_HEAD) | |
324 | die("bug: a non-header pattern in grep header list."); | |
325 | if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field) | |
326 | die("bug: unknown header field %d", p->field); | |
327 | compile_regexp(p, opt); | |
80235ba7 | 328 | } |
5aaeb733 JH |
329 | |
330 | for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) | |
331 | header_group[fld] = NULL; | |
332 | ||
333 | for (p = opt->header_list; p; p = p->next) { | |
334 | struct grep_expr *h; | |
335 | struct grep_pat *pp = p; | |
336 | ||
337 | h = compile_pattern_atom(&pp); | |
338 | if (!h || pp != p->next) | |
339 | die("bug: malformed header expr"); | |
340 | if (!header_group[p->field]) { | |
341 | header_group[p->field] = h; | |
342 | continue; | |
343 | } | |
344 | header_group[p->field] = grep_or_expr(h, header_group[p->field]); | |
345 | } | |
346 | ||
347 | header_expr = NULL; | |
348 | ||
349 | for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) { | |
350 | if (!header_group[fld]) | |
351 | continue; | |
352 | if (!header_expr) | |
353 | header_expr = grep_true_expr(); | |
354 | header_expr = grep_or_expr(header_group[fld], header_expr); | |
355 | } | |
95ce9ce2 JH |
356 | return header_expr; |
357 | } | |
358 | ||
359 | void compile_grep_patterns(struct grep_opt *opt) | |
360 | { | |
361 | struct grep_pat *p; | |
362 | struct grep_expr *header_expr = prep_header_patterns(opt); | |
0ab7befa | 363 | |
83b5d2f5 | 364 | for (p = opt->pattern_list; p; p = p->next) { |
480c1ca6 JH |
365 | switch (p->token) { |
366 | case GREP_PATTERN: /* atom */ | |
367 | case GREP_PATTERN_HEAD: | |
368 | case GREP_PATTERN_BODY: | |
c822255c | 369 | compile_regexp(p, opt); |
480c1ca6 JH |
370 | break; |
371 | default: | |
83b5d2f5 | 372 | opt->extended = 1; |
480c1ca6 JH |
373 | break; |
374 | } | |
83b5d2f5 JH |
375 | } |
376 | ||
80235ba7 JH |
377 | if (opt->all_match || header_expr) |
378 | opt->extended = 1; | |
379 | else if (!opt->extended) | |
83b5d2f5 JH |
380 | return; |
381 | ||
83b5d2f5 | 382 | p = opt->pattern_list; |
ba150a3f MB |
383 | if (p) |
384 | opt->pattern_expression = compile_pattern_expr(&p); | |
83b5d2f5 JH |
385 | if (p) |
386 | die("incomplete pattern expression: %s", p->pattern); | |
80235ba7 JH |
387 | |
388 | if (!header_expr) | |
389 | return; | |
390 | ||
5aaeb733 | 391 | if (!opt->pattern_expression) |
80235ba7 | 392 | opt->pattern_expression = header_expr; |
5aaeb733 JH |
393 | else |
394 | opt->pattern_expression = grep_or_expr(opt->pattern_expression, | |
395 | header_expr); | |
80235ba7 | 396 | opt->all_match = 1; |
83b5d2f5 JH |
397 | } |
398 | ||
b48fb5b6 JH |
399 | static void free_pattern_expr(struct grep_expr *x) |
400 | { | |
401 | switch (x->node) { | |
5aaeb733 | 402 | case GREP_NODE_TRUE: |
b48fb5b6 JH |
403 | case GREP_NODE_ATOM: |
404 | break; | |
405 | case GREP_NODE_NOT: | |
406 | free_pattern_expr(x->u.unary); | |
407 | break; | |
408 | case GREP_NODE_AND: | |
409 | case GREP_NODE_OR: | |
410 | free_pattern_expr(x->u.binary.left); | |
411 | free_pattern_expr(x->u.binary.right); | |
412 | break; | |
413 | } | |
414 | free(x); | |
415 | } | |
416 | ||
417 | void free_grep_patterns(struct grep_opt *opt) | |
418 | { | |
419 | struct grep_pat *p, *n; | |
420 | ||
421 | for (p = opt->pattern_list; p; p = n) { | |
422 | n = p->next; | |
423 | switch (p->token) { | |
424 | case GREP_PATTERN: /* atom */ | |
425 | case GREP_PATTERN_HEAD: | |
426 | case GREP_PATTERN_BODY: | |
9eceddee FK |
427 | if (p->kws) |
428 | kwsfree(p->kws); | |
429 | else if (p->pcre_regexp) | |
63e7e9d8 MK |
430 | free_pcre_regexp(p); |
431 | else | |
432 | regfree(&p->regexp); | |
b48fb5b6 JH |
433 | break; |
434 | default: | |
435 | break; | |
436 | } | |
437 | free(p); | |
438 | } | |
439 | ||
440 | if (!opt->extended) | |
441 | return; | |
442 | free_pattern_expr(opt->pattern_expression); | |
443 | } | |
444 | ||
83b5d2f5 JH |
445 | static char *end_of_line(char *cp, unsigned long *left) |
446 | { | |
447 | unsigned long l = *left; | |
448 | while (l && *cp != '\n') { | |
449 | l--; | |
450 | cp++; | |
451 | } | |
452 | *left = l; | |
453 | return cp; | |
454 | } | |
455 | ||
456 | static int word_char(char ch) | |
457 | { | |
458 | return isalnum(ch) || ch == '_'; | |
459 | } | |
460 | ||
55f638bd ML |
461 | static void output_color(struct grep_opt *opt, const void *data, size_t size, |
462 | const char *color) | |
463 | { | |
daa0c3d9 | 464 | if (want_color(opt->color) && color && color[0]) { |
55f638bd ML |
465 | opt->output(opt, color, strlen(color)); |
466 | opt->output(opt, data, size); | |
467 | opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET)); | |
468 | } else | |
469 | opt->output(opt, data, size); | |
470 | } | |
471 | ||
472 | static void output_sep(struct grep_opt *opt, char sign) | |
473 | { | |
474 | if (opt->null_following_name) | |
475 | opt->output(opt, "\0", 1); | |
476 | else | |
477 | output_color(opt, &sign, 1, opt->color_sep); | |
478 | } | |
479 | ||
83caecca RZ |
480 | static void show_name(struct grep_opt *opt, const char *name) |
481 | { | |
55f638bd | 482 | output_color(opt, name, strlen(name), opt->color_filename); |
5b594f45 | 483 | opt->output(opt, opt->null_following_name ? "\0" : "\n", 1); |
83caecca RZ |
484 | } |
485 | ||
ed40a095 RS |
486 | static int fixmatch(struct grep_pat *p, char *line, char *eol, |
487 | regmatch_t *match) | |
83b5d2f5 | 488 | { |
9eceddee FK |
489 | struct kwsmatch kwsm; |
490 | size_t offset = kwsexec(p->kws, line, eol - line, &kwsm); | |
491 | if (offset == -1) { | |
83b5d2f5 JH |
492 | match->rm_so = match->rm_eo = -1; |
493 | return REG_NOMATCH; | |
9eceddee FK |
494 | } else { |
495 | match->rm_so = offset; | |
496 | match->rm_eo = match->rm_so + kwsm.size[0]; | |
83b5d2f5 JH |
497 | return 0; |
498 | } | |
499 | } | |
500 | ||
f96e5673 RS |
501 | static int regmatch(const regex_t *preg, char *line, char *eol, |
502 | regmatch_t *match, int eflags) | |
503 | { | |
504 | #ifdef REG_STARTEND | |
505 | match->rm_so = 0; | |
506 | match->rm_eo = eol - line; | |
507 | eflags |= REG_STARTEND; | |
508 | #endif | |
509 | return regexec(preg, line, 1, match, eflags); | |
510 | } | |
511 | ||
97e77784 MK |
512 | static int patmatch(struct grep_pat *p, char *line, char *eol, |
513 | regmatch_t *match, int eflags) | |
514 | { | |
515 | int hit; | |
516 | ||
517 | if (p->fixed) | |
518 | hit = !fixmatch(p, line, eol, match); | |
63e7e9d8 MK |
519 | else if (p->pcre_regexp) |
520 | hit = !pcrematch(p, line, eol, match, eflags); | |
97e77784 MK |
521 | else |
522 | hit = !regmatch(&p->regexp, line, eol, match, eflags); | |
523 | ||
524 | return hit; | |
525 | } | |
526 | ||
a4d7d2c6 JH |
527 | static int strip_timestamp(char *bol, char **eol_p) |
528 | { | |
529 | char *eol = *eol_p; | |
530 | int ch; | |
531 | ||
532 | while (bol < --eol) { | |
533 | if (*eol != '>') | |
534 | continue; | |
535 | *eol_p = ++eol; | |
536 | ch = *eol; | |
537 | *eol = '\0'; | |
538 | return ch; | |
539 | } | |
540 | return 0; | |
541 | } | |
542 | ||
543 | static struct { | |
544 | const char *field; | |
545 | size_t len; | |
546 | } header_field[] = { | |
547 | { "author ", 7 }, | |
548 | { "committer ", 10 }, | |
549 | }; | |
550 | ||
d7eb527d | 551 | static int match_one_pattern(struct grep_pat *p, char *bol, char *eol, |
79212772 RS |
552 | enum grep_context ctx, |
553 | regmatch_t *pmatch, int eflags) | |
83b5d2f5 JH |
554 | { |
555 | int hit = 0; | |
a4d7d2c6 | 556 | int saved_ch = 0; |
e701fadb | 557 | const char *start = bol; |
83b5d2f5 | 558 | |
480c1ca6 JH |
559 | if ((p->token != GREP_PATTERN) && |
560 | ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD))) | |
561 | return 0; | |
562 | ||
a4d7d2c6 JH |
563 | if (p->token == GREP_PATTERN_HEAD) { |
564 | const char *field; | |
565 | size_t len; | |
566 | assert(p->field < ARRAY_SIZE(header_field)); | |
567 | field = header_field[p->field].field; | |
568 | len = header_field[p->field].len; | |
569 | if (strncmp(bol, field, len)) | |
570 | return 0; | |
571 | bol += len; | |
572 | saved_ch = strip_timestamp(bol, &eol); | |
573 | } | |
574 | ||
83b5d2f5 | 575 | again: |
97e77784 | 576 | hit = patmatch(p, bol, eol, pmatch, eflags); |
83b5d2f5 | 577 | |
d7eb527d | 578 | if (hit && p->word_regexp) { |
83b5d2f5 | 579 | if ((pmatch[0].rm_so < 0) || |
84201eae | 580 | (eol - bol) < pmatch[0].rm_so || |
83b5d2f5 JH |
581 | (pmatch[0].rm_eo < 0) || |
582 | (eol - bol) < pmatch[0].rm_eo) | |
583 | die("regexp returned nonsense"); | |
584 | ||
585 | /* Match beginning must be either beginning of the | |
586 | * line, or at word boundary (i.e. the last char must | |
587 | * not be a word char). Similarly, match end must be | |
588 | * either end of the line, or at word boundary | |
589 | * (i.e. the next char must not be a word char). | |
590 | */ | |
fb62eb7f | 591 | if ( ((pmatch[0].rm_so == 0) || |
83b5d2f5 JH |
592 | !word_char(bol[pmatch[0].rm_so-1])) && |
593 | ((pmatch[0].rm_eo == (eol-bol)) || | |
594 | !word_char(bol[pmatch[0].rm_eo])) ) | |
595 | ; | |
596 | else | |
597 | hit = 0; | |
598 | ||
84201eae RS |
599 | /* Words consist of at least one character. */ |
600 | if (pmatch->rm_so == pmatch->rm_eo) | |
601 | hit = 0; | |
602 | ||
83b5d2f5 JH |
603 | if (!hit && pmatch[0].rm_so + bol + 1 < eol) { |
604 | /* There could be more than one match on the | |
605 | * line, and the first match might not be | |
606 | * strict word match. But later ones could be! | |
fb62eb7f RS |
607 | * Forward to the next possible start, i.e. the |
608 | * next position following a non-word char. | |
83b5d2f5 JH |
609 | */ |
610 | bol = pmatch[0].rm_so + bol + 1; | |
fb62eb7f RS |
611 | while (word_char(bol[-1]) && bol < eol) |
612 | bol++; | |
dbb6a4ad | 613 | eflags |= REG_NOTBOL; |
fb62eb7f RS |
614 | if (bol < eol) |
615 | goto again; | |
83b5d2f5 JH |
616 | } |
617 | } | |
a4d7d2c6 JH |
618 | if (p->token == GREP_PATTERN_HEAD && saved_ch) |
619 | *eol = saved_ch; | |
e701fadb RS |
620 | if (hit) { |
621 | pmatch[0].rm_so += bol - start; | |
622 | pmatch[0].rm_eo += bol - start; | |
623 | } | |
83b5d2f5 JH |
624 | return hit; |
625 | } | |
626 | ||
d7eb527d RS |
627 | static int match_expr_eval(struct grep_expr *x, char *bol, char *eol, |
628 | enum grep_context ctx, int collect_hits) | |
83b5d2f5 | 629 | { |
0ab7befa | 630 | int h = 0; |
79212772 | 631 | regmatch_t match; |
0ab7befa | 632 | |
c922b01f LT |
633 | if (!x) |
634 | die("Not a valid grep expression"); | |
83b5d2f5 | 635 | switch (x->node) { |
5aaeb733 JH |
636 | case GREP_NODE_TRUE: |
637 | h = 1; | |
638 | break; | |
83b5d2f5 | 639 | case GREP_NODE_ATOM: |
79212772 | 640 | h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0); |
83b5d2f5 JH |
641 | break; |
642 | case GREP_NODE_NOT: | |
d7eb527d | 643 | h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0); |
0ab7befa | 644 | break; |
83b5d2f5 | 645 | case GREP_NODE_AND: |
d7eb527d | 646 | if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0)) |
252d560d | 647 | return 0; |
d7eb527d | 648 | h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0); |
0ab7befa | 649 | break; |
83b5d2f5 | 650 | case GREP_NODE_OR: |
0ab7befa | 651 | if (!collect_hits) |
d7eb527d | 652 | return (match_expr_eval(x->u.binary.left, |
0ab7befa | 653 | bol, eol, ctx, 0) || |
d7eb527d | 654 | match_expr_eval(x->u.binary.right, |
0ab7befa | 655 | bol, eol, ctx, 0)); |
d7eb527d | 656 | h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0); |
0ab7befa | 657 | x->u.binary.left->hit |= h; |
d7eb527d | 658 | h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1); |
0ab7befa JH |
659 | break; |
660 | default: | |
d7530708 | 661 | die("Unexpected node type (internal error) %d", x->node); |
83b5d2f5 | 662 | } |
0ab7befa JH |
663 | if (collect_hits) |
664 | x->hit |= h; | |
665 | return h; | |
83b5d2f5 JH |
666 | } |
667 | ||
480c1ca6 | 668 | static int match_expr(struct grep_opt *opt, char *bol, char *eol, |
0ab7befa | 669 | enum grep_context ctx, int collect_hits) |
83b5d2f5 JH |
670 | { |
671 | struct grep_expr *x = opt->pattern_expression; | |
d7eb527d | 672 | return match_expr_eval(x, bol, eol, ctx, collect_hits); |
83b5d2f5 JH |
673 | } |
674 | ||
480c1ca6 | 675 | static int match_line(struct grep_opt *opt, char *bol, char *eol, |
0ab7befa | 676 | enum grep_context ctx, int collect_hits) |
83b5d2f5 JH |
677 | { |
678 | struct grep_pat *p; | |
79212772 RS |
679 | regmatch_t match; |
680 | ||
83b5d2f5 | 681 | if (opt->extended) |
0ab7befa JH |
682 | return match_expr(opt, bol, eol, ctx, collect_hits); |
683 | ||
684 | /* we do not call with collect_hits without being extended */ | |
83b5d2f5 | 685 | for (p = opt->pattern_list; p; p = p->next) { |
79212772 | 686 | if (match_one_pattern(p, bol, eol, ctx, &match, 0)) |
83b5d2f5 JH |
687 | return 1; |
688 | } | |
689 | return 0; | |
690 | } | |
691 | ||
7e8f59d5 RS |
692 | static int match_next_pattern(struct grep_pat *p, char *bol, char *eol, |
693 | enum grep_context ctx, | |
694 | regmatch_t *pmatch, int eflags) | |
695 | { | |
696 | regmatch_t match; | |
697 | ||
698 | if (!match_one_pattern(p, bol, eol, ctx, &match, eflags)) | |
699 | return 0; | |
700 | if (match.rm_so < 0 || match.rm_eo < 0) | |
701 | return 0; | |
702 | if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) { | |
703 | if (match.rm_so > pmatch->rm_so) | |
704 | return 1; | |
705 | if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo) | |
706 | return 1; | |
707 | } | |
708 | pmatch->rm_so = match.rm_so; | |
709 | pmatch->rm_eo = match.rm_eo; | |
710 | return 1; | |
711 | } | |
712 | ||
713 | static int next_match(struct grep_opt *opt, char *bol, char *eol, | |
714 | enum grep_context ctx, regmatch_t *pmatch, int eflags) | |
715 | { | |
716 | struct grep_pat *p; | |
717 | int hit = 0; | |
718 | ||
719 | pmatch->rm_so = pmatch->rm_eo = -1; | |
720 | if (bol < eol) { | |
721 | for (p = opt->pattern_list; p; p = p->next) { | |
722 | switch (p->token) { | |
723 | case GREP_PATTERN: /* atom */ | |
724 | case GREP_PATTERN_HEAD: | |
725 | case GREP_PATTERN_BODY: | |
726 | hit |= match_next_pattern(p, bol, eol, ctx, | |
727 | pmatch, eflags); | |
728 | break; | |
729 | default: | |
730 | break; | |
731 | } | |
732 | } | |
733 | } | |
734 | return hit; | |
735 | } | |
736 | ||
737 | static void show_line(struct grep_opt *opt, char *bol, char *eol, | |
738 | const char *name, unsigned lno, char sign) | |
739 | { | |
740 | int rest = eol - bol; | |
00588bb5 | 741 | char *line_color = NULL; |
7e8f59d5 | 742 | |
a8f0e764 RS |
743 | if (opt->file_break && opt->last_shown == 0) { |
744 | if (opt->show_hunk_mark) | |
745 | opt->output(opt, "\n", 1); | |
ba8ea749 | 746 | } else if (opt->pre_context || opt->post_context || opt->funcbody) { |
046802d0 | 747 | if (opt->last_shown == 0) { |
55f638bd ML |
748 | if (opt->show_hunk_mark) { |
749 | output_color(opt, "--", 2, opt->color_sep); | |
750 | opt->output(opt, "\n", 1); | |
07b838f0 | 751 | } |
55f638bd ML |
752 | } else if (lno > opt->last_shown + 1) { |
753 | output_color(opt, "--", 2, opt->color_sep); | |
754 | opt->output(opt, "\n", 1); | |
755 | } | |
5dd06d38 | 756 | } |
1d84f72e RS |
757 | if (opt->heading && opt->last_shown == 0) { |
758 | output_color(opt, name, strlen(name), opt->color_filename); | |
759 | opt->output(opt, "\n", 1); | |
760 | } | |
5dd06d38 RS |
761 | opt->last_shown = lno; |
762 | ||
1d84f72e | 763 | if (!opt->heading && opt->pathname) { |
55f638bd ML |
764 | output_color(opt, name, strlen(name), opt->color_filename); |
765 | output_sep(opt, sign); | |
5b594f45 FK |
766 | } |
767 | if (opt->linenum) { | |
768 | char buf[32]; | |
769 | snprintf(buf, sizeof(buf), "%d", lno); | |
55f638bd ML |
770 | output_color(opt, buf, strlen(buf), opt->color_lineno); |
771 | output_sep(opt, sign); | |
5b594f45 | 772 | } |
7e8f59d5 RS |
773 | if (opt->color) { |
774 | regmatch_t match; | |
775 | enum grep_context ctx = GREP_CONTEXT_BODY; | |
776 | int ch = *eol; | |
777 | int eflags = 0; | |
778 | ||
00588bb5 ML |
779 | if (sign == ':') |
780 | line_color = opt->color_selected; | |
781 | else if (sign == '-') | |
782 | line_color = opt->color_context; | |
783 | else if (sign == '=') | |
784 | line_color = opt->color_function; | |
7e8f59d5 RS |
785 | *eol = '\0'; |
786 | while (next_match(opt, bol, eol, ctx, &match, eflags)) { | |
1f5b9cc4 RS |
787 | if (match.rm_so == match.rm_eo) |
788 | break; | |
5b594f45 | 789 | |
00588bb5 | 790 | output_color(opt, bol, match.rm_so, line_color); |
55f638bd ML |
791 | output_color(opt, bol + match.rm_so, |
792 | match.rm_eo - match.rm_so, | |
793 | opt->color_match); | |
7e8f59d5 RS |
794 | bol += match.rm_eo; |
795 | rest -= match.rm_eo; | |
796 | eflags = REG_NOTBOL; | |
797 | } | |
798 | *eol = ch; | |
799 | } | |
00588bb5 | 800 | output_color(opt, bol, rest, line_color); |
5b594f45 | 801 | opt->output(opt, "\n", 1); |
7e8f59d5 RS |
802 | } |
803 | ||
60ecac98 | 804 | static int match_funcname(struct grep_opt *opt, char *bol, char *eol) |
2944e4e6 | 805 | { |
60ecac98 RS |
806 | xdemitconf_t *xecfg = opt->priv; |
807 | if (xecfg && xecfg->find_func) { | |
808 | char buf[1]; | |
809 | return xecfg->find_func(bol, eol - bol, buf, 1, | |
810 | xecfg->find_func_priv) >= 0; | |
811 | } | |
812 | ||
2944e4e6 RS |
813 | if (bol == eol) |
814 | return 0; | |
815 | if (isalpha(*bol) || *bol == '_' || *bol == '$') | |
816 | return 1; | |
817 | return 0; | |
818 | } | |
819 | ||
820 | static void show_funcname_line(struct grep_opt *opt, const char *name, | |
821 | char *buf, char *bol, unsigned lno) | |
822 | { | |
823 | while (bol > buf) { | |
824 | char *eol = --bol; | |
825 | ||
826 | while (bol > buf && bol[-1] != '\n') | |
827 | bol--; | |
828 | lno--; | |
829 | ||
830 | if (lno <= opt->last_shown) | |
831 | break; | |
832 | ||
60ecac98 | 833 | if (match_funcname(opt, bol, eol)) { |
2944e4e6 RS |
834 | show_line(opt, bol, eol, name, lno, '='); |
835 | break; | |
836 | } | |
837 | } | |
838 | } | |
839 | ||
49de3216 | 840 | static void show_pre_context(struct grep_opt *opt, const char *name, char *buf, |
ba8ea749 | 841 | char *bol, char *end, unsigned lno) |
49de3216 | 842 | { |
2944e4e6 | 843 | unsigned cur = lno, from = 1, funcname_lno = 0; |
ba8ea749 RS |
844 | int funcname_needed = !!opt->funcname; |
845 | ||
846 | if (opt->funcbody && !match_funcname(opt, bol, end)) | |
847 | funcname_needed = 2; | |
49de3216 RS |
848 | |
849 | if (opt->pre_context < lno) | |
850 | from = lno - opt->pre_context; | |
851 | if (from <= opt->last_shown) | |
852 | from = opt->last_shown + 1; | |
853 | ||
854 | /* Rewind. */ | |
ba8ea749 RS |
855 | while (bol > buf && |
856 | cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) { | |
2944e4e6 RS |
857 | char *eol = --bol; |
858 | ||
49de3216 RS |
859 | while (bol > buf && bol[-1] != '\n') |
860 | bol--; | |
861 | cur--; | |
60ecac98 | 862 | if (funcname_needed && match_funcname(opt, bol, eol)) { |
2944e4e6 RS |
863 | funcname_lno = cur; |
864 | funcname_needed = 0; | |
865 | } | |
49de3216 RS |
866 | } |
867 | ||
2944e4e6 RS |
868 | /* We need to look even further back to find a function signature. */ |
869 | if (opt->funcname && funcname_needed) | |
870 | show_funcname_line(opt, name, buf, bol, cur); | |
871 | ||
49de3216 RS |
872 | /* Back forward. */ |
873 | while (cur < lno) { | |
2944e4e6 | 874 | char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-'; |
49de3216 RS |
875 | |
876 | while (*eol != '\n') | |
877 | eol++; | |
2944e4e6 | 878 | show_line(opt, bol, eol, name, cur, sign); |
49de3216 RS |
879 | bol = eol + 1; |
880 | cur++; | |
881 | } | |
882 | } | |
883 | ||
a26345b6 JH |
884 | static int should_lookahead(struct grep_opt *opt) |
885 | { | |
886 | struct grep_pat *p; | |
887 | ||
888 | if (opt->extended) | |
889 | return 0; /* punt for too complex stuff */ | |
890 | if (opt->invert) | |
891 | return 0; | |
892 | for (p = opt->pattern_list; p; p = p->next) { | |
893 | if (p->token != GREP_PATTERN) | |
894 | return 0; /* punt for "header only" and stuff */ | |
895 | } | |
896 | return 1; | |
897 | } | |
898 | ||
899 | static int look_ahead(struct grep_opt *opt, | |
900 | unsigned long *left_p, | |
901 | unsigned *lno_p, | |
902 | char **bol_p) | |
903 | { | |
904 | unsigned lno = *lno_p; | |
905 | char *bol = *bol_p; | |
906 | struct grep_pat *p; | |
907 | char *sp, *last_bol; | |
908 | regoff_t earliest = -1; | |
909 | ||
910 | for (p = opt->pattern_list; p; p = p->next) { | |
911 | int hit; | |
912 | regmatch_t m; | |
913 | ||
97e77784 | 914 | hit = patmatch(p, bol, bol + *left_p, &m, 0); |
a26345b6 JH |
915 | if (!hit || m.rm_so < 0 || m.rm_eo < 0) |
916 | continue; | |
917 | if (earliest < 0 || m.rm_so < earliest) | |
918 | earliest = m.rm_so; | |
919 | } | |
920 | ||
921 | if (earliest < 0) { | |
922 | *bol_p = bol + *left_p; | |
923 | *left_p = 0; | |
924 | return 1; | |
925 | } | |
926 | for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--) | |
927 | ; /* find the beginning of the line */ | |
928 | last_bol = sp; | |
929 | ||
930 | for (sp = bol; sp < last_bol; sp++) { | |
931 | if (*sp == '\n') | |
932 | lno++; | |
933 | } | |
934 | *left_p -= last_bol - bol; | |
935 | *bol_p = last_bol; | |
936 | *lno_p = lno; | |
937 | return 0; | |
938 | } | |
939 | ||
5b594f45 FK |
940 | int grep_threads_ok(const struct grep_opt *opt) |
941 | { | |
942 | /* If this condition is true, then we may use the attribute | |
943 | * machinery in grep_buffer_1. The attribute code is not | |
944 | * thread safe, so we disable the use of threads. | |
945 | */ | |
946 | if (opt->funcname && !opt->unmatch_name_only && !opt->status_only && | |
947 | !opt->name_only) | |
948 | return 0; | |
949 | ||
5b594f45 FK |
950 | return 1; |
951 | } | |
952 | ||
953 | static void std_output(struct grep_opt *opt, const void *buf, size_t size) | |
954 | { | |
955 | fwrite(buf, size, 1, stdout); | |
956 | } | |
957 | ||
0ab7befa JH |
958 | static int grep_buffer_1(struct grep_opt *opt, const char *name, |
959 | char *buf, unsigned long size, int collect_hits) | |
83b5d2f5 JH |
960 | { |
961 | char *bol = buf; | |
962 | unsigned long left = size; | |
963 | unsigned lno = 1; | |
83b5d2f5 | 964 | unsigned last_hit = 0; |
83b5d2f5 | 965 | int binary_match_only = 0; |
83b5d2f5 | 966 | unsigned count = 0; |
a26345b6 | 967 | int try_lookahead = 0; |
ba8ea749 | 968 | int show_function = 0; |
480c1ca6 | 969 | enum grep_context ctx = GREP_CONTEXT_HEAD; |
60ecac98 | 970 | xdemitconf_t xecfg; |
83b5d2f5 | 971 | |
5b594f45 FK |
972 | if (!opt->output) |
973 | opt->output = std_output; | |
974 | ||
ba8ea749 RS |
975 | if (opt->pre_context || opt->post_context || opt->file_break || |
976 | opt->funcbody) { | |
08303c36 RS |
977 | /* Show hunk marks, except for the first file. */ |
978 | if (opt->last_shown) | |
979 | opt->show_hunk_mark = 1; | |
980 | /* | |
981 | * If we're using threads then we can't easily identify | |
982 | * the first file. Always put hunk marks in that case | |
983 | * and skip the very first one later in work_done(). | |
984 | */ | |
985 | if (opt->output != std_output) | |
986 | opt->show_hunk_mark = 1; | |
987 | } | |
431d6e7b RS |
988 | opt->last_shown = 0; |
989 | ||
64fcec78 RS |
990 | switch (opt->binary) { |
991 | case GREP_BINARY_DEFAULT: | |
992 | if (buffer_is_binary(buf, size)) | |
83b5d2f5 | 993 | binary_match_only = 1; |
64fcec78 RS |
994 | break; |
995 | case GREP_BINARY_NOMATCH: | |
996 | if (buffer_is_binary(buf, size)) | |
83b5d2f5 | 997 | return 0; /* Assume unmatch */ |
64fcec78 RS |
998 | break; |
999 | case GREP_BINARY_TEXT: | |
1000 | break; | |
1001 | default: | |
1002 | die("bug: unknown binary handling mode"); | |
83b5d2f5 JH |
1003 | } |
1004 | ||
60ecac98 RS |
1005 | memset(&xecfg, 0, sizeof(xecfg)); |
1006 | if (opt->funcname && !opt->unmatch_name_only && !opt->status_only && | |
1007 | !opt->name_only && !binary_match_only && !collect_hits) { | |
1008 | struct userdiff_driver *drv = userdiff_find_by_path(name); | |
1009 | if (drv && drv->funcname.pattern) { | |
1010 | const struct userdiff_funcname *pe = &drv->funcname; | |
1011 | xdiff_set_find_func(&xecfg, pe->pattern, pe->cflags); | |
1012 | opt->priv = &xecfg; | |
1013 | } | |
1014 | } | |
a26345b6 | 1015 | try_lookahead = should_lookahead(opt); |
60ecac98 | 1016 | |
83b5d2f5 JH |
1017 | while (left) { |
1018 | char *eol, ch; | |
0ab7befa | 1019 | int hit; |
83b5d2f5 | 1020 | |
a26345b6 | 1021 | /* |
8997da38 | 1022 | * look_ahead() skips quickly to the line that possibly |
a26345b6 JH |
1023 | * has the next hit; don't call it if we need to do |
1024 | * something more than just skipping the current line | |
1025 | * in response to an unmatch for the current line. E.g. | |
1026 | * inside a post-context window, we will show the current | |
1027 | * line as a context around the previous hit when it | |
1028 | * doesn't hit. | |
1029 | */ | |
1030 | if (try_lookahead | |
1031 | && !(last_hit | |
ba8ea749 RS |
1032 | && (show_function || |
1033 | lno <= last_hit + opt->post_context)) | |
a26345b6 JH |
1034 | && look_ahead(opt, &left, &lno, &bol)) |
1035 | break; | |
83b5d2f5 JH |
1036 | eol = end_of_line(bol, &left); |
1037 | ch = *eol; | |
1038 | *eol = 0; | |
1039 | ||
480c1ca6 JH |
1040 | if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol)) |
1041 | ctx = GREP_CONTEXT_BODY; | |
1042 | ||
0ab7befa | 1043 | hit = match_line(opt, bol, eol, ctx, collect_hits); |
83b5d2f5 JH |
1044 | *eol = ch; |
1045 | ||
0ab7befa JH |
1046 | if (collect_hits) |
1047 | goto next_line; | |
1048 | ||
83b5d2f5 JH |
1049 | /* "grep -v -e foo -e bla" should list lines |
1050 | * that do not have either, so inversion should | |
1051 | * be done outside. | |
1052 | */ | |
1053 | if (opt->invert) | |
1054 | hit = !hit; | |
1055 | if (opt->unmatch_name_only) { | |
1056 | if (hit) | |
1057 | return 0; | |
1058 | goto next_line; | |
1059 | } | |
1060 | if (hit) { | |
1061 | count++; | |
1062 | if (opt->status_only) | |
1063 | return 1; | |
321ffcc0 RS |
1064 | if (opt->name_only) { |
1065 | show_name(opt, name); | |
1066 | return 1; | |
1067 | } | |
c30c10cf RS |
1068 | if (opt->count) |
1069 | goto next_line; | |
83b5d2f5 | 1070 | if (binary_match_only) { |
5b594f45 | 1071 | opt->output(opt, "Binary file ", 12); |
55f638bd ML |
1072 | output_color(opt, name, strlen(name), |
1073 | opt->color_filename); | |
5b594f45 | 1074 | opt->output(opt, " matches\n", 9); |
83b5d2f5 JH |
1075 | return 1; |
1076 | } | |
83b5d2f5 JH |
1077 | /* Hit at this line. If we haven't shown the |
1078 | * pre-context lines, we would need to show them. | |
83b5d2f5 | 1079 | */ |
ba8ea749 RS |
1080 | if (opt->pre_context || opt->funcbody) |
1081 | show_pre_context(opt, name, buf, bol, eol, lno); | |
2944e4e6 RS |
1082 | else if (opt->funcname) |
1083 | show_funcname_line(opt, name, buf, bol, lno); | |
c30c10cf | 1084 | show_line(opt, bol, eol, name, lno, ':'); |
5dd06d38 | 1085 | last_hit = lno; |
ba8ea749 RS |
1086 | if (opt->funcbody) |
1087 | show_function = 1; | |
1088 | goto next_line; | |
83b5d2f5 | 1089 | } |
ba8ea749 RS |
1090 | if (show_function && match_funcname(opt, bol, eol)) |
1091 | show_function = 0; | |
1092 | if (show_function || | |
1093 | (last_hit && lno <= last_hit + opt->post_context)) { | |
83b5d2f5 JH |
1094 | /* If the last hit is within the post context, |
1095 | * we need to show this line. | |
1096 | */ | |
83b5d2f5 | 1097 | show_line(opt, bol, eol, name, lno, '-'); |
83b5d2f5 | 1098 | } |
83b5d2f5 JH |
1099 | |
1100 | next_line: | |
1101 | bol = eol + 1; | |
1102 | if (!left) | |
1103 | break; | |
1104 | left--; | |
1105 | lno++; | |
1106 | } | |
1107 | ||
0ab7befa JH |
1108 | if (collect_hits) |
1109 | return 0; | |
b48fb5b6 | 1110 | |
83b5d2f5 JH |
1111 | if (opt->status_only) |
1112 | return 0; | |
1113 | if (opt->unmatch_name_only) { | |
1114 | /* We did not see any hit, so we want to show this */ | |
83caecca | 1115 | show_name(opt, name); |
83b5d2f5 JH |
1116 | return 1; |
1117 | } | |
1118 | ||
60ecac98 RS |
1119 | xdiff_clear_find_func(&xecfg); |
1120 | opt->priv = NULL; | |
1121 | ||
83b5d2f5 JH |
1122 | /* NEEDSWORK: |
1123 | * The real "grep -c foo *.c" gives many "bar.c:0" lines, | |
1124 | * which feels mostly useless but sometimes useful. Maybe | |
1125 | * make it another option? For now suppress them. | |
1126 | */ | |
5b594f45 FK |
1127 | if (opt->count && count) { |
1128 | char buf[32]; | |
55f638bd ML |
1129 | output_color(opt, name, strlen(name), opt->color_filename); |
1130 | output_sep(opt, ':'); | |
1131 | snprintf(buf, sizeof(buf), "%u\n", count); | |
5b594f45 | 1132 | opt->output(opt, buf, strlen(buf)); |
c30c10cf | 1133 | return 1; |
5b594f45 | 1134 | } |
83b5d2f5 JH |
1135 | return !!last_hit; |
1136 | } | |
1137 | ||
0ab7befa JH |
1138 | static void clr_hit_marker(struct grep_expr *x) |
1139 | { | |
1140 | /* All-hit markers are meaningful only at the very top level | |
1141 | * OR node. | |
1142 | */ | |
1143 | while (1) { | |
1144 | x->hit = 0; | |
1145 | if (x->node != GREP_NODE_OR) | |
1146 | return; | |
1147 | x->u.binary.left->hit = 0; | |
1148 | x = x->u.binary.right; | |
1149 | } | |
1150 | } | |
1151 | ||
1152 | static int chk_hit_marker(struct grep_expr *x) | |
1153 | { | |
1154 | /* Top level nodes have hit markers. See if they all are hits */ | |
1155 | while (1) { | |
1156 | if (x->node != GREP_NODE_OR) | |
1157 | return x->hit; | |
1158 | if (!x->u.binary.left->hit) | |
1159 | return 0; | |
1160 | x = x->u.binary.right; | |
1161 | } | |
1162 | } | |
1163 | ||
1164 | int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size) | |
1165 | { | |
1166 | /* | |
1167 | * we do not have to do the two-pass grep when we do not check | |
1168 | * buffer-wide "all-match". | |
1169 | */ | |
1170 | if (!opt->all_match) | |
1171 | return grep_buffer_1(opt, name, buf, size, 0); | |
1172 | ||
1173 | /* Otherwise the toplevel "or" terms hit a bit differently. | |
1174 | * We first clear hit markers from them. | |
1175 | */ | |
1176 | clr_hit_marker(opt->pattern_expression); | |
1177 | grep_buffer_1(opt, name, buf, size, 1); | |
1178 | ||
1179 | if (!chk_hit_marker(opt->pattern_expression)) | |
1180 | return 0; | |
1181 | ||
1182 | return grep_buffer_1(opt, name, buf, size, 0); | |
1183 | } |