Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 1342 lines (1178 sloc) 30.534 kb
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1 #include "cache.h"
2 #include "grep.h"
60ecac98 » René Scharfe
2009-07-02 grep -p: support user defined regular expressions
3 #include "userdiff.h"
6bfce93e » dscho
2007-06-05 Move buffer_is_binary() to xdiff-interface.h
4 #include "xdiff-interface.h"
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
5
a4d7d2c6 » gitster
2008-09-04 log --author/--committer: really match only with name part
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 » René Scharfe
2010-05-22 grep: support NUL chars in search strings for -F
10 p->patternlen = strlen(pat);
a4d7d2c6 » gitster
2008-09-04 log --author/--committer: really match only with name part
11 p->origin = "header";
12 p->no = 0;
13 p->token = GREP_PATTERN_HEAD;
14 p->field = field;
80235ba7 » gitster
2010-01-17 "log --author=me --grep=it" should find intersection, not union
15 *opt->header_tail = p;
16 opt->header_tail = &p->next;
a4d7d2c6 » gitster
2008-09-04 log --author/--committer: really match only with name part
17 p->next = NULL;
18 }
19
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
20 void append_grep_pattern(struct grep_opt *opt, const char *pat,
21 const char *origin, int no, enum grep_pat_token t)
22 {
ed40a095 » René Scharfe
2010-05-22 grep: support NUL chars in search strings for -F
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)
28 {
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
29 struct grep_pat *p = xcalloc(1, sizeof(*p));
30 p->pattern = pat;
ed40a095 » René Scharfe
2010-05-22 grep: support NUL chars in search strings for -F
31 p->patternlen = patlen;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
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 » Fredrik Kuivinen
2010-01-25 Threaded grep
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 » René Scharfe
2010-05-22 grep: support NUL chars in search strings for -F
55 append_grep_pat(ret, pat->pattern, pat->patternlen,
56 pat->origin, pat->no, pat->token);
5b594f45 » Fredrik Kuivinen
2010-01-25 Threaded grep
57 }
58
59 return ret;
60 }
61
a30c148a » Michał Kiedrowicz
2011-05-09 grep: Extract compile_regexp_failed() from compile_regexp()
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 » Michał Kiedrowicz
2011-05-09 git-grep: Learn PCRE
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 » Fredrik Kuivinen
2011-08-21 Use kwset in grep
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 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
158 static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
159 {
c822255c » René Scharfe
2009-01-10 grep: don't call regexec() for fixed strings
160 int err;
161
d7eb527d » René Scharfe
2009-03-07 grep: remove grep_opt argument from match_expr_eval()
162 p->word_regexp = opt->word_regexp;
5183bf67 » briancollins
2009-11-06 grep: Allow case insensitive search of fixed-strings
163 p->ignore_case = opt->ignore_case;
d7eb527d » René Scharfe
2009-03-07 grep: remove grep_opt argument from match_expr_eval()
164
9eceddee » Fredrik Kuivinen
2011-08-21 Use kwset in grep
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) {
171 if (opt->regflags & REG_ICASE || p->ignore_case) {
172 static char trans[256];
173 int i;
174 for (i = 0; i < 256; i++)
175 trans[i] = tolower(i);
176 p->kws = kwsalloc(trans);
177 } else {
178 p->kws = kwsalloc(NULL);
179 }
180 kwsincr(p->kws, p->pattern, p->patternlen);
181 kwsprep(p->kws);
c822255c » René Scharfe
2009-01-10 grep: don't call regexec() for fixed strings
182 return;
9eceddee » Fredrik Kuivinen
2011-08-21 Use kwset in grep
183 }
c822255c » René Scharfe
2009-01-10 grep: don't call regexec() for fixed strings
184
63e7e9d8 » Michał Kiedrowicz
2011-05-09 git-grep: Learn PCRE
185 if (opt->pcre) {
186 compile_pcre_regexp(p, opt);
187 return;
188 }
189
c822255c » René Scharfe
2009-01-10 grep: don't call regexec() for fixed strings
190 err = regcomp(&p->regexp, p->pattern, opt->regflags);
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
191 if (err) {
192 char errbuf[1024];
193 regerror(err, &p->regexp, errbuf, 1024);
194 regfree(&p->regexp);
a30c148a » Michał Kiedrowicz
2011-05-09 grep: Extract compile_regexp_failed() from compile_regexp()
195 compile_regexp_failed(p, errbuf);
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
196 }
197 }
198
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
199 static struct grep_expr *compile_pattern_or(struct grep_pat **);
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
200 static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
201 {
202 struct grep_pat *p;
203 struct grep_expr *x;
204
205 p = *list;
c922b01f » torvalds
2009-04-27 grep: fix segfault when "git grep '('" is given
206 if (!p)
207 return NULL;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
208 switch (p->token) {
209 case GREP_PATTERN: /* atom */
480c1ca6 » Junio C Hamano
2006-09-20 Update grep internal for grepping only in head/body
210 case GREP_PATTERN_HEAD:
211 case GREP_PATTERN_BODY:
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
212 x = xcalloc(1, sizeof (struct grep_expr));
213 x->node = GREP_NODE_ATOM;
214 x->u.atom = p;
215 *list = p->next;
216 return x;
217 case GREP_OPEN_PAREN:
218 *list = p->next;
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
219 x = compile_pattern_or(list);
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
220 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
221 die("unmatched parenthesis");
222 *list = (*list)->next;
223 return x;
224 default:
225 return NULL;
226 }
227 }
228
229 static struct grep_expr *compile_pattern_not(struct grep_pat **list)
230 {
231 struct grep_pat *p;
232 struct grep_expr *x;
233
234 p = *list;
c922b01f » torvalds
2009-04-27 grep: fix segfault when "git grep '('" is given
235 if (!p)
236 return NULL;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
237 switch (p->token) {
238 case GREP_NOT:
239 if (!p->next)
240 die("--not not followed by pattern expression");
241 *list = p->next;
242 x = xcalloc(1, sizeof (struct grep_expr));
243 x->node = GREP_NODE_NOT;
244 x->u.unary = compile_pattern_not(list);
245 if (!x->u.unary)
246 die("--not followed by non pattern expression");
247 return x;
248 default:
249 return compile_pattern_atom(list);
250 }
251 }
252
253 static struct grep_expr *compile_pattern_and(struct grep_pat **list)
254 {
255 struct grep_pat *p;
256 struct grep_expr *x, *y, *z;
257
258 x = compile_pattern_not(list);
259 p = *list;
260 if (p && p->token == GREP_AND) {
261 if (!p->next)
262 die("--and not followed by pattern expression");
263 *list = p->next;
264 y = compile_pattern_and(list);
265 if (!y)
266 die("--and not followed by pattern expression");
267 z = xcalloc(1, sizeof (struct grep_expr));
268 z->node = GREP_NODE_AND;
269 z->u.binary.left = x;
270 z->u.binary.right = y;
271 return z;
272 }
273 return x;
274 }
275
276 static struct grep_expr *compile_pattern_or(struct grep_pat **list)
277 {
278 struct grep_pat *p;
279 struct grep_expr *x, *y, *z;
280
281 x = compile_pattern_and(list);
282 p = *list;
283 if (x && p && p->token != GREP_CLOSE_PAREN) {
284 y = compile_pattern_or(list);
285 if (!y)
286 die("not a pattern expression %s", p->pattern);
287 z = xcalloc(1, sizeof (struct grep_expr));
288 z->node = GREP_NODE_OR;
289 z->u.binary.left = x;
290 z->u.binary.right = y;
291 return z;
292 }
293 return x;
294 }
295
296 static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
297 {
298 return compile_pattern_or(list);
299 }
300
5aaeb733 » gitster
2010-09-12 log --author: take union of multiple "author" requests
301 static struct grep_expr *grep_true_expr(void)
302 {
303 struct grep_expr *z = xcalloc(1, sizeof(*z));
304 z->node = GREP_NODE_TRUE;
305 return z;
306 }
307
308 static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
309 {
310 struct grep_expr *z = xcalloc(1, sizeof(*z));
311 z->node = GREP_NODE_OR;
312 z->u.binary.left = left;
313 z->u.binary.right = right;
314 return z;
315 }
316
95ce9ce2 » gitster
2010-09-12 grep: move logic to compile header pattern into a separate helper
317 static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
318 {
319 struct grep_pat *p;
95ce9ce2 » gitster
2010-09-12 grep: move logic to compile header pattern into a separate helper
320 struct grep_expr *header_expr;
5aaeb733 » gitster
2010-09-12 log --author: take union of multiple "author" requests
321 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
322 enum grep_header_field fld;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
323
95ce9ce2 » gitster
2010-09-12 grep: move logic to compile header pattern into a separate helper
324 if (!opt->header_list)
325 return NULL;
326 p = opt->header_list;
327 for (p = opt->header_list; p; p = p->next) {
328 if (p->token != GREP_PATTERN_HEAD)
329 die("bug: a non-header pattern in grep header list.");
330 if (p->field < 0 || GREP_HEADER_FIELD_MAX <= p->field)
331 die("bug: unknown header field %d", p->field);
332 compile_regexp(p, opt);
80235ba7 » gitster
2010-01-17 "log --author=me --grep=it" should find intersection, not union
333 }
5aaeb733 » gitster
2010-09-12 log --author: take union of multiple "author" requests
334
335 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
336 header_group[fld] = NULL;
337
338 for (p = opt->header_list; p; p = p->next) {
339 struct grep_expr *h;
340 struct grep_pat *pp = p;
341
342 h = compile_pattern_atom(&pp);
343 if (!h || pp != p->next)
344 die("bug: malformed header expr");
345 if (!header_group[p->field]) {
346 header_group[p->field] = h;
347 continue;
348 }
349 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
350 }
351
352 header_expr = NULL;
353
354 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
355 if (!header_group[fld])
356 continue;
357 if (!header_expr)
358 header_expr = grep_true_expr();
359 header_expr = grep_or_expr(header_group[fld], header_expr);
360 }
95ce9ce2 » gitster
2010-09-12 grep: move logic to compile header pattern into a separate helper
361 return header_expr;
362 }
363
364 void compile_grep_patterns(struct grep_opt *opt)
365 {
366 struct grep_pat *p;
367 struct grep_expr *header_expr = prep_header_patterns(opt);
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
368
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
369 for (p = opt->pattern_list; p; p = p->next) {
480c1ca6 » Junio C Hamano
2006-09-20 Update grep internal for grepping only in head/body
370 switch (p->token) {
371 case GREP_PATTERN: /* atom */
372 case GREP_PATTERN_HEAD:
373 case GREP_PATTERN_BODY:
c822255c » René Scharfe
2009-01-10 grep: don't call regexec() for fixed strings
374 compile_regexp(p, opt);
480c1ca6 » Junio C Hamano
2006-09-20 Update grep internal for grepping only in head/body
375 break;
376 default:
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
377 opt->extended = 1;
480c1ca6 » Junio C Hamano
2006-09-20 Update grep internal for grepping only in head/body
378 break;
379 }
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
380 }
381
80235ba7 » gitster
2010-01-17 "log --author=me --grep=it" should find intersection, not union
382 if (opt->all_match || header_expr)
383 opt->extended = 1;
384 else if (!opt->extended)
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
385 return;
386
387 p = opt->pattern_list;
ba150a3f » Michele Ballabio
2009-03-18 git log: avoid segfault with --all-match
388 if (p)
389 opt->pattern_expression = compile_pattern_expr(&p);
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
390 if (p)
391 die("incomplete pattern expression: %s", p->pattern);
80235ba7 » gitster
2010-01-17 "log --author=me --grep=it" should find intersection, not union
392
393 if (!header_expr)
394 return;
395
5aaeb733 » gitster
2010-09-12 log --author: take union of multiple "author" requests
396 if (!opt->pattern_expression)
80235ba7 » gitster
2010-01-17 "log --author=me --grep=it" should find intersection, not union
397 opt->pattern_expression = header_expr;
5aaeb733 » gitster
2010-09-12 log --author: take union of multiple "author" requests
398 else
399 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
400 header_expr);
80235ba7 » gitster
2010-01-17 "log --author=me --grep=it" should find intersection, not union
401 opt->all_match = 1;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
402 }
403
b48fb5b6 » Junio C Hamano
2006-09-27 grep: free expressions and patterns when done.
404 static void free_pattern_expr(struct grep_expr *x)
405 {
406 switch (x->node) {
5aaeb733 » gitster
2010-09-12 log --author: take union of multiple "author" requests
407 case GREP_NODE_TRUE:
b48fb5b6 » Junio C Hamano
2006-09-27 grep: free expressions and patterns when done.
408 case GREP_NODE_ATOM:
409 break;
410 case GREP_NODE_NOT:
411 free_pattern_expr(x->u.unary);
412 break;
413 case GREP_NODE_AND:
414 case GREP_NODE_OR:
415 free_pattern_expr(x->u.binary.left);
416 free_pattern_expr(x->u.binary.right);
417 break;
418 }
419 free(x);
420 }
421
422 void free_grep_patterns(struct grep_opt *opt)
423 {
424 struct grep_pat *p, *n;
425
426 for (p = opt->pattern_list; p; p = n) {
427 n = p->next;
428 switch (p->token) {
429 case GREP_PATTERN: /* atom */
430 case GREP_PATTERN_HEAD:
431 case GREP_PATTERN_BODY:
9eceddee » Fredrik Kuivinen
2011-08-21 Use kwset in grep
432 if (p->kws)
433 kwsfree(p->kws);
434 else if (p->pcre_regexp)
63e7e9d8 » Michał Kiedrowicz
2011-05-09 git-grep: Learn PCRE
435 free_pcre_regexp(p);
436 else
437 regfree(&p->regexp);
b48fb5b6 » Junio C Hamano
2006-09-27 grep: free expressions and patterns when done.
438 break;
439 default:
440 break;
441 }
442 free(p);
443 }
444
445 if (!opt->extended)
446 return;
447 free_pattern_expr(opt->pattern_expression);
448 }
449
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
450 static char *end_of_line(char *cp, unsigned long *left)
451 {
452 unsigned long l = *left;
453 while (l && *cp != '\n') {
454 l--;
455 cp++;
456 }
457 *left = l;
458 return cp;
459 }
460
461 static int word_char(char ch)
462 {
463 return isalnum(ch) || ch == '_';
464 }
465
55f638bd » MarkLodato
2010-03-07 grep: Colorize filename, line number, and separator
466 static void output_color(struct grep_opt *opt, const void *data, size_t size,
467 const char *color)
468 {
daa0c3d9 »
2011-08-17 color: delay auto-color decision until point of use
469 if (want_color(opt->color) && color && color[0]) {
55f638bd » MarkLodato
2010-03-07 grep: Colorize filename, line number, and separator
470 opt->output(opt, color, strlen(color));
471 opt->output(opt, data, size);
472 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
473 } else
474 opt->output(opt, data, size);
475 }
476
477 static void output_sep(struct grep_opt *opt, char sign)
478 {
479 if (opt->null_following_name)
480 opt->output(opt, "\0", 1);
481 else
482 output_color(opt, &sign, 1, opt->color_sep);
483 }
484
83caecca » Raphael Zimmerer
2008-10-01 git grep: Add "-z/--null" option as in GNU's grep.
485 static void show_name(struct grep_opt *opt, const char *name)
486 {
55f638bd » MarkLodato
2010-03-07 grep: Colorize filename, line number, and separator
487 output_color(opt, name, strlen(name), opt->color_filename);
5b594f45 » Fredrik Kuivinen
2010-01-25 Threaded grep
488 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
83caecca » Raphael Zimmerer
2008-10-01 git grep: Add "-z/--null" option as in GNU's grep.
489 }
490
ed40a095 » René Scharfe
2010-05-22 grep: support NUL chars in search strings for -F
491 static int fixmatch(struct grep_pat *p, char *line, char *eol,
492 regmatch_t *match)
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
493 {
9eceddee » Fredrik Kuivinen
2011-08-21 Use kwset in grep
494 struct kwsmatch kwsm;
495 size_t offset = kwsexec(p->kws, line, eol - line, &kwsm);
496 if (offset == -1) {
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
497 match->rm_so = match->rm_eo = -1;
498 return REG_NOMATCH;
9eceddee » Fredrik Kuivinen
2011-08-21 Use kwset in grep
499 } else {
500 match->rm_so = offset;
501 match->rm_eo = match->rm_so + kwsm.size[0];
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
502 return 0;
503 }
504 }
505
f96e5673 » René Scharfe
2010-05-22 grep: use REG_STARTEND for all matching if available
506 static int regmatch(const regex_t *preg, char *line, char *eol,
507 regmatch_t *match, int eflags)
508 {
509 #ifdef REG_STARTEND
510 match->rm_so = 0;
511 match->rm_eo = eol - line;
512 eflags |= REG_STARTEND;
513 #endif
514 return regexec(preg, line, 1, match, eflags);
515 }
516
97e77784 » Michał Kiedrowicz
2011-05-05 grep: Put calls to fixmatch() and regmatch() into patmatch()
517 static int patmatch(struct grep_pat *p, char *line, char *eol,
518 regmatch_t *match, int eflags)
519 {
520 int hit;
521
522 if (p->fixed)
523 hit = !fixmatch(p, line, eol, match);
63e7e9d8 » Michał Kiedrowicz
2011-05-09 git-grep: Learn PCRE
524 else if (p->pcre_regexp)
525 hit = !pcrematch(p, line, eol, match, eflags);
97e77784 » Michał Kiedrowicz
2011-05-05 grep: Put calls to fixmatch() and regmatch() into patmatch()
526 else
527 hit = !regmatch(&p->regexp, line, eol, match, eflags);
528
529 return hit;
530 }
531
a4d7d2c6 » gitster
2008-09-04 log --author/--committer: really match only with name part
532 static int strip_timestamp(char *bol, char **eol_p)
533 {
534 char *eol = *eol_p;
535 int ch;
536
537 while (bol < --eol) {
538 if (*eol != '>')
539 continue;
540 *eol_p = ++eol;
541 ch = *eol;
542 *eol = '\0';
543 return ch;
544 }
545 return 0;
546 }
547
548 static struct {
549 const char *field;
550 size_t len;
551 } header_field[] = {
552 { "author ", 7 },
553 { "committer ", 10 },
554 };
555
d7eb527d » René Scharfe
2009-03-07 grep: remove grep_opt argument from match_expr_eval()
556 static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
79212772 » René Scharfe
2009-03-07 grep: add pmatch and eflags arguments to match_one_pattern()
557 enum grep_context ctx,
558 regmatch_t *pmatch, int eflags)
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
559 {
560 int hit = 0;
a4d7d2c6 » gitster
2008-09-04 log --author/--committer: really match only with name part
561 int saved_ch = 0;
e701fadb » René Scharfe
2009-05-20 grep: fix word-regexp colouring
562 const char *start = bol;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
563
480c1ca6 » Junio C Hamano
2006-09-20 Update grep internal for grepping only in head/body
564 if ((p->token != GREP_PATTERN) &&
565 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
566 return 0;
567
a4d7d2c6 » gitster
2008-09-04 log --author/--committer: really match only with name part
568 if (p->token == GREP_PATTERN_HEAD) {
569 const char *field;
570 size_t len;
571 assert(p->field < ARRAY_SIZE(header_field));
572 field = header_field[p->field].field;
573 len = header_field[p->field].len;
574 if (strncmp(bol, field, len))
575 return 0;
576 bol += len;
577 saved_ch = strip_timestamp(bol, &eol);
578 }
579
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
580 again:
97e77784 » Michał Kiedrowicz
2011-05-05 grep: Put calls to fixmatch() and regmatch() into patmatch()
581 hit = patmatch(p, bol, eol, pmatch, eflags);
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
582
d7eb527d » René Scharfe
2009-03-07 grep: remove grep_opt argument from match_expr_eval()
583 if (hit && p->word_regexp) {
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
584 if ((pmatch[0].rm_so < 0) ||
84201eae » René Scharfe
2009-06-03 grep: fix empty word-regexp matches
585 (eol - bol) < pmatch[0].rm_so ||
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
586 (pmatch[0].rm_eo < 0) ||
587 (eol - bol) < pmatch[0].rm_eo)
588 die("regexp returned nonsense");
589
590 /* Match beginning must be either beginning of the
591 * line, or at word boundary (i.e. the last char must
592 * not be a word char). Similarly, match end must be
593 * either end of the line, or at word boundary
594 * (i.e. the next char must not be a word char).
595 */
fb62eb7f » René Scharfe
2009-01-10 grep -w: forward to next possible position after rejected match
596 if ( ((pmatch[0].rm_so == 0) ||
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
597 !word_char(bol[pmatch[0].rm_so-1])) &&
598 ((pmatch[0].rm_eo == (eol-bol)) ||
599 !word_char(bol[pmatch[0].rm_eo])) )
600 ;
601 else
602 hit = 0;
603
84201eae » René Scharfe
2009-06-03 grep: fix empty word-regexp matches
604 /* Words consist of at least one character. */
605 if (pmatch->rm_so == pmatch->rm_eo)
606 hit = 0;
607
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
608 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
609 /* There could be more than one match on the
610 * line, and the first match might not be
611 * strict word match. But later ones could be!
fb62eb7f » René Scharfe
2009-01-10 grep -w: forward to next possible position after rejected match
612 * Forward to the next possible start, i.e. the
613 * next position following a non-word char.
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
614 */
615 bol = pmatch[0].rm_so + bol + 1;
fb62eb7f » René Scharfe
2009-01-10 grep -w: forward to next possible position after rejected match
616 while (word_char(bol[-1]) && bol < eol)
617 bol++;
dbb6a4ad » René Scharfe
2009-05-23 grep: fix word-regexp at the beginning of lines
618 eflags |= REG_NOTBOL;
fb62eb7f » René Scharfe
2009-01-10 grep -w: forward to next possible position after rejected match
619 if (bol < eol)
620 goto again;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
621 }
622 }
a4d7d2c6 » gitster
2008-09-04 log --author/--committer: really match only with name part
623 if (p->token == GREP_PATTERN_HEAD && saved_ch)
624 *eol = saved_ch;
e701fadb » René Scharfe
2009-05-20 grep: fix word-regexp colouring
625 if (hit) {
626 pmatch[0].rm_so += bol - start;
627 pmatch[0].rm_eo += bol - start;
628 }
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
629 return hit;
630 }
631
d7eb527d » René Scharfe
2009-03-07 grep: remove grep_opt argument from match_expr_eval()
632 static int match_expr_eval(struct grep_expr *x, char *bol, char *eol,
633 enum grep_context ctx, int collect_hits)
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
634 {
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
635 int h = 0;
79212772 » René Scharfe
2009-03-07 grep: add pmatch and eflags arguments to match_one_pattern()
636 regmatch_t match;
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
637
c922b01f » torvalds
2009-04-27 grep: fix segfault when "git grep '('" is given
638 if (!x)
639 die("Not a valid grep expression");
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
640 switch (x->node) {
5aaeb733 » gitster
2010-09-12 log --author: take union of multiple "author" requests
641 case GREP_NODE_TRUE:
642 h = 1;
643 break;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
644 case GREP_NODE_ATOM:
79212772 » René Scharfe
2009-03-07 grep: add pmatch and eflags arguments to match_one_pattern()
645 h = match_one_pattern(x->u.atom, bol, eol, ctx, &match, 0);
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
646 break;
647 case GREP_NODE_NOT:
d7eb527d » René Scharfe
2009-03-07 grep: remove grep_opt argument from match_expr_eval()
648 h = !match_expr_eval(x->u.unary, bol, eol, ctx, 0);
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
649 break;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
650 case GREP_NODE_AND:
d7eb527d » René Scharfe
2009-03-07 grep: remove grep_opt argument from match_expr_eval()
651 if (!match_expr_eval(x->u.binary.left, bol, eol, ctx, 0))
252d560d » René Scharfe
2009-03-07 grep: micro-optimize hit collection for AND nodes
652 return 0;
d7eb527d » René Scharfe
2009-03-07 grep: remove grep_opt argument from match_expr_eval()
653 h = match_expr_eval(x->u.binary.right, bol, eol, ctx, 0);
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
654 break;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
655 case GREP_NODE_OR:
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
656 if (!collect_hits)
d7eb527d » René Scharfe
2009-03-07 grep: remove grep_opt argument from match_expr_eval()
657 return (match_expr_eval(x->u.binary.left,
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
658 bol, eol, ctx, 0) ||
d7eb527d » René Scharfe
2009-03-07 grep: remove grep_opt argument from match_expr_eval()
659 match_expr_eval(x->u.binary.right,
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
660 bol, eol, ctx, 0));
d7eb527d » René Scharfe
2009-03-07 grep: remove grep_opt argument from match_expr_eval()
661 h = match_expr_eval(x->u.binary.left, bol, eol, ctx, 0);
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
662 x->u.binary.left->hit |= h;
d7eb527d » René Scharfe
2009-03-07 grep: remove grep_opt argument from match_expr_eval()
663 h |= match_expr_eval(x->u.binary.right, bol, eol, ctx, 1);
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
664 break;
665 default:
d7530708 » aspotashev
2009-01-04 remove trailing LF in die() messages
666 die("Unexpected node type (internal error) %d", x->node);
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
667 }
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
668 if (collect_hits)
669 x->hit |= h;
670 return h;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
671 }
672
480c1ca6 » Junio C Hamano
2006-09-20 Update grep internal for grepping only in head/body
673 static int match_expr(struct grep_opt *opt, char *bol, char *eol,
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
674 enum grep_context ctx, int collect_hits)
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
675 {
676 struct grep_expr *x = opt->pattern_expression;
d7eb527d » René Scharfe
2009-03-07 grep: remove grep_opt argument from match_expr_eval()
677 return match_expr_eval(x, bol, eol, ctx, collect_hits);
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
678 }
679
480c1ca6 » Junio C Hamano
2006-09-20 Update grep internal for grepping only in head/body
680 static int match_line(struct grep_opt *opt, char *bol, char *eol,
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
681 enum grep_context ctx, int collect_hits)
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
682 {
683 struct grep_pat *p;
79212772 » René Scharfe
2009-03-07 grep: add pmatch and eflags arguments to match_one_pattern()
684 regmatch_t match;
685
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
686 if (opt->extended)
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
687 return match_expr(opt, bol, eol, ctx, collect_hits);
688
689 /* we do not call with collect_hits without being extended */
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
690 for (p = opt->pattern_list; p; p = p->next) {
79212772 » René Scharfe
2009-03-07 grep: add pmatch and eflags arguments to match_one_pattern()
691 if (match_one_pattern(p, bol, eol, ctx, &match, 0))
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
692 return 1;
693 }
694 return 0;
695 }
696
7e8f59d5 » René Scharfe
2009-03-07 grep: color patterns in output
697 static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
698 enum grep_context ctx,
699 regmatch_t *pmatch, int eflags)
700 {
701 regmatch_t match;
702
703 if (!match_one_pattern(p, bol, eol, ctx, &match, eflags))
704 return 0;
705 if (match.rm_so < 0 || match.rm_eo < 0)
706 return 0;
707 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
708 if (match.rm_so > pmatch->rm_so)
709 return 1;
710 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
711 return 1;
712 }
713 pmatch->rm_so = match.rm_so;
714 pmatch->rm_eo = match.rm_eo;
715 return 1;
716 }
717
718 static int next_match(struct grep_opt *opt, char *bol, char *eol,
719 enum grep_context ctx, regmatch_t *pmatch, int eflags)
720 {
721 struct grep_pat *p;
722 int hit = 0;
723
724 pmatch->rm_so = pmatch->rm_eo = -1;
725 if (bol < eol) {
726 for (p = opt->pattern_list; p; p = p->next) {
727 switch (p->token) {
728 case GREP_PATTERN: /* atom */
729 case GREP_PATTERN_HEAD:
730 case GREP_PATTERN_BODY:
731 hit |= match_next_pattern(p, bol, eol, ctx,
732 pmatch, eflags);
733 break;
734 default:
735 break;
736 }
737 }
738 }
739 return hit;
740 }
741
742 static void show_line(struct grep_opt *opt, char *bol, char *eol,
743 const char *name, unsigned lno, char sign)
744 {
745 int rest = eol - bol;
00588bb5 » MarkLodato
2010-03-07 grep: Colorize selected, context, and function lines
746 char *line_color = NULL;
7e8f59d5 » René Scharfe
2009-03-07 grep: color patterns in output
747
a8f0e764 » René Scharfe
2011-06-05 grep: add --break
748 if (opt->file_break && opt->last_shown == 0) {
749 if (opt->show_hunk_mark)
750 opt->output(opt, "\n", 1);
ba8ea749 » René Scharfe
2011-08-01 grep: add option to show whole function as context
751 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
046802d0 » René Scharfe
2009-07-02 grep: print context hunk marks between files
752 if (opt->last_shown == 0) {
55f638bd » MarkLodato
2010-03-07 grep: Colorize filename, line number, and separator
753 if (opt->show_hunk_mark) {
754 output_color(opt, "--", 2, opt->color_sep);
755 opt->output(opt, "\n", 1);
07b838f0 » gitster
2010-04-03 Merge branch 'rs/threaded-grep-context'
756 }
55f638bd » MarkLodato
2010-03-07 grep: Colorize filename, line number, and separator
757 } else if (lno > opt->last_shown + 1) {
758 output_color(opt, "--", 2, opt->color_sep);
759 opt->output(opt, "\n", 1);
760 }
5dd06d38 » René Scharfe
2009-07-02 grep: move context hunk mark handling into show_line()
761 }
1d84f72e » René Scharfe
2011-06-05 grep: add --heading
762 if (opt->heading && opt->last_shown == 0) {
763 output_color(opt, name, strlen(name), opt->color_filename);
764 opt->output(opt, "\n", 1);
765 }
5dd06d38 » René Scharfe
2009-07-02 grep: move context hunk mark handling into show_line()
766 opt->last_shown = lno;
767
1d84f72e » René Scharfe
2011-06-05 grep: add --heading
768 if (!opt->heading && opt->pathname) {
55f638bd » MarkLodato
2010-03-07 grep: Colorize filename, line number, and separator
769 output_color(opt, name, strlen(name), opt->color_filename);
770 output_sep(opt, sign);
5b594f45 » Fredrik Kuivinen
2010-01-25 Threaded grep
771 }
772 if (opt->linenum) {
773 char buf[32];
774 snprintf(buf, sizeof(buf), "%d", lno);
55f638bd » MarkLodato
2010-03-07 grep: Colorize filename, line number, and separator
775 output_color(opt, buf, strlen(buf), opt->color_lineno);
776 output_sep(opt, sign);
5b594f45 » Fredrik Kuivinen
2010-01-25 Threaded grep
777 }
7e8f59d5 » René Scharfe
2009-03-07 grep: color patterns in output
778 if (opt->color) {
779 regmatch_t match;
780 enum grep_context ctx = GREP_CONTEXT_BODY;
781 int ch = *eol;
782 int eflags = 0;
783
00588bb5 » MarkLodato
2010-03-07 grep: Colorize selected, context, and function lines
784 if (sign == ':')
785 line_color = opt->color_selected;
786 else if (sign == '-')
787 line_color = opt->color_context;
788 else if (sign == '=')
789 line_color = opt->color_function;
7e8f59d5 » René Scharfe
2009-03-07 grep: color patterns in output
790 *eol = '\0';
791 while (next_match(opt, bol, eol, ctx, &match, eflags)) {
1f5b9cc4 » René Scharfe
2009-06-01 grep: fix colouring of matches with zero length
792 if (match.rm_so == match.rm_eo)
793 break;
5b594f45 » Fredrik Kuivinen
2010-01-25 Threaded grep
794
00588bb5 » MarkLodato
2010-03-07 grep: Colorize selected, context, and function lines
795 output_color(opt, bol, match.rm_so, line_color);
55f638bd » MarkLodato
2010-03-07 grep: Colorize filename, line number, and separator
796 output_color(opt, bol + match.rm_so,
797 match.rm_eo - match.rm_so,
798 opt->color_match);
7e8f59d5 » René Scharfe
2009-03-07 grep: color patterns in output
799 bol += match.rm_eo;
800 rest -= match.rm_eo;
801 eflags = REG_NOTBOL;
802 }
803 *eol = ch;
804 }
00588bb5 » MarkLodato
2010-03-07 grep: Colorize selected, context, and function lines
805 output_color(opt, bol, rest, line_color);
5b594f45 » Fredrik Kuivinen
2010-01-25 Threaded grep
806 opt->output(opt, "\n", 1);
7e8f59d5 » René Scharfe
2009-03-07 grep: color patterns in output
807 }
808
0579f91d » trast
2011-12-12 grep: enable threading with -p and -W using lazy attribute lookup
809 #ifndef NO_PTHREADS
78db6ea9 »
2012-02-02 grep: make locking flag global
810 int grep_use_locks;
811
0579f91d » trast
2011-12-12 grep: enable threading with -p and -W using lazy attribute lookup
812 /*
813 * This lock protects access to the gitattributes machinery, which is
814 * not thread-safe.
815 */
816 pthread_mutex_t grep_attr_mutex;
817
78db6ea9 »
2012-02-02 grep: make locking flag global
818 static inline void grep_attr_lock(void)
0579f91d » trast
2011-12-12 grep: enable threading with -p and -W using lazy attribute lookup
819 {
78db6ea9 »
2012-02-02 grep: make locking flag global
820 if (grep_use_locks)
0579f91d » trast
2011-12-12 grep: enable threading with -p and -W using lazy attribute lookup
821 pthread_mutex_lock(&grep_attr_mutex);
822 }
823
78db6ea9 »
2012-02-02 grep: make locking flag global
824 static inline void grep_attr_unlock(void)
0579f91d » trast
2011-12-12 grep: enable threading with -p and -W using lazy attribute lookup
825 {
78db6ea9 »
2012-02-02 grep: make locking flag global
826 if (grep_use_locks)
0579f91d » trast
2011-12-12 grep: enable threading with -p and -W using lazy attribute lookup
827 pthread_mutex_unlock(&grep_attr_mutex);
828 }
b3aeb285 »
2012-02-02 grep: move sha1-reading mutex into low-level code
829
830 /*
831 * Same as git_attr_mutex, but protecting the thread-unsafe object db access.
832 */
833 pthread_mutex_t grep_read_mutex;
834
0579f91d » trast
2011-12-12 grep: enable threading with -p and -W using lazy attribute lookup
835 #else
78db6ea9 »
2012-02-02 grep: make locking flag global
836 #define grep_attr_lock()
837 #define grep_attr_unlock()
0579f91d » trast
2011-12-12 grep: enable threading with -p and -W using lazy attribute lookup
838 #endif
839
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
840 static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
2944e4e6 » René Scharfe
2009-07-02 grep: add option -p/--show-function
841 {
60ecac98 » René Scharfe
2009-07-02 grep -p: support user defined regular expressions
842 xdemitconf_t *xecfg = opt->priv;
0579f91d » trast
2011-12-12 grep: enable threading with -p and -W using lazy attribute lookup
843 if (xecfg && !xecfg->find_func) {
844 struct userdiff_driver *drv;
78db6ea9 »
2012-02-02 grep: make locking flag global
845 grep_attr_lock();
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
846 drv = userdiff_find_by_path(gs->name);
78db6ea9 »
2012-02-02 grep: make locking flag global
847 grep_attr_unlock();
0579f91d » trast
2011-12-12 grep: enable threading with -p and -W using lazy attribute lookup
848 if (drv && drv->funcname.pattern) {
849 const struct userdiff_funcname *pe = &drv->funcname;
850 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
851 } else {
852 xecfg = opt->priv = NULL;
853 }
854 }
855
856 if (xecfg) {
60ecac98 » René Scharfe
2009-07-02 grep -p: support user defined regular expressions
857 char buf[1];
858 return xecfg->find_func(bol, eol - bol, buf, 1,
859 xecfg->find_func_priv) >= 0;
860 }
861
2944e4e6 » René Scharfe
2009-07-02 grep: add option -p/--show-function
862 if (bol == eol)
863 return 0;
864 if (isalpha(*bol) || *bol == '_' || *bol == '$')
865 return 1;
866 return 0;
867 }
868
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
869 static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
870 char *bol, unsigned lno)
2944e4e6 » René Scharfe
2009-07-02 grep: add option -p/--show-function
871 {
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
872 while (bol > gs->buf) {
2944e4e6 » René Scharfe
2009-07-02 grep: add option -p/--show-function
873 char *eol = --bol;
874
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
875 while (bol > gs->buf && bol[-1] != '\n')
2944e4e6 » René Scharfe
2009-07-02 grep: add option -p/--show-function
876 bol--;
877 lno--;
878
879 if (lno <= opt->last_shown)
880 break;
881
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
882 if (match_funcname(opt, gs, bol, eol)) {
883 show_line(opt, bol, eol, gs->name, lno, '=');
2944e4e6 » René Scharfe
2009-07-02 grep: add option -p/--show-function
884 break;
885 }
886 }
887 }
888
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
889 static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
ba8ea749 » René Scharfe
2011-08-01 grep: add option to show whole function as context
890 char *bol, char *end, unsigned lno)
49de3216 » René Scharfe
2009-07-02 grep: handle pre context lines on demand
891 {
2944e4e6 » René Scharfe
2009-07-02 grep: add option -p/--show-function
892 unsigned cur = lno, from = 1, funcname_lno = 0;
ba8ea749 » René Scharfe
2011-08-01 grep: add option to show whole function as context
893 int funcname_needed = !!opt->funcname;
894
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
895 if (opt->funcbody && !match_funcname(opt, gs, bol, end))
ba8ea749 » René Scharfe
2011-08-01 grep: add option to show whole function as context
896 funcname_needed = 2;
49de3216 » René Scharfe
2009-07-02 grep: handle pre context lines on demand
897
898 if (opt->pre_context < lno)
899 from = lno - opt->pre_context;
900 if (from <= opt->last_shown)
901 from = opt->last_shown + 1;
902
903 /* Rewind. */
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
904 while (bol > gs->buf &&
ba8ea749 » René Scharfe
2011-08-01 grep: add option to show whole function as context
905 cur > (funcname_needed == 2 ? opt->last_shown + 1 : from)) {
2944e4e6 » René Scharfe
2009-07-02 grep: add option -p/--show-function
906 char *eol = --bol;
907
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
908 while (bol > gs->buf && bol[-1] != '\n')
49de3216 » René Scharfe
2009-07-02 grep: handle pre context lines on demand
909 bol--;
910 cur--;
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
911 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
2944e4e6 » René Scharfe
2009-07-02 grep: add option -p/--show-function
912 funcname_lno = cur;
913 funcname_needed = 0;
914 }
49de3216 » René Scharfe
2009-07-02 grep: handle pre context lines on demand
915 }
916
2944e4e6 » René Scharfe
2009-07-02 grep: add option -p/--show-function
917 /* We need to look even further back to find a function signature. */
918 if (opt->funcname && funcname_needed)
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
919 show_funcname_line(opt, gs, bol, cur);
2944e4e6 » René Scharfe
2009-07-02 grep: add option -p/--show-function
920
49de3216 » René Scharfe
2009-07-02 grep: handle pre context lines on demand
921 /* Back forward. */
922 while (cur < lno) {
2944e4e6 » René Scharfe
2009-07-02 grep: add option -p/--show-function
923 char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
49de3216 » René Scharfe
2009-07-02 grep: handle pre context lines on demand
924
925 while (*eol != '\n')
926 eol++;
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
927 show_line(opt, bol, eol, gs->name, cur, sign);
49de3216 » René Scharfe
2009-07-02 grep: handle pre context lines on demand
928 bol = eol + 1;
929 cur++;
930 }
931 }
932
a26345b6 » gitster
2010-01-10 grep: optimize built-in grep by skipping lines that do not hit
933 static int should_lookahead(struct grep_opt *opt)
934 {
935 struct grep_pat *p;
936
937 if (opt->extended)
938 return 0; /* punt for too complex stuff */
939 if (opt->invert)
940 return 0;
941 for (p = opt->pattern_list; p; p = p->next) {
942 if (p->token != GREP_PATTERN)
943 return 0; /* punt for "header only" and stuff */
944 }
945 return 1;
946 }
947
948 static int look_ahead(struct grep_opt *opt,
949 unsigned long *left_p,
950 unsigned *lno_p,
951 char **bol_p)
952 {
953 unsigned lno = *lno_p;
954 char *bol = *bol_p;
955 struct grep_pat *p;
956 char *sp, *last_bol;
957 regoff_t earliest = -1;
958
959 for (p = opt->pattern_list; p; p = p->next) {
960 int hit;
961 regmatch_t m;
962
97e77784 » Michał Kiedrowicz
2011-05-05 grep: Put calls to fixmatch() and regmatch() into patmatch()
963 hit = patmatch(p, bol, bol + *left_p, &m, 0);
a26345b6 » gitster
2010-01-10 grep: optimize built-in grep by skipping lines that do not hit
964 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
965 continue;
966 if (earliest < 0 || m.rm_so < earliest)
967 earliest = m.rm_so;
968 }
969
970 if (earliest < 0) {
971 *bol_p = bol + *left_p;
972 *left_p = 0;
973 return 1;
974 }
975 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
976 ; /* find the beginning of the line */
977 last_bol = sp;
978
979 for (sp = bol; sp < last_bol; sp++) {
980 if (*sp == '\n')
981 lno++;
982 }
983 *left_p -= last_bol - bol;
984 *bol_p = last_bol;
985 *lno_p = lno;
986 return 0;
987 }
988
5b594f45 » Fredrik Kuivinen
2010-01-25 Threaded grep
989 static void std_output(struct grep_opt *opt, const void *buf, size_t size)
990 {
991 fwrite(buf, size, 1, stdout);
992 }
993
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
994 static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
995 {
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
996 char *bol;
997 unsigned long left;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
998 unsigned lno = 1;
999 unsigned last_hit = 0;
1000 int binary_match_only = 0;
1001 unsigned count = 0;
a26345b6 » gitster
2010-01-10 grep: optimize built-in grep by skipping lines that do not hit
1002 int try_lookahead = 0;
ba8ea749 » René Scharfe
2011-08-01 grep: add option to show whole function as context
1003 int show_function = 0;
480c1ca6 » Junio C Hamano
2006-09-20 Update grep internal for grepping only in head/body
1004 enum grep_context ctx = GREP_CONTEXT_HEAD;
60ecac98 » René Scharfe
2009-07-02 grep -p: support user defined regular expressions
1005 xdemitconf_t xecfg;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1006
5b594f45 » Fredrik Kuivinen
2010-01-25 Threaded grep
1007 if (!opt->output)
1008 opt->output = std_output;
1009
ba8ea749 » René Scharfe
2011-08-01 grep: add option to show whole function as context
1010 if (opt->pre_context || opt->post_context || opt->file_break ||
1011 opt->funcbody) {
08303c36 » René Scharfe
2011-06-05 grep: fix coloring of hunk marks between files
1012 /* Show hunk marks, except for the first file. */
1013 if (opt->last_shown)
1014 opt->show_hunk_mark = 1;
1015 /*
1016 * If we're using threads then we can't easily identify
1017 * the first file. Always put hunk marks in that case
1018 * and skip the very first one later in work_done().
1019 */
1020 if (opt->output != std_output)
1021 opt->show_hunk_mark = 1;
1022 }
431d6e7b » René Scharfe
2010-03-15 grep: enable threading for context line printing
1023 opt->last_shown = 0;
1024
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1025 if (grep_source_load(gs) < 0)
1026 return 0;
1027
64fcec78 » René Scharfe
2010-05-22 grep: grep: refactor handling of binary mode options
1028 switch (opt->binary) {
1029 case GREP_BINARY_DEFAULT:
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1030 if (buffer_is_binary(gs->buf, gs->size))
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1031 binary_match_only = 1;
64fcec78 » René Scharfe
2010-05-22 grep: grep: refactor handling of binary mode options
1032 break;
1033 case GREP_BINARY_NOMATCH:
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1034 if (buffer_is_binary(gs->buf, gs->size))
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1035 return 0; /* Assume unmatch */
64fcec78 » René Scharfe
2010-05-22 grep: grep: refactor handling of binary mode options
1036 break;
1037 case GREP_BINARY_TEXT:
1038 break;
1039 default:
1040 die("bug: unknown binary handling mode");
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1041 }
1042
60ecac98 » René Scharfe
2009-07-02 grep -p: support user defined regular expressions
1043 memset(&xecfg, 0, sizeof(xecfg));
0579f91d » trast
2011-12-12 grep: enable threading with -p and -W using lazy attribute lookup
1044 opt->priv = &xecfg;
1045
a26345b6 » gitster
2010-01-10 grep: optimize built-in grep by skipping lines that do not hit
1046 try_lookahead = should_lookahead(opt);
60ecac98 » René Scharfe
2009-07-02 grep -p: support user defined regular expressions
1047
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1048 bol = gs->buf;
1049 left = gs->size;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1050 while (left) {
1051 char *eol, ch;
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
1052 int hit;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1053
a26345b6 » gitster
2010-01-10 grep: optimize built-in grep by skipping lines that do not hit
1054 /*
8997da38 » Michał Kiedrowicz
2011-05-09 grep: Fix a typo in a comment
1055 * look_ahead() skips quickly to the line that possibly
a26345b6 » gitster
2010-01-10 grep: optimize built-in grep by skipping lines that do not hit
1056 * has the next hit; don't call it if we need to do
1057 * something more than just skipping the current line
1058 * in response to an unmatch for the current line. E.g.
1059 * inside a post-context window, we will show the current
1060 * line as a context around the previous hit when it
1061 * doesn't hit.
1062 */
1063 if (try_lookahead
1064 && !(last_hit
ba8ea749 » René Scharfe
2011-08-01 grep: add option to show whole function as context
1065 && (show_function ||
1066 lno <= last_hit + opt->post_context))
a26345b6 » gitster
2010-01-10 grep: optimize built-in grep by skipping lines that do not hit
1067 && look_ahead(opt, &left, &lno, &bol))
1068 break;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1069 eol = end_of_line(bol, &left);
1070 ch = *eol;
1071 *eol = 0;
1072
480c1ca6 » Junio C Hamano
2006-09-20 Update grep internal for grepping only in head/body
1073 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1074 ctx = GREP_CONTEXT_BODY;
1075
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
1076 hit = match_line(opt, bol, eol, ctx, collect_hits);
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1077 *eol = ch;
1078
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
1079 if (collect_hits)
1080 goto next_line;
1081
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1082 /* "grep -v -e foo -e bla" should list lines
1083 * that do not have either, so inversion should
1084 * be done outside.
1085 */
1086 if (opt->invert)
1087 hit = !hit;
1088 if (opt->unmatch_name_only) {
1089 if (hit)
1090 return 0;
1091 goto next_line;
1092 }
1093 if (hit) {
1094 count++;
1095 if (opt->status_only)
1096 return 1;
321ffcc0 » René Scharfe
2010-05-22 grep: --name-only over binary
1097 if (opt->name_only) {
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1098 show_name(opt, gs->name);
321ffcc0 » René Scharfe
2010-05-22 grep: --name-only over binary
1099 return 1;
1100 }
c30c10cf » René Scharfe
2010-05-22 grep: --count over binary
1101 if (opt->count)
1102 goto next_line;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1103 if (binary_match_only) {
5b594f45 » Fredrik Kuivinen
2010-01-25 Threaded grep
1104 opt->output(opt, "Binary file ", 12);
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1105 output_color(opt, gs->name, strlen(gs->name),
55f638bd » MarkLodato
2010-03-07 grep: Colorize filename, line number, and separator
1106 opt->color_filename);
5b594f45 » Fredrik Kuivinen
2010-01-25 Threaded grep
1107 opt->output(opt, " matches\n", 9);
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1108 return 1;
1109 }
1110 /* Hit at this line. If we haven't shown the
1111 * pre-context lines, we would need to show them.
1112 */
ba8ea749 » René Scharfe
2011-08-01 grep: add option to show whole function as context
1113 if (opt->pre_context || opt->funcbody)
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1114 show_pre_context(opt, gs, bol, eol, lno);
2944e4e6 » René Scharfe
2009-07-02 grep: add option -p/--show-function
1115 else if (opt->funcname)
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1116 show_funcname_line(opt, gs, bol, lno);
1117 show_line(opt, bol, eol, gs->name, lno, ':');
5dd06d38 » René Scharfe
2009-07-02 grep: move context hunk mark handling into show_line()
1118 last_hit = lno;
ba8ea749 » René Scharfe
2011-08-01 grep: add option to show whole function as context
1119 if (opt->funcbody)
1120 show_function = 1;
1121 goto next_line;
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1122 }
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1123 if (show_function && match_funcname(opt, gs, bol, eol))
ba8ea749 » René Scharfe
2011-08-01 grep: add option to show whole function as context
1124 show_function = 0;
1125 if (show_function ||
1126 (last_hit && lno <= last_hit + opt->post_context)) {
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1127 /* If the last hit is within the post context,
1128 * we need to show this line.
1129 */
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1130 show_line(opt, bol, eol, gs->name, lno, '-');
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1131 }
1132
1133 next_line:
1134 bol = eol + 1;
1135 if (!left)
1136 break;
1137 left--;
1138 lno++;
1139 }
1140
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
1141 if (collect_hits)
1142 return 0;
b48fb5b6 » Junio C Hamano
2006-09-27 grep: free expressions and patterns when done.
1143
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1144 if (opt->status_only)
1145 return 0;
1146 if (opt->unmatch_name_only) {
1147 /* We did not see any hit, so we want to show this */
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1148 show_name(opt, gs->name);
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1149 return 1;
1150 }
1151
60ecac98 » René Scharfe
2009-07-02 grep -p: support user defined regular expressions
1152 xdiff_clear_find_func(&xecfg);
1153 opt->priv = NULL;
1154
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1155 /* NEEDSWORK:
1156 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1157 * which feels mostly useless but sometimes useful. Maybe
1158 * make it another option? For now suppress them.
1159 */
5b594f45 » Fredrik Kuivinen
2010-01-25 Threaded grep
1160 if (opt->count && count) {
1161 char buf[32];
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1162 output_color(opt, gs->name, strlen(gs->name), opt->color_filename);
55f638bd » MarkLodato
2010-03-07 grep: Colorize filename, line number, and separator
1163 output_sep(opt, ':');
1164 snprintf(buf, sizeof(buf), "%u\n", count);
5b594f45 » Fredrik Kuivinen
2010-01-25 Threaded grep
1165 opt->output(opt, buf, strlen(buf));
c30c10cf » René Scharfe
2010-05-22 grep: --count over binary
1166 return 1;
5b594f45 » Fredrik Kuivinen
2010-01-25 Threaded grep
1167 }
83b5d2f5 » Junio C Hamano
2006-09-17 builtin-grep: make pieces of it available as library.
1168 return !!last_hit;
1169 }
1170
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
1171 static void clr_hit_marker(struct grep_expr *x)
1172 {
1173 /* All-hit markers are meaningful only at the very top level
1174 * OR node.
1175 */
1176 while (1) {
1177 x->hit = 0;
1178 if (x->node != GREP_NODE_OR)
1179 return;
1180 x->u.binary.left->hit = 0;
1181 x = x->u.binary.right;
1182 }
1183 }
1184
1185 static int chk_hit_marker(struct grep_expr *x)
1186 {
1187 /* Top level nodes have hit markers. See if they all are hits */
1188 while (1) {
1189 if (x->node != GREP_NODE_OR)
1190 return x->hit;
1191 if (!x->u.binary.left->hit)
1192 return 0;
1193 x = x->u.binary.right;
1194 }
1195 }
1196
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1197 int grep_source(struct grep_opt *opt, struct grep_source *gs)
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
1198 {
1199 /*
1200 * we do not have to do the two-pass grep when we do not check
1201 * buffer-wide "all-match".
1202 */
1203 if (!opt->all_match)
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1204 return grep_source_1(opt, gs, 0);
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
1205
1206 /* Otherwise the toplevel "or" terms hit a bit differently.
1207 * We first clear hit markers from them.
1208 */
1209 clr_hit_marker(opt->pattern_expression);
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1210 grep_source_1(opt, gs, 1);
0ab7befa » Junio C Hamano
2006-09-27 grep --all-match
1211
1212 if (!chk_hit_marker(opt->pattern_expression))
1213 return 0;
1214
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1215 return grep_source_1(opt, gs, 0);
1216 }
1217
c876d6da »
2012-02-02 grep: drop grep_buffer's "name" parameter
1218 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size)
e1327023 »
2012-02-02 grep: refactor the concept of "grep source" into an object
1219 {
1220 struct grep_source gs;
1221 int r;
1222
c876d6da »
2012-02-02 grep: drop grep_buffer's "name" parameter
1223 grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL);