]>
Commit | Line | Data |
---|---|---|
83b5d2f5 | 1 | #include "cache.h" |
b2141fc1 | 2 | #include "config.h" |
83b5d2f5 | 3 | #include "grep.h" |
cbd53a21 | 4 | #include "object-store.h" |
60ecac98 | 5 | #include "userdiff.h" |
6bfce93e | 6 | #include "xdiff-interface.h" |
335ec3bf JK |
7 | #include "diff.h" |
8 | #include "diffcore.h" | |
5c1ebcca | 9 | #include "commit.h" |
793dc676 | 10 | #include "quote.h" |
3ac68a93 | 11 | #include "help.h" |
83b5d2f5 | 12 | |
07a7d656 | 13 | static int grep_source_load(struct grep_source *gs); |
acd00ea0 NTND |
14 | static int grep_source_is_binary(struct grep_source *gs, |
15 | struct index_state *istate); | |
07a7d656 | 16 | |
96313423 MÅ |
17 | static void std_output(struct grep_opt *opt, const void *buf, size_t size) |
18 | { | |
19 | fwrite(buf, size, 1, stdout); | |
20 | } | |
21 | ||
22 | static struct grep_opt grep_defaults = { | |
23 | .relative = 1, | |
24 | .pathname = 1, | |
25 | .max_depth = -1, | |
26 | .pattern_type_option = GREP_PATTERN_TYPE_UNSPECIFIED, | |
27 | .colors = { | |
28 | [GREP_COLOR_CONTEXT] = "", | |
29 | [GREP_COLOR_FILENAME] = "", | |
30 | [GREP_COLOR_FUNCTION] = "", | |
31 | [GREP_COLOR_LINENO] = "", | |
32 | [GREP_COLOR_COLUMNNO] = "", | |
33 | [GREP_COLOR_MATCH_CONTEXT] = GIT_COLOR_BOLD_RED, | |
34 | [GREP_COLOR_MATCH_SELECTED] = GIT_COLOR_BOLD_RED, | |
35 | [GREP_COLOR_SELECTED] = "", | |
36 | [GREP_COLOR_SEP] = GIT_COLOR_CYAN, | |
37 | }, | |
38 | .only_matching = 0, | |
39 | .color = -1, | |
40 | .output = std_output, | |
41 | }; | |
7687a054 | 42 | |
513f2b0b CMAB |
43 | #ifdef USE_LIBPCRE2 |
44 | static pcre2_general_context *pcre2_global_context; | |
45 | ||
46 | static void *pcre2_malloc(PCRE2_SIZE size, MAYBE_UNUSED void *memory_data) | |
47 | { | |
48 | return malloc(size); | |
49 | } | |
50 | ||
51 | static void pcre2_free(void *pointer, MAYBE_UNUSED void *memory_data) | |
52 | { | |
867fc7f3 | 53 | free(pointer); |
513f2b0b CMAB |
54 | } |
55 | #endif | |
56 | ||
fa151dc5 NTND |
57 | static const char *color_grep_slots[] = { |
58 | [GREP_COLOR_CONTEXT] = "context", | |
59 | [GREP_COLOR_FILENAME] = "filename", | |
60 | [GREP_COLOR_FUNCTION] = "function", | |
61 | [GREP_COLOR_LINENO] = "lineNumber", | |
d036d667 | 62 | [GREP_COLOR_COLUMNNO] = "column", |
fa151dc5 NTND |
63 | [GREP_COLOR_MATCH_CONTEXT] = "matchContext", |
64 | [GREP_COLOR_MATCH_SELECTED] = "matchSelected", | |
65 | [GREP_COLOR_SELECTED] = "selected", | |
66 | [GREP_COLOR_SEP] = "separator", | |
67 | }; | |
68 | ||
7687a054 JH |
69 | static int parse_pattern_type_arg(const char *opt, const char *arg) |
70 | { | |
71 | if (!strcmp(arg, "default")) | |
72 | return GREP_PATTERN_TYPE_UNSPECIFIED; | |
73 | else if (!strcmp(arg, "basic")) | |
74 | return GREP_PATTERN_TYPE_BRE; | |
75 | else if (!strcmp(arg, "extended")) | |
76 | return GREP_PATTERN_TYPE_ERE; | |
77 | else if (!strcmp(arg, "fixed")) | |
78 | return GREP_PATTERN_TYPE_FIXED; | |
79 | else if (!strcmp(arg, "perl")) | |
80 | return GREP_PATTERN_TYPE_PCRE; | |
81 | die("bad %s argument: %s", opt, arg); | |
82 | } | |
83 | ||
3ac68a93 NTND |
84 | define_list_config_array_extra(color_grep_slots, {"match"}); |
85 | ||
7687a054 JH |
86 | /* |
87 | * Read the configuration file once and store it in | |
88 | * the grep_defaults template. | |
89 | */ | |
90 | int grep_config(const char *var, const char *value, void *cb) | |
91 | { | |
92 | struct grep_opt *opt = &grep_defaults; | |
fa151dc5 | 93 | const char *slot; |
7687a054 JH |
94 | |
95 | if (userdiff_config(var, value) < 0) | |
96 | return -1; | |
97 | ||
6ba9bb76 MÅ |
98 | /* |
99 | * The instance of grep_opt that we set up here is copied by | |
100 | * grep_init() to be used by each individual invocation. | |
101 | * When populating a new field of this structure here, be | |
102 | * sure to think about ownership -- e.g., you might need to | |
103 | * override the shallow copy in grep_init() with a deep copy. | |
104 | */ | |
105 | ||
7687a054 | 106 | if (!strcmp(var, "grep.extendedregexp")) { |
c7e38551 | 107 | opt->extended_regexp_option = git_config_bool(var, value); |
7687a054 JH |
108 | return 0; |
109 | } | |
110 | ||
111 | if (!strcmp(var, "grep.patterntype")) { | |
112 | opt->pattern_type_option = parse_pattern_type_arg(var, value); | |
113 | return 0; | |
114 | } | |
115 | ||
116 | if (!strcmp(var, "grep.linenumber")) { | |
117 | opt->linenum = git_config_bool(var, value); | |
118 | return 0; | |
119 | } | |
6653fec3 TB |
120 | if (!strcmp(var, "grep.column")) { |
121 | opt->columnnum = git_config_bool(var, value); | |
122 | return 0; | |
123 | } | |
7687a054 | 124 | |
6453f7b3 AS |
125 | if (!strcmp(var, "grep.fullname")) { |
126 | opt->relative = !git_config_bool(var, value); | |
127 | return 0; | |
128 | } | |
129 | ||
7687a054 JH |
130 | if (!strcmp(var, "color.grep")) |
131 | opt->color = git_config_colorbool(var, value); | |
fa151dc5 NTND |
132 | if (!strcmp(var, "color.grep.match")) { |
133 | if (grep_config("color.grep.matchcontext", value, cb) < 0) | |
134 | return -1; | |
135 | if (grep_config("color.grep.matchselected", value, cb) < 0) | |
136 | return -1; | |
137 | } else if (skip_prefix(var, "color.grep.", &slot)) { | |
138 | int i = LOOKUP_CONFIG(color_grep_slots, slot); | |
139 | char *color; | |
140 | ||
141 | if (i < 0) | |
142 | return -1; | |
143 | color = opt->colors[i]; | |
7687a054 JH |
144 | if (!value) |
145 | return config_error_nonbool(var); | |
f6c5a296 | 146 | return color_parse(value, color); |
7687a054 JH |
147 | } |
148 | return 0; | |
149 | } | |
150 | ||
151 | /* | |
152 | * Initialize one instance of grep_opt and copy the | |
153 | * default values from the template we read the configuration | |
154 | * information in an earlier call to git_config(grep_config). | |
57d46604 CMAB |
155 | * |
156 | * If using PCRE, make sure that the library is configured | |
157 | * to use the same allocator as Git (e.g. nedmalloc on Windows). | |
513f2b0b CMAB |
158 | * |
159 | * Any allocated memory needs to be released in grep_destroy(). | |
7687a054 | 160 | */ |
38bbc2ea | 161 | void grep_init(struct grep_opt *opt, struct repository *repo, const char *prefix) |
7687a054 | 162 | { |
513f2b0b CMAB |
163 | #if defined(USE_LIBPCRE2) |
164 | if (!pcre2_global_context) | |
165 | pcre2_global_context = pcre2_general_context_create( | |
166 | pcre2_malloc, pcre2_free, NULL); | |
167 | #endif | |
168 | ||
6ba9bb76 MÅ |
169 | *opt = grep_defaults; |
170 | ||
38bbc2ea | 171 | opt->repo = repo; |
7687a054 JH |
172 | opt->prefix = prefix; |
173 | opt->prefix_length = (prefix && *prefix) ? strlen(prefix) : 0; | |
174 | opt->pattern_tail = &opt->pattern_list; | |
175 | opt->header_tail = &opt->header_list; | |
7687a054 | 176 | } |
07a7d656 | 177 | |
513f2b0b CMAB |
178 | void grep_destroy(void) |
179 | { | |
180 | #ifdef USE_LIBPCRE2 | |
181 | pcre2_general_context_free(pcre2_global_context); | |
182 | #endif | |
183 | } | |
184 | ||
8465541e | 185 | static void grep_set_pattern_type_option(enum grep_pattern_type pattern_type, struct grep_opt *opt) |
c5c31d33 | 186 | { |
07a3d411 ÆAB |
187 | /* |
188 | * When committing to the pattern type by setting the relevant | |
189 | * fields in grep_opt it's generally not necessary to zero out | |
190 | * the fields we're not choosing, since they won't have been | |
191 | * set by anything. The extended_regexp_option field is the | |
192 | * only exception to this. | |
193 | * | |
194 | * This is because in the process of parsing grep.patternType | |
195 | * & grep.extendedRegexp we set opt->pattern_type_option and | |
196 | * opt->extended_regexp_option, respectively. We then | |
197 | * internally use opt->extended_regexp_option to see if we're | |
198 | * compiling an ERE. It must be unset if that's not actually | |
199 | * the case. | |
200 | */ | |
201 | if (pattern_type != GREP_PATTERN_TYPE_ERE && | |
202 | opt->extended_regexp_option) | |
203 | opt->extended_regexp_option = 0; | |
204 | ||
c5c31d33 JH |
205 | switch (pattern_type) { |
206 | case GREP_PATTERN_TYPE_UNSPECIFIED: | |
207 | /* fall through */ | |
208 | ||
209 | case GREP_PATTERN_TYPE_BRE: | |
c5c31d33 JH |
210 | break; |
211 | ||
212 | case GREP_PATTERN_TYPE_ERE: | |
07a3d411 | 213 | opt->extended_regexp_option = 1; |
c5c31d33 JH |
214 | break; |
215 | ||
216 | case GREP_PATTERN_TYPE_FIXED: | |
217 | opt->fixed = 1; | |
c5c31d33 JH |
218 | break; |
219 | ||
220 | case GREP_PATTERN_TYPE_PCRE: | |
94da9193 | 221 | opt->pcre2 = 1; |
c5c31d33 JH |
222 | break; |
223 | } | |
224 | } | |
225 | ||
8465541e JH |
226 | void grep_commit_pattern_type(enum grep_pattern_type pattern_type, struct grep_opt *opt) |
227 | { | |
228 | if (pattern_type != GREP_PATTERN_TYPE_UNSPECIFIED) | |
229 | grep_set_pattern_type_option(pattern_type, opt); | |
230 | else if (opt->pattern_type_option != GREP_PATTERN_TYPE_UNSPECIFIED) | |
231 | grep_set_pattern_type_option(opt->pattern_type_option, opt); | |
232 | else if (opt->extended_regexp_option) | |
07a3d411 ÆAB |
233 | /* |
234 | * This branch *must* happen after setting from the | |
235 | * opt->pattern_type_option above, we don't want | |
236 | * grep.extendedRegexp to override grep.patternType! | |
237 | */ | |
8465541e JH |
238 | grep_set_pattern_type_option(GREP_PATTERN_TYPE_ERE, opt); |
239 | } | |
240 | ||
fc456751 RS |
241 | static struct grep_pat *create_grep_pat(const char *pat, size_t patlen, |
242 | const char *origin, int no, | |
243 | enum grep_pat_token t, | |
244 | enum grep_header_field field) | |
a4d7d2c6 JH |
245 | { |
246 | struct grep_pat *p = xcalloc(1, sizeof(*p)); | |
526a858a | 247 | p->pattern = xmemdupz(pat, patlen); |
fc456751 RS |
248 | p->patternlen = patlen; |
249 | p->origin = origin; | |
250 | p->no = no; | |
251 | p->token = t; | |
a4d7d2c6 | 252 | p->field = field; |
fc456751 RS |
253 | return p; |
254 | } | |
255 | ||
2b3873ff RS |
256 | static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p) |
257 | { | |
258 | **tail = p; | |
259 | *tail = &p->next; | |
a4d7d2c6 | 260 | p->next = NULL; |
526a858a RS |
261 | |
262 | switch (p->token) { | |
263 | case GREP_PATTERN: /* atom */ | |
264 | case GREP_PATTERN_HEAD: | |
265 | case GREP_PATTERN_BODY: | |
266 | for (;;) { | |
267 | struct grep_pat *new_pat; | |
268 | size_t len = 0; | |
269 | char *cp = p->pattern + p->patternlen, *nl = NULL; | |
270 | while (++len <= p->patternlen) { | |
271 | if (*(--cp) == '\n') { | |
272 | nl = cp; | |
273 | break; | |
274 | } | |
275 | } | |
276 | if (!nl) | |
277 | break; | |
278 | new_pat = create_grep_pat(nl + 1, len - 1, p->origin, | |
279 | p->no, p->token, p->field); | |
280 | new_pat->next = p->next; | |
281 | if (!p->next) | |
282 | *tail = &new_pat->next; | |
283 | p->next = new_pat; | |
284 | *nl = '\0'; | |
285 | p->patternlen -= len; | |
286 | } | |
287 | break; | |
288 | default: | |
289 | break; | |
290 | } | |
2b3873ff RS |
291 | } |
292 | ||
fc456751 RS |
293 | void append_header_grep_pattern(struct grep_opt *opt, |
294 | enum grep_header_field field, const char *pat) | |
295 | { | |
296 | struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0, | |
297 | GREP_PATTERN_HEAD, field); | |
baa6378f JH |
298 | if (field == GREP_HEADER_REFLOG) |
299 | opt->use_reflog_filter = 1; | |
2b3873ff | 300 | do_append_grep_pat(&opt->header_tail, p); |
a4d7d2c6 JH |
301 | } |
302 | ||
83b5d2f5 JH |
303 | void append_grep_pattern(struct grep_opt *opt, const char *pat, |
304 | const char *origin, int no, enum grep_pat_token t) | |
ed40a095 RS |
305 | { |
306 | append_grep_pat(opt, pat, strlen(pat), origin, no, t); | |
307 | } | |
308 | ||
309 | void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, | |
310 | const char *origin, int no, enum grep_pat_token t) | |
83b5d2f5 | 311 | { |
fc456751 | 312 | struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0); |
2b3873ff | 313 | do_append_grep_pat(&opt->pattern_tail, p); |
83b5d2f5 JH |
314 | } |
315 | ||
5b594f45 FK |
316 | struct grep_opt *grep_opt_dup(const struct grep_opt *opt) |
317 | { | |
318 | struct grep_pat *pat; | |
319 | struct grep_opt *ret = xmalloc(sizeof(struct grep_opt)); | |
320 | *ret = *opt; | |
321 | ||
322 | ret->pattern_list = NULL; | |
323 | ret->pattern_tail = &ret->pattern_list; | |
324 | ||
325 | for(pat = opt->pattern_list; pat != NULL; pat = pat->next) | |
326 | { | |
327 | if(pat->token == GREP_PATTERN_HEAD) | |
328 | append_header_grep_pattern(ret, pat->field, | |
329 | pat->pattern); | |
330 | else | |
ed40a095 RS |
331 | append_grep_pat(ret, pat->pattern, pat->patternlen, |
332 | pat->origin, pat->no, pat->token); | |
5b594f45 FK |
333 | } |
334 | ||
335 | return ret; | |
336 | } | |
337 | ||
a30c148a MK |
338 | static NORETURN void compile_regexp_failed(const struct grep_pat *p, |
339 | const char *error) | |
340 | { | |
341 | char where[1024]; | |
342 | ||
343 | if (p->no) | |
19bdd3e7 | 344 | xsnprintf(where, sizeof(where), "In '%s' at %d, ", p->origin, p->no); |
a30c148a | 345 | else if (p->origin) |
19bdd3e7 | 346 | xsnprintf(where, sizeof(where), "%s, ", p->origin); |
a30c148a MK |
347 | else |
348 | where[0] = 0; | |
349 | ||
350 | die("%s'%s': %s", where, p->pattern, error); | |
351 | } | |
352 | ||
543f1c0c ÆAB |
353 | static int is_fixed(const char *s, size_t len) |
354 | { | |
355 | size_t i; | |
356 | ||
357 | for (i = 0; i < len; i++) { | |
358 | if (is_regex_special(s[i])) | |
359 | return 0; | |
360 | } | |
361 | ||
362 | return 1; | |
363 | } | |
364 | ||
94da9193 ÆAB |
365 | #ifdef USE_LIBPCRE2 |
366 | static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt) | |
367 | { | |
368 | int error; | |
369 | PCRE2_UCHAR errbuf[256]; | |
370 | PCRE2_SIZE erroffset; | |
371 | int options = PCRE2_MULTILINE; | |
94da9193 | 372 | int jitret; |
a25b9085 ÆAB |
373 | int patinforet; |
374 | size_t jitsizearg; | |
94da9193 ÆAB |
375 | |
376 | assert(opt->pcre2); | |
377 | ||
378 | p->pcre2_compile_context = NULL; | |
379 | ||
513f2b0b | 380 | /* pcre2_global_context is initialized in append_grep_pattern */ |
94da9193 | 381 | if (opt->ignore_case) { |
44570188 | 382 | if (!opt->ignore_locale && has_non_ascii(p->pattern)) { |
513f2b0b CMAB |
383 | if (!pcre2_global_context) |
384 | BUG("pcre2_global_context uninitialized"); | |
10da030a | 385 | p->pcre2_tables = pcre2_maketables(pcre2_global_context); |
94da9193 | 386 | p->pcre2_compile_context = pcre2_compile_context_create(NULL); |
10da030a CMAB |
387 | pcre2_set_character_tables(p->pcre2_compile_context, |
388 | p->pcre2_tables); | |
94da9193 ÆAB |
389 | } |
390 | options |= PCRE2_CASELESS; | |
391 | } | |
870eea81 ÆAB |
392 | if (!opt->ignore_locale && is_utf8_locale() && has_non_ascii(p->pattern) && |
393 | !(!opt->ignore_case && (p->fixed || p->is_fixed))) | |
94da9193 ÆAB |
394 | options |= PCRE2_UTF; |
395 | ||
396 | p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern, | |
397 | p->patternlen, options, &error, &erroffset, | |
398 | p->pcre2_compile_context); | |
399 | ||
400 | if (p->pcre2_pattern) { | |
401 | p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, NULL); | |
402 | if (!p->pcre2_match_data) | |
403 | die("Couldn't allocate PCRE2 match data"); | |
404 | } else { | |
405 | pcre2_get_error_message(error, errbuf, sizeof(errbuf)); | |
406 | compile_regexp_failed(p, (const char *)&errbuf); | |
407 | } | |
408 | ||
409 | pcre2_config(PCRE2_CONFIG_JIT, &p->pcre2_jit_on); | |
c581e4a7 BB |
410 | if (opt->debug) |
411 | fprintf(stderr, "pcre2_jit_on=%d\n", p->pcre2_jit_on); | |
04bef50c | 412 | if (p->pcre2_jit_on) { |
94da9193 ÆAB |
413 | jitret = pcre2_jit_compile(p->pcre2_pattern, PCRE2_JIT_COMPLETE); |
414 | if (jitret) | |
415 | die("Couldn't JIT the PCRE2 pattern '%s', got '%d'\n", p->pattern, jitret); | |
a25b9085 ÆAB |
416 | |
417 | /* | |
418 | * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just | |
419 | * tells us whether the library itself supports JIT, | |
420 | * but to see whether we're going to be actually using | |
421 | * JIT we need to extract PCRE2_INFO_JITSIZE from the | |
422 | * pattern *after* we do pcre2_jit_compile() above. | |
423 | * | |
424 | * This is because if the pattern contains the | |
425 | * (*NO_JIT) verb (see pcre2syntax(3)) | |
426 | * pcre2_jit_compile() will exit early with 0. If we | |
427 | * then proceed to call pcre2_jit_match() further down | |
428 | * the line instead of pcre2_match() we'll either | |
429 | * segfault (pre PCRE 10.31) or run into a fatal error | |
430 | * (post PCRE2 10.31) | |
431 | */ | |
432 | patinforet = pcre2_pattern_info(p->pcre2_pattern, PCRE2_INFO_JITSIZE, &jitsizearg); | |
433 | if (patinforet) | |
434 | BUG("pcre2_pattern_info() failed: %d", patinforet); | |
435 | if (jitsizearg == 0) { | |
436 | p->pcre2_jit_on = 0; | |
c581e4a7 BB |
437 | if (opt->debug) |
438 | fprintf(stderr, "pcre2_jit_on=%d: (*NO_JIT) in regex\n", | |
439 | p->pcre2_jit_on); | |
a25b9085 ÆAB |
440 | return; |
441 | } | |
94da9193 ÆAB |
442 | } |
443 | } | |
444 | ||
445 | static int pcre2match(struct grep_pat *p, const char *line, const char *eol, | |
446 | regmatch_t *match, int eflags) | |
447 | { | |
448 | int ret, flags = 0; | |
449 | PCRE2_SIZE *ovector; | |
450 | PCRE2_UCHAR errbuf[256]; | |
451 | ||
452 | if (eflags & REG_NOTBOL) | |
453 | flags |= PCRE2_NOTBOL; | |
454 | ||
455 | if (p->pcre2_jit_on) | |
456 | ret = pcre2_jit_match(p->pcre2_pattern, (unsigned char *)line, | |
457 | eol - line, 0, flags, p->pcre2_match_data, | |
458 | NULL); | |
459 | else | |
460 | ret = pcre2_match(p->pcre2_pattern, (unsigned char *)line, | |
461 | eol - line, 0, flags, p->pcre2_match_data, | |
462 | NULL); | |
463 | ||
464 | if (ret < 0 && ret != PCRE2_ERROR_NOMATCH) { | |
465 | pcre2_get_error_message(ret, errbuf, sizeof(errbuf)); | |
466 | die("%s failed with error code %d: %s", | |
467 | (p->pcre2_jit_on ? "pcre2_jit_match" : "pcre2_match"), ret, | |
468 | errbuf); | |
469 | } | |
470 | if (ret > 0) { | |
471 | ovector = pcre2_get_ovector_pointer(p->pcre2_match_data); | |
472 | ret = 0; | |
473 | match->rm_so = (int)ovector[0]; | |
474 | match->rm_eo = (int)ovector[1]; | |
475 | } | |
476 | ||
477 | return ret; | |
478 | } | |
479 | ||
480 | static void free_pcre2_pattern(struct grep_pat *p) | |
481 | { | |
482 | pcre2_compile_context_free(p->pcre2_compile_context); | |
483 | pcre2_code_free(p->pcre2_pattern); | |
484 | pcre2_match_data_free(p->pcre2_match_data); | |
10da030a | 485 | free((void *)p->pcre2_tables); |
94da9193 ÆAB |
486 | } |
487 | #else /* !USE_LIBPCRE2 */ | |
488 | static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt) | |
489 | { | |
94da9193 ÆAB |
490 | die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE"); |
491 | } | |
492 | ||
493 | static int pcre2match(struct grep_pat *p, const char *line, const char *eol, | |
494 | regmatch_t *match, int eflags) | |
495 | { | |
496 | return 1; | |
497 | } | |
498 | ||
499 | static void free_pcre2_pattern(struct grep_pat *p) | |
500 | { | |
501 | } | |
94da9193 | 502 | |
793dc676 NTND |
503 | static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt) |
504 | { | |
505 | struct strbuf sb = STRBUF_INIT; | |
506 | int err; | |
1ceababc | 507 | int regflags = 0; |
793dc676 NTND |
508 | |
509 | basic_regex_quote_buf(&sb, p->pattern); | |
793dc676 NTND |
510 | if (opt->ignore_case) |
511 | regflags |= REG_ICASE; | |
512 | err = regcomp(&p->regexp, sb.buf, regflags); | |
513 | if (opt->debug) | |
514 | fprintf(stderr, "fixed %s\n", sb.buf); | |
515 | strbuf_release(&sb); | |
516 | if (err) { | |
517 | char errbuf[1024]; | |
518 | regerror(err, &p->regexp, errbuf, sizeof(errbuf)); | |
793dc676 NTND |
519 | compile_regexp_failed(p, errbuf); |
520 | } | |
521 | } | |
b65abcaf | 522 | #endif /* !USE_LIBPCRE2 */ |
793dc676 | 523 | |
83b5d2f5 JH |
524 | static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) |
525 | { | |
c822255c | 526 | int err; |
07a3d411 | 527 | int regflags = REG_NEWLINE; |
c822255c | 528 | |
d7eb527d | 529 | p->word_regexp = opt->word_regexp; |
5183bf67 | 530 | p->ignore_case = opt->ignore_case; |
48de2a76 | 531 | p->fixed = opt->fixed; |
d7eb527d | 532 | |
45d1f37c ÆAB |
533 | if (memchr(p->pattern, 0, p->patternlen) && !opt->pcre2) |
534 | die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2")); | |
535 | ||
09872f64 | 536 | p->is_fixed = is_fixed(p->pattern, p->patternlen); |
8a599983 ÆAB |
537 | #ifdef USE_LIBPCRE2 |
538 | if (!p->fixed && !p->is_fixed) { | |
539 | const char *no_jit = "(*NO_JIT)"; | |
540 | const int no_jit_len = strlen(no_jit); | |
541 | if (starts_with(p->pattern, no_jit) && | |
542 | is_fixed(p->pattern + no_jit_len, | |
543 | p->patternlen - no_jit_len)) | |
544 | p->is_fixed = 1; | |
545 | } | |
546 | #endif | |
09872f64 | 547 | if (p->fixed || p->is_fixed) { |
b65abcaf ÆAB |
548 | #ifdef USE_LIBPCRE2 |
549 | opt->pcre2 = 1; | |
09872f64 | 550 | if (p->is_fixed) { |
b65abcaf ÆAB |
551 | compile_pcre2_pattern(p, opt); |
552 | } else { | |
553 | /* | |
554 | * E.g. t7811-grep-open.sh relies on the | |
555 | * pattern being restored. | |
556 | */ | |
557 | char *old_pattern = p->pattern; | |
558 | size_t old_patternlen = p->patternlen; | |
559 | struct strbuf sb = STRBUF_INIT; | |
560 | ||
561 | /* | |
562 | * There is the PCRE2_LITERAL flag, but it's | |
563 | * only in PCRE v2 10.30 and later. Needing to | |
564 | * ifdef our way around that and dealing with | |
565 | * it + PCRE2_MULTILINE being an error is more | |
566 | * complex than just quoting this ourselves. | |
567 | */ | |
568 | strbuf_add(&sb, "\\Q", 2); | |
569 | strbuf_add(&sb, p->pattern, p->patternlen); | |
570 | strbuf_add(&sb, "\\E", 2); | |
571 | ||
572 | p->pattern = sb.buf; | |
573 | p->patternlen = sb.len; | |
574 | compile_pcre2_pattern(p, opt); | |
575 | p->pattern = old_pattern; | |
576 | p->patternlen = old_patternlen; | |
577 | strbuf_release(&sb); | |
578 | } | |
579 | #else /* !USE_LIBPCRE2 */ | |
793dc676 | 580 | compile_fixed_regexp(p, opt); |
b65abcaf | 581 | #endif /* !USE_LIBPCRE2 */ |
793dc676 | 582 | return; |
9eceddee | 583 | } |
c822255c | 584 | |
94da9193 ÆAB |
585 | if (opt->pcre2) { |
586 | compile_pcre2_pattern(p, opt); | |
587 | return; | |
588 | } | |
589 | ||
07a3d411 ÆAB |
590 | if (p->ignore_case) |
591 | regflags |= REG_ICASE; | |
592 | if (opt->extended_regexp_option) | |
593 | regflags |= REG_EXTENDED; | |
594 | err = regcomp(&p->regexp, p->pattern, regflags); | |
83b5d2f5 JH |
595 | if (err) { |
596 | char errbuf[1024]; | |
83b5d2f5 | 597 | regerror(err, &p->regexp, errbuf, 1024); |
a30c148a | 598 | compile_regexp_failed(p, errbuf); |
83b5d2f5 JH |
599 | } |
600 | } | |
601 | ||
0ab7befa | 602 | static struct grep_expr *compile_pattern_or(struct grep_pat **); |
83b5d2f5 JH |
603 | static struct grep_expr *compile_pattern_atom(struct grep_pat **list) |
604 | { | |
605 | struct grep_pat *p; | |
606 | struct grep_expr *x; | |
607 | ||
608 | p = *list; | |
c922b01f LT |
609 | if (!p) |
610 | return NULL; | |
83b5d2f5 JH |
611 | switch (p->token) { |
612 | case GREP_PATTERN: /* atom */ | |
480c1ca6 JH |
613 | case GREP_PATTERN_HEAD: |
614 | case GREP_PATTERN_BODY: | |
83b5d2f5 JH |
615 | x = xcalloc(1, sizeof (struct grep_expr)); |
616 | x->node = GREP_NODE_ATOM; | |
617 | x->u.atom = p; | |
618 | *list = p->next; | |
619 | return x; | |
620 | case GREP_OPEN_PAREN: | |
621 | *list = p->next; | |
0ab7befa | 622 | x = compile_pattern_or(list); |
83b5d2f5 JH |
623 | if (!*list || (*list)->token != GREP_CLOSE_PAREN) |
624 | die("unmatched parenthesis"); | |
625 | *list = (*list)->next; | |
626 | return x; | |
627 | default: | |
628 | return NULL; | |
629 | } | |
630 | } | |
631 | ||
632 | static struct grep_expr *compile_pattern_not(struct grep_pat **list) | |
633 | { | |
634 | struct grep_pat *p; | |
635 | struct grep_expr *x; | |
636 | ||
637 | p = *list; | |
c922b01f LT |
638 | if (!p) |
639 | return NULL; | |
83b5d2f5 JH |
640 | switch (p->token) { |
641 | case GREP_NOT: | |
642 | if (!p->next) | |
643 | die("--not not followed by pattern expression"); | |
644 | *list = p->next; | |
645 | x = xcalloc(1, sizeof (struct grep_expr)); | |
646 | x->node = GREP_NODE_NOT; | |
647 | x->u.unary = compile_pattern_not(list); | |
648 | if (!x->u.unary) | |
649 | die("--not followed by non pattern expression"); | |
650 | return x; | |
651 | default: | |
652 | return compile_pattern_atom(list); | |
653 | } | |
654 | } | |
655 | ||
656 | static struct grep_expr *compile_pattern_and(struct grep_pat **list) | |
657 | { | |
658 | struct grep_pat *p; | |
659 | struct grep_expr *x, *y, *z; | |
660 | ||
661 | x = compile_pattern_not(list); | |
662 | p = *list; | |
663 | if (p && p->token == GREP_AND) { | |
664 | if (!p->next) | |
665 | die("--and not followed by pattern expression"); | |
666 | *list = p->next; | |
667 | y = compile_pattern_and(list); | |
668 | if (!y) | |
669 | die("--and not followed by pattern expression"); | |
670 | z = xcalloc(1, sizeof (struct grep_expr)); | |
671 | z->node = GREP_NODE_AND; | |
672 | z->u.binary.left = x; | |
673 | z->u.binary.right = y; | |
674 | return z; | |
675 | } | |
676 | return x; | |
677 | } | |
678 | ||
679 | static struct grep_expr *compile_pattern_or(struct grep_pat **list) | |
680 | { | |
681 | struct grep_pat *p; | |
682 | struct grep_expr *x, *y, *z; | |
683 | ||
684 | x = compile_pattern_and(list); | |
685 | p = *list; | |
686 | if (x && p && p->token != GREP_CLOSE_PAREN) { | |
687 | y = compile_pattern_or(list); | |
688 | if (!y) | |
689 | die("not a pattern expression %s", p->pattern); | |
690 | z = xcalloc(1, sizeof (struct grep_expr)); | |
691 | z->node = GREP_NODE_OR; | |
692 | z->u.binary.left = x; | |
693 | z->u.binary.right = y; | |
694 | return z; | |
695 | } | |
696 | return x; | |
697 | } | |
698 | ||
699 | static struct grep_expr *compile_pattern_expr(struct grep_pat **list) | |
700 | { | |
701 | return compile_pattern_or(list); | |
702 | } | |
703 | ||
17bf35a3 JH |
704 | static void indent(int in) |
705 | { | |
706 | while (in-- > 0) | |
707 | fputc(' ', stderr); | |
708 | } | |
709 | ||
710 | static void dump_grep_pat(struct grep_pat *p) | |
711 | { | |
712 | switch (p->token) { | |
713 | case GREP_AND: fprintf(stderr, "*and*"); break; | |
714 | case GREP_OPEN_PAREN: fprintf(stderr, "*(*"); break; | |
715 | case GREP_CLOSE_PAREN: fprintf(stderr, "*)*"); break; | |
716 | case GREP_NOT: fprintf(stderr, "*not*"); break; | |
717 | case GREP_OR: fprintf(stderr, "*or*"); break; | |
718 | ||
719 | case GREP_PATTERN: fprintf(stderr, "pattern"); break; | |
720 | case GREP_PATTERN_HEAD: fprintf(stderr, "pattern_head"); break; | |
721 | case GREP_PATTERN_BODY: fprintf(stderr, "pattern_body"); break; | |
722 | } | |
723 | ||
724 | switch (p->token) { | |
725 | default: break; | |
726 | case GREP_PATTERN_HEAD: | |
727 | fprintf(stderr, "<head %d>", p->field); break; | |
728 | case GREP_PATTERN_BODY: | |
729 | fprintf(stderr, "<body>"); break; | |
730 | } | |
731 | switch (p->token) { | |
732 | default: break; | |
733 | case GREP_PATTERN_HEAD: | |
734 | case GREP_PATTERN_BODY: | |
735 | case GREP_PATTERN: | |
736 | fprintf(stderr, "%.*s", (int)p->patternlen, p->pattern); | |
737 | break; | |
738 | } | |
739 | fputc('\n', stderr); | |
740 | } | |
741 | ||
742 | static void dump_grep_expression_1(struct grep_expr *x, int in) | |
743 | { | |
744 | indent(in); | |
745 | switch (x->node) { | |
746 | case GREP_NODE_TRUE: | |
747 | fprintf(stderr, "true\n"); | |
748 | break; | |
749 | case GREP_NODE_ATOM: | |
750 | dump_grep_pat(x->u.atom); | |
751 | break; | |
752 | case GREP_NODE_NOT: | |
753 | fprintf(stderr, "(not\n"); | |
754 | dump_grep_expression_1(x->u.unary, in+1); | |
755 | indent(in); | |
756 | fprintf(stderr, ")\n"); | |
757 | break; | |
758 | case GREP_NODE_AND: | |
759 | fprintf(stderr, "(and\n"); | |
760 | dump_grep_expression_1(x->u.binary.left, in+1); | |
761 | dump_grep_expression_1(x->u.binary.right, in+1); | |
762 | indent(in); | |
763 | fprintf(stderr, ")\n"); | |
764 | break; | |
765 | case GREP_NODE_OR: | |
766 | fprintf(stderr, "(or\n"); | |
767 | dump_grep_expression_1(x->u.binary.left, in+1); | |
768 | dump_grep_expression_1(x->u.binary.right, in+1); | |
769 | indent(in); | |
770 | fprintf(stderr, ")\n"); | |
771 | break; | |
772 | } | |
773 | } | |
774 | ||
07a7d656 | 775 | static void dump_grep_expression(struct grep_opt *opt) |
17bf35a3 JH |
776 | { |
777 | struct grep_expr *x = opt->pattern_expression; | |
778 | ||
779 | if (opt->all_match) | |
780 | fprintf(stderr, "[all-match]\n"); | |
781 | dump_grep_expression_1(x, 0); | |
782 | fflush(NULL); | |
783 | } | |
784 | ||
5aaeb733 JH |
785 | static struct grep_expr *grep_true_expr(void) |
786 | { | |
787 | struct grep_expr *z = xcalloc(1, sizeof(*z)); | |
788 | z->node = GREP_NODE_TRUE; | |
789 | return z; | |
790 | } | |
791 | ||
792 | static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right) | |
793 | { | |
794 | struct grep_expr *z = xcalloc(1, sizeof(*z)); | |
795 | z->node = GREP_NODE_OR; | |
796 | z->u.binary.left = left; | |
797 | z->u.binary.right = right; | |
798 | return z; | |
799 | } | |
800 | ||
95ce9ce2 | 801 | static struct grep_expr *prep_header_patterns(struct grep_opt *opt) |
83b5d2f5 JH |
802 | { |
803 | struct grep_pat *p; | |
95ce9ce2 | 804 | struct grep_expr *header_expr; |
5aaeb733 JH |
805 | struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]); |
806 | enum grep_header_field fld; | |
83b5d2f5 | 807 | |
95ce9ce2 JH |
808 | if (!opt->header_list) |
809 | return NULL; | |
2385f246 | 810 | |
95ce9ce2 JH |
811 | for (p = opt->header_list; p; p = p->next) { |
812 | if (p->token != GREP_PATTERN_HEAD) | |
033abf97 | 813 | BUG("a non-header pattern in grep header list."); |
3ce3ffb8 AP |
814 | if (p->field < GREP_HEADER_FIELD_MIN || |
815 | GREP_HEADER_FIELD_MAX <= p->field) | |
033abf97 | 816 | BUG("unknown header field %d", p->field); |
95ce9ce2 | 817 | compile_regexp(p, opt); |
80235ba7 | 818 | } |
5aaeb733 JH |
819 | |
820 | for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) | |
821 | header_group[fld] = NULL; | |
822 | ||
823 | for (p = opt->header_list; p; p = p->next) { | |
824 | struct grep_expr *h; | |
825 | struct grep_pat *pp = p; | |
826 | ||
827 | h = compile_pattern_atom(&pp); | |
828 | if (!h || pp != p->next) | |
033abf97 | 829 | BUG("malformed header expr"); |
5aaeb733 JH |
830 | if (!header_group[p->field]) { |
831 | header_group[p->field] = h; | |
832 | continue; | |
833 | } | |
834 | header_group[p->field] = grep_or_expr(h, header_group[p->field]); | |
835 | } | |
836 | ||
837 | header_expr = NULL; | |
838 | ||
839 | for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) { | |
840 | if (!header_group[fld]) | |
841 | continue; | |
842 | if (!header_expr) | |
843 | header_expr = grep_true_expr(); | |
844 | header_expr = grep_or_expr(header_group[fld], header_expr); | |
845 | } | |
95ce9ce2 JH |
846 | return header_expr; |
847 | } | |
848 | ||
13e4fc7e JH |
849 | static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y) |
850 | { | |
851 | struct grep_expr *z = x; | |
852 | ||
853 | while (x) { | |
854 | assert(x->node == GREP_NODE_OR); | |
855 | if (x->u.binary.right && | |
856 | x->u.binary.right->node == GREP_NODE_TRUE) { | |
857 | x->u.binary.right = y; | |
858 | break; | |
859 | } | |
860 | x = x->u.binary.right; | |
861 | } | |
862 | return z; | |
863 | } | |
864 | ||
17bf35a3 | 865 | static void compile_grep_patterns_real(struct grep_opt *opt) |
95ce9ce2 JH |
866 | { |
867 | struct grep_pat *p; | |
868 | struct grep_expr *header_expr = prep_header_patterns(opt); | |
0ab7befa | 869 | |
83b5d2f5 | 870 | for (p = opt->pattern_list; p; p = p->next) { |
480c1ca6 JH |
871 | switch (p->token) { |
872 | case GREP_PATTERN: /* atom */ | |
873 | case GREP_PATTERN_HEAD: | |
874 | case GREP_PATTERN_BODY: | |
c822255c | 875 | compile_regexp(p, opt); |
480c1ca6 JH |
876 | break; |
877 | default: | |
83b5d2f5 | 878 | opt->extended = 1; |
480c1ca6 JH |
879 | break; |
880 | } | |
83b5d2f5 JH |
881 | } |
882 | ||
80235ba7 JH |
883 | if (opt->all_match || header_expr) |
884 | opt->extended = 1; | |
17bf35a3 | 885 | else if (!opt->extended && !opt->debug) |
83b5d2f5 JH |
886 | return; |
887 | ||
83b5d2f5 | 888 | p = opt->pattern_list; |
ba150a3f MB |
889 | if (p) |
890 | opt->pattern_expression = compile_pattern_expr(&p); | |
83b5d2f5 JH |
891 | if (p) |
892 | die("incomplete pattern expression: %s", p->pattern); | |
80235ba7 JH |
893 | |
894 | if (!header_expr) | |
895 | return; | |
896 | ||
5aaeb733 | 897 | if (!opt->pattern_expression) |
80235ba7 | 898 | opt->pattern_expression = header_expr; |
13e4fc7e JH |
899 | else if (opt->all_match) |
900 | opt->pattern_expression = grep_splice_or(header_expr, | |
901 | opt->pattern_expression); | |
5aaeb733 JH |
902 | else |
903 | opt->pattern_expression = grep_or_expr(opt->pattern_expression, | |
904 | header_expr); | |
80235ba7 | 905 | opt->all_match = 1; |
83b5d2f5 JH |
906 | } |
907 | ||
17bf35a3 JH |
908 | void compile_grep_patterns(struct grep_opt *opt) |
909 | { | |
910 | compile_grep_patterns_real(opt); | |
911 | if (opt->debug) | |
912 | dump_grep_expression(opt); | |
913 | } | |
914 | ||
b48fb5b6 JH |
915 | static void free_pattern_expr(struct grep_expr *x) |
916 | { | |
917 | switch (x->node) { | |
5aaeb733 | 918 | case GREP_NODE_TRUE: |
b48fb5b6 JH |
919 | case GREP_NODE_ATOM: |
920 | break; | |
921 | case GREP_NODE_NOT: | |
922 | free_pattern_expr(x->u.unary); | |
923 | break; | |
924 | case GREP_NODE_AND: | |
925 | case GREP_NODE_OR: | |
926 | free_pattern_expr(x->u.binary.left); | |
927 | free_pattern_expr(x->u.binary.right); | |
928 | break; | |
929 | } | |
930 | free(x); | |
931 | } | |
932 | ||
933 | void free_grep_patterns(struct grep_opt *opt) | |
934 | { | |
935 | struct grep_pat *p, *n; | |
936 | ||
937 | for (p = opt->pattern_list; p; p = n) { | |
938 | n = p->next; | |
939 | switch (p->token) { | |
940 | case GREP_PATTERN: /* atom */ | |
941 | case GREP_PATTERN_HEAD: | |
942 | case GREP_PATTERN_BODY: | |
7599730b | 943 | if (p->pcre2_pattern) |
94da9193 | 944 | free_pcre2_pattern(p); |
63e7e9d8 MK |
945 | else |
946 | regfree(&p->regexp); | |
526a858a | 947 | free(p->pattern); |
b48fb5b6 JH |
948 | break; |
949 | default: | |
950 | break; | |
951 | } | |
952 | free(p); | |
953 | } | |
954 | ||
955 | if (!opt->extended) | |
956 | return; | |
957 | free_pattern_expr(opt->pattern_expression); | |
958 | } | |
959 | ||
83b5d2f5 JH |
960 | static char *end_of_line(char *cp, unsigned long *left) |
961 | { | |
962 | unsigned long l = *left; | |
963 | while (l && *cp != '\n') { | |
964 | l--; | |
965 | cp++; | |
966 | } | |
967 | *left = l; | |
968 | return cp; | |
969 | } | |
970 | ||
971 | static int word_char(char ch) | |
972 | { | |
973 | return isalnum(ch) || ch == '_'; | |
974 | } | |
975 | ||
55f638bd ML |
976 | static void output_color(struct grep_opt *opt, const void *data, size_t size, |
977 | const char *color) | |
978 | { | |
daa0c3d9 | 979 | if (want_color(opt->color) && color && color[0]) { |
55f638bd ML |
980 | opt->output(opt, color, strlen(color)); |
981 | opt->output(opt, data, size); | |
982 | opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET)); | |
983 | } else | |
984 | opt->output(opt, data, size); | |
985 | } | |
986 | ||
987 | static void output_sep(struct grep_opt *opt, char sign) | |
988 | { | |
989 | if (opt->null_following_name) | |
990 | opt->output(opt, "\0", 1); | |
991 | else | |
fa151dc5 | 992 | output_color(opt, &sign, 1, opt->colors[GREP_COLOR_SEP]); |
55f638bd ML |
993 | } |
994 | ||
83caecca RZ |
995 | static void show_name(struct grep_opt *opt, const char *name) |
996 | { | |
fa151dc5 | 997 | output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]); |
5b594f45 | 998 | opt->output(opt, opt->null_following_name ? "\0" : "\n", 1); |
83caecca RZ |
999 | } |
1000 | ||
97e77784 MK |
1001 | static int patmatch(struct grep_pat *p, char *line, char *eol, |
1002 | regmatch_t *match, int eflags) | |
1003 | { | |
1004 | int hit; | |
1005 | ||
7599730b | 1006 | if (p->pcre2_pattern) |
94da9193 | 1007 | hit = !pcre2match(p, line, eol, match, eflags); |
97e77784 | 1008 | else |
b7d36ffc JS |
1009 | hit = !regexec_buf(&p->regexp, line, eol - line, 1, match, |
1010 | eflags); | |
97e77784 MK |
1011 | |
1012 | return hit; | |
1013 | } | |
1014 | ||
a4d7d2c6 JH |
1015 | static int strip_timestamp(char *bol, char **eol_p) |
1016 | { | |
1017 | char *eol = *eol_p; | |
1018 | int ch; | |
1019 | ||
1020 | while (bol < --eol) { | |
1021 | if (*eol != '>') | |
1022 | continue; | |
1023 | *eol_p = ++eol; | |
1024 | ch = *eol; | |
1025 | *eol = '\0'; | |
1026 | return ch; | |
1027 | } | |
1028 | return 0; | |
1029 | } | |
1030 | ||
1031 | static struct { | |
1032 | const char *field; | |
1033 | size_t len; | |
1034 | } header_field[] = { | |
1035 | { "author ", 7 }, | |
1036 | { "committer ", 10 }, | |
72fd13f7 | 1037 | { "reflog ", 7 }, |
a4d7d2c6 JH |
1038 | }; |
1039 | ||
d7eb527d | 1040 | static int match_one_pattern(struct grep_pat *p, char *bol, char *eol, |
79212772 RS |
1041 | enum grep_context ctx, |
1042 | regmatch_t *pmatch, int eflags) | |
83b5d2f5 JH |
1043 | { |
1044 | int hit = 0; | |
a4d7d2c6 | 1045 | int saved_ch = 0; |
e701fadb | 1046 | const char *start = bol; |
83b5d2f5 | 1047 | |
480c1ca6 JH |
1048 | if ((p->token != GREP_PATTERN) && |
1049 | ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD))) | |
1050 | return 0; | |
1051 | ||
a4d7d2c6 JH |
1052 | if (p->token == GREP_PATTERN_HEAD) { |
1053 | const char *field; | |
1054 | size_t len; | |
1055 | assert(p->field < ARRAY_SIZE(header_field)); | |
1056 | field = header_field[p->field].field; | |
1057 | len = header_field[p->field].len; | |
1058 | if (strncmp(bol, field, len)) | |
1059 | return 0; | |
1060 | bol += len; | |
ad4813b3 NTND |
1061 | switch (p->field) { |
1062 | case GREP_HEADER_AUTHOR: | |
1063 | case GREP_HEADER_COMMITTER: | |
1064 | saved_ch = strip_timestamp(bol, &eol); | |
1065 | break; | |
1066 | default: | |
1067 | break; | |
1068 | } | |
a4d7d2c6 JH |
1069 | } |
1070 | ||
83b5d2f5 | 1071 | again: |
97e77784 | 1072 | hit = patmatch(p, bol, eol, pmatch, eflags); |
83b5d2f5 | 1073 | |
d7eb527d | 1074 | if (hit && p->word_regexp) { |
83b5d2f5 | 1075 | if ((pmatch[0].rm_so < 0) || |
84201eae | 1076 | (eol - bol) < pmatch[0].rm_so || |
83b5d2f5 JH |
1077 | (pmatch[0].rm_eo < 0) || |
1078 | (eol - bol) < pmatch[0].rm_eo) | |
1079 | die("regexp returned nonsense"); | |
1080 | ||
1081 | /* Match beginning must be either beginning of the | |
1082 | * line, or at word boundary (i.e. the last char must | |
1083 | * not be a word char). Similarly, match end must be | |
1084 | * either end of the line, or at word boundary | |
1085 | * (i.e. the next char must not be a word char). | |
1086 | */ | |
fb62eb7f | 1087 | if ( ((pmatch[0].rm_so == 0) || |
83b5d2f5 JH |
1088 | !word_char(bol[pmatch[0].rm_so-1])) && |
1089 | ((pmatch[0].rm_eo == (eol-bol)) || | |
1090 | !word_char(bol[pmatch[0].rm_eo])) ) | |
1091 | ; | |
1092 | else | |
1093 | hit = 0; | |
1094 | ||
84201eae RS |
1095 | /* Words consist of at least one character. */ |
1096 | if (pmatch->rm_so == pmatch->rm_eo) | |
1097 | hit = 0; | |
1098 | ||
83b5d2f5 JH |
1099 | if (!hit && pmatch[0].rm_so + bol + 1 < eol) { |
1100 | /* There could be more than one match on the | |
1101 | * line, and the first match might not be | |
1102 | * strict word match. But later ones could be! | |
fb62eb7f RS |
1103 | * Forward to the next possible start, i.e. the |
1104 | * next position following a non-word char. | |
83b5d2f5 JH |
1105 | */ |
1106 | bol = pmatch[0].rm_so + bol + 1; | |
fb62eb7f RS |
1107 | while (word_char(bol[-1]) && bol < eol) |
1108 | bol++; | |
dbb6a4ad | 1109 | eflags |= REG_NOTBOL; |
fb62eb7f RS |
1110 | if (bol < eol) |
1111 | goto again; | |
83b5d2f5 JH |
1112 | } |
1113 | } | |
a4d7d2c6 JH |
1114 | if (p->token == GREP_PATTERN_HEAD && saved_ch) |
1115 | *eol = saved_ch; | |
e701fadb RS |
1116 | if (hit) { |
1117 | pmatch[0].rm_so += bol - start; | |
1118 | pmatch[0].rm_eo += bol - start; | |
1119 | } | |
83b5d2f5 JH |
1120 | return hit; |
1121 | } | |
1122 | ||
68d686e6 TB |
1123 | static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x, char *bol, |
1124 | char *eol, enum grep_context ctx, ssize_t *col, | |
1125 | ssize_t *icol, int collect_hits) | |
83b5d2f5 | 1126 | { |
0ab7befa JH |
1127 | int h = 0; |
1128 | ||
c922b01f LT |
1129 | if (!x) |
1130 | die("Not a valid grep expression"); | |
83b5d2f5 | 1131 | switch (x->node) { |
5aaeb733 JH |
1132 | case GREP_NODE_TRUE: |
1133 | h = 1; | |
1134 | break; | |
83b5d2f5 | 1135 | case GREP_NODE_ATOM: |
68d686e6 TB |
1136 | { |
1137 | regmatch_t tmp; | |
1138 | h = match_one_pattern(x->u.atom, bol, eol, ctx, | |
1139 | &tmp, 0); | |
1140 | if (h && (*col < 0 || tmp.rm_so < *col)) | |
1141 | *col = tmp.rm_so; | |
1142 | } | |
83b5d2f5 JH |
1143 | break; |
1144 | case GREP_NODE_NOT: | |
68d686e6 TB |
1145 | /* |
1146 | * Upon visiting a GREP_NODE_NOT, col and icol become swapped. | |
1147 | */ | |
1148 | h = !match_expr_eval(opt, x->u.unary, bol, eol, ctx, icol, col, | |
1149 | 0); | |
0ab7befa | 1150 | break; |
83b5d2f5 | 1151 | case GREP_NODE_AND: |
017c0fcf | 1152 | h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col, |
68d686e6 | 1153 | icol, 0); |
017c0fcf TB |
1154 | if (h || opt->columnnum) { |
1155 | /* | |
1156 | * Don't short-circuit AND when given --column, since a | |
1157 | * NOT earlier in the tree may turn this into an OR. In | |
1158 | * this case, see the below comment. | |
1159 | */ | |
1160 | h &= match_expr_eval(opt, x->u.binary.right, bol, eol, | |
1161 | ctx, col, icol, 0); | |
1162 | } | |
0ab7befa | 1163 | break; |
83b5d2f5 | 1164 | case GREP_NODE_OR: |
017c0fcf TB |
1165 | if (!(collect_hits || opt->columnnum)) { |
1166 | /* | |
1167 | * Don't short-circuit OR when given --column (or | |
1168 | * collecting hits) to ensure we don't skip a later | |
1169 | * child that would produce an earlier match. | |
1170 | */ | |
68d686e6 TB |
1171 | return (match_expr_eval(opt, x->u.binary.left, bol, eol, |
1172 | ctx, col, icol, 0) || | |
1173 | match_expr_eval(opt, x->u.binary.right, bol, | |
1174 | eol, ctx, col, icol, 0)); | |
017c0fcf | 1175 | } |
68d686e6 TB |
1176 | h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col, |
1177 | icol, 0); | |
017c0fcf TB |
1178 | if (collect_hits) |
1179 | x->u.binary.left->hit |= h; | |
68d686e6 | 1180 | h |= match_expr_eval(opt, x->u.binary.right, bol, eol, ctx, col, |
017c0fcf | 1181 | icol, collect_hits); |
0ab7befa JH |
1182 | break; |
1183 | default: | |
d7530708 | 1184 | die("Unexpected node type (internal error) %d", x->node); |
83b5d2f5 | 1185 | } |
0ab7befa JH |
1186 | if (collect_hits) |
1187 | x->hit |= h; | |
1188 | return h; | |
83b5d2f5 JH |
1189 | } |
1190 | ||
480c1ca6 | 1191 | static int match_expr(struct grep_opt *opt, char *bol, char *eol, |
68d686e6 TB |
1192 | enum grep_context ctx, ssize_t *col, |
1193 | ssize_t *icol, int collect_hits) | |
83b5d2f5 JH |
1194 | { |
1195 | struct grep_expr *x = opt->pattern_expression; | |
68d686e6 | 1196 | return match_expr_eval(opt, x, bol, eol, ctx, col, icol, collect_hits); |
83b5d2f5 JH |
1197 | } |
1198 | ||
480c1ca6 | 1199 | static int match_line(struct grep_opt *opt, char *bol, char *eol, |
68d686e6 | 1200 | ssize_t *col, ssize_t *icol, |
0ab7befa | 1201 | enum grep_context ctx, int collect_hits) |
83b5d2f5 JH |
1202 | { |
1203 | struct grep_pat *p; | |
017c0fcf | 1204 | int hit = 0; |
79212772 | 1205 | |
83b5d2f5 | 1206 | if (opt->extended) |
68d686e6 TB |
1207 | return match_expr(opt, bol, eol, ctx, col, icol, |
1208 | collect_hits); | |
0ab7befa JH |
1209 | |
1210 | /* we do not call with collect_hits without being extended */ | |
83b5d2f5 | 1211 | for (p = opt->pattern_list; p; p = p->next) { |
68d686e6 TB |
1212 | regmatch_t tmp; |
1213 | if (match_one_pattern(p, bol, eol, ctx, &tmp, 0)) { | |
017c0fcf TB |
1214 | hit |= 1; |
1215 | if (!opt->columnnum) { | |
1216 | /* | |
1217 | * Without --column, any single match on a line | |
1218 | * is enough to know that it needs to be | |
1219 | * printed. With --column, scan _all_ patterns | |
1220 | * to find the earliest. | |
1221 | */ | |
1222 | break; | |
1223 | } | |
1224 | if (*col < 0 || tmp.rm_so < *col) | |
1225 | *col = tmp.rm_so; | |
68d686e6 | 1226 | } |
83b5d2f5 | 1227 | } |
017c0fcf | 1228 | return hit; |
83b5d2f5 JH |
1229 | } |
1230 | ||
7e8f59d5 RS |
1231 | static int match_next_pattern(struct grep_pat *p, char *bol, char *eol, |
1232 | enum grep_context ctx, | |
1233 | regmatch_t *pmatch, int eflags) | |
1234 | { | |
1235 | regmatch_t match; | |
1236 | ||
1237 | if (!match_one_pattern(p, bol, eol, ctx, &match, eflags)) | |
1238 | return 0; | |
1239 | if (match.rm_so < 0 || match.rm_eo < 0) | |
1240 | return 0; | |
1241 | if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) { | |
1242 | if (match.rm_so > pmatch->rm_so) | |
1243 | return 1; | |
1244 | if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo) | |
1245 | return 1; | |
1246 | } | |
1247 | pmatch->rm_so = match.rm_so; | |
1248 | pmatch->rm_eo = match.rm_eo; | |
1249 | return 1; | |
1250 | } | |
1251 | ||
1252 | static int next_match(struct grep_opt *opt, char *bol, char *eol, | |
1253 | enum grep_context ctx, regmatch_t *pmatch, int eflags) | |
1254 | { | |
1255 | struct grep_pat *p; | |
1256 | int hit = 0; | |
1257 | ||
1258 | pmatch->rm_so = pmatch->rm_eo = -1; | |
1259 | if (bol < eol) { | |
1260 | for (p = opt->pattern_list; p; p = p->next) { | |
1261 | switch (p->token) { | |
1262 | case GREP_PATTERN: /* atom */ | |
1263 | case GREP_PATTERN_HEAD: | |
1264 | case GREP_PATTERN_BODY: | |
1265 | hit |= match_next_pattern(p, bol, eol, ctx, | |
1266 | pmatch, eflags); | |
1267 | break; | |
1268 | default: | |
1269 | break; | |
1270 | } | |
1271 | } | |
1272 | } | |
1273 | return hit; | |
1274 | } | |
1275 | ||
c707ded3 TB |
1276 | static void show_line_header(struct grep_opt *opt, const char *name, |
1277 | unsigned lno, ssize_t cno, char sign) | |
7e8f59d5 | 1278 | { |
1d84f72e | 1279 | if (opt->heading && opt->last_shown == 0) { |
fa151dc5 | 1280 | output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]); |
1d84f72e RS |
1281 | opt->output(opt, "\n", 1); |
1282 | } | |
5dd06d38 RS |
1283 | opt->last_shown = lno; |
1284 | ||
1d84f72e | 1285 | if (!opt->heading && opt->pathname) { |
fa151dc5 | 1286 | output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]); |
55f638bd | 1287 | output_sep(opt, sign); |
5b594f45 FK |
1288 | } |
1289 | if (opt->linenum) { | |
1290 | char buf[32]; | |
1a168e5c | 1291 | xsnprintf(buf, sizeof(buf), "%d", lno); |
fa151dc5 | 1292 | output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_LINENO]); |
55f638bd | 1293 | output_sep(opt, sign); |
5b594f45 | 1294 | } |
89252cd0 TB |
1295 | /* |
1296 | * Treat 'cno' as the 1-indexed offset from the start of a non-context | |
1297 | * line to its first match. Otherwise, 'cno' is 0 indicating that we are | |
1298 | * being called with a context line. | |
1299 | */ | |
1300 | if (opt->columnnum && cno) { | |
1301 | char buf[32]; | |
1302 | xsnprintf(buf, sizeof(buf), "%"PRIuMAX, (uintmax_t)cno); | |
d036d667 | 1303 | output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_COLUMNNO]); |
89252cd0 TB |
1304 | output_sep(opt, sign); |
1305 | } | |
c707ded3 TB |
1306 | } |
1307 | ||
1308 | static void show_line(struct grep_opt *opt, char *bol, char *eol, | |
1309 | const char *name, unsigned lno, ssize_t cno, char sign) | |
1310 | { | |
1311 | int rest = eol - bol; | |
9d8db06e TB |
1312 | const char *match_color = NULL; |
1313 | const char *line_color = NULL; | |
c707ded3 TB |
1314 | |
1315 | if (opt->file_break && opt->last_shown == 0) { | |
1316 | if (opt->show_hunk_mark) | |
1317 | opt->output(opt, "\n", 1); | |
1318 | } else if (opt->pre_context || opt->post_context || opt->funcbody) { | |
1319 | if (opt->last_shown == 0) { | |
1320 | if (opt->show_hunk_mark) { | |
87ece7ce | 1321 | output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]); |
c707ded3 TB |
1322 | opt->output(opt, "\n", 1); |
1323 | } | |
1324 | } else if (lno > opt->last_shown + 1) { | |
87ece7ce | 1325 | output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]); |
c707ded3 TB |
1326 | opt->output(opt, "\n", 1); |
1327 | } | |
1328 | } | |
9d8db06e TB |
1329 | if (!opt->only_matching) { |
1330 | /* | |
1331 | * In case the line we're being called with contains more than | |
1332 | * one match, leave printing each header to the loop below. | |
1333 | */ | |
1334 | show_line_header(opt, name, lno, cno, sign); | |
1335 | } | |
1336 | if (opt->color || opt->only_matching) { | |
7e8f59d5 RS |
1337 | regmatch_t match; |
1338 | enum grep_context ctx = GREP_CONTEXT_BODY; | |
1339 | int ch = *eol; | |
1340 | int eflags = 0; | |
1341 | ||
9d8db06e TB |
1342 | if (opt->color) { |
1343 | if (sign == ':') | |
87ece7ce | 1344 | match_color = opt->colors[GREP_COLOR_MATCH_SELECTED]; |
9d8db06e | 1345 | else |
87ece7ce | 1346 | match_color = opt->colors[GREP_COLOR_MATCH_CONTEXT]; |
9d8db06e | 1347 | if (sign == ':') |
87ece7ce | 1348 | line_color = opt->colors[GREP_COLOR_SELECTED]; |
9d8db06e | 1349 | else if (sign == '-') |
87ece7ce | 1350 | line_color = opt->colors[GREP_COLOR_CONTEXT]; |
9d8db06e | 1351 | else if (sign == '=') |
87ece7ce | 1352 | line_color = opt->colors[GREP_COLOR_FUNCTION]; |
9d8db06e | 1353 | } |
7e8f59d5 RS |
1354 | *eol = '\0'; |
1355 | while (next_match(opt, bol, eol, ctx, &match, eflags)) { | |
1f5b9cc4 RS |
1356 | if (match.rm_so == match.rm_eo) |
1357 | break; | |
5b594f45 | 1358 | |
9d8db06e TB |
1359 | if (opt->only_matching) |
1360 | show_line_header(opt, name, lno, cno, sign); | |
1361 | else | |
1362 | output_color(opt, bol, match.rm_so, line_color); | |
55f638bd | 1363 | output_color(opt, bol + match.rm_so, |
79a77109 | 1364 | match.rm_eo - match.rm_so, match_color); |
9d8db06e TB |
1365 | if (opt->only_matching) |
1366 | opt->output(opt, "\n", 1); | |
7e8f59d5 | 1367 | bol += match.rm_eo; |
9d8db06e | 1368 | cno += match.rm_eo; |
7e8f59d5 RS |
1369 | rest -= match.rm_eo; |
1370 | eflags = REG_NOTBOL; | |
1371 | } | |
1372 | *eol = ch; | |
1373 | } | |
9d8db06e TB |
1374 | if (!opt->only_matching) { |
1375 | output_color(opt, bol, rest, line_color); | |
1376 | opt->output(opt, "\n", 1); | |
1377 | } | |
7e8f59d5 RS |
1378 | } |
1379 | ||
78db6ea9 JK |
1380 | int grep_use_locks; |
1381 | ||
0579f91d TR |
1382 | /* |
1383 | * This lock protects access to the gitattributes machinery, which is | |
1384 | * not thread-safe. | |
1385 | */ | |
1386 | pthread_mutex_t grep_attr_mutex; | |
1387 | ||
78db6ea9 | 1388 | static inline void grep_attr_lock(void) |
0579f91d | 1389 | { |
78db6ea9 | 1390 | if (grep_use_locks) |
0579f91d TR |
1391 | pthread_mutex_lock(&grep_attr_mutex); |
1392 | } | |
1393 | ||
78db6ea9 | 1394 | static inline void grep_attr_unlock(void) |
0579f91d | 1395 | { |
78db6ea9 | 1396 | if (grep_use_locks) |
0579f91d TR |
1397 | pthread_mutex_unlock(&grep_attr_mutex); |
1398 | } | |
b3aeb285 | 1399 | |
e1327023 | 1400 | static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol) |
2944e4e6 | 1401 | { |
60ecac98 | 1402 | xdemitconf_t *xecfg = opt->priv; |
0579f91d | 1403 | if (xecfg && !xecfg->find_func) { |
acd00ea0 | 1404 | grep_source_load_driver(gs, opt->repo->index); |
94ad9d9e JK |
1405 | if (gs->driver->funcname.pattern) { |
1406 | const struct userdiff_funcname *pe = &gs->driver->funcname; | |
0579f91d TR |
1407 | xdiff_set_find_func(xecfg, pe->pattern, pe->cflags); |
1408 | } else { | |
1409 | xecfg = opt->priv = NULL; | |
1410 | } | |
1411 | } | |
1412 | ||
1413 | if (xecfg) { | |
60ecac98 RS |
1414 | char buf[1]; |
1415 | return xecfg->find_func(bol, eol - bol, buf, 1, | |
1416 | xecfg->find_func_priv) >= 0; | |
1417 | } | |
1418 | ||
2944e4e6 RS |
1419 | if (bol == eol) |
1420 | return 0; | |
1421 | if (isalpha(*bol) || *bol == '_' || *bol == '$') | |
1422 | return 1; | |
1423 | return 0; | |
1424 | } | |
1425 | ||
e1327023 JK |
1426 | static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs, |
1427 | char *bol, unsigned lno) | |
2944e4e6 | 1428 | { |
e1327023 | 1429 | while (bol > gs->buf) { |
2944e4e6 RS |
1430 | char *eol = --bol; |
1431 | ||
e1327023 | 1432 | while (bol > gs->buf && bol[-1] != '\n') |
2944e4e6 RS |
1433 | bol--; |
1434 | lno--; | |
1435 | ||
1436 | if (lno <= opt->last_shown) | |
1437 | break; | |
1438 | ||
e1327023 | 1439 | if (match_funcname(opt, gs, bol, eol)) { |
89252cd0 | 1440 | show_line(opt, bol, eol, gs->name, lno, 0, '='); |
2944e4e6 RS |
1441 | break; |
1442 | } | |
1443 | } | |
1444 | } | |
1445 | ||
a5dc20b0 RS |
1446 | static int is_empty_line(const char *bol, const char *eol); |
1447 | ||
e1327023 | 1448 | static void show_pre_context(struct grep_opt *opt, struct grep_source *gs, |
ba8ea749 | 1449 | char *bol, char *end, unsigned lno) |
49de3216 | 1450 | { |
6653a01b | 1451 | unsigned cur = lno, from = 1, funcname_lno = 0, orig_from; |
a5dc20b0 | 1452 | int funcname_needed = !!opt->funcname, comment_needed = 0; |
ba8ea749 | 1453 | |
49de3216 RS |
1454 | if (opt->pre_context < lno) |
1455 | from = lno - opt->pre_context; | |
1456 | if (from <= opt->last_shown) | |
1457 | from = opt->last_shown + 1; | |
6653a01b | 1458 | orig_from = from; |
a5dc20b0 RS |
1459 | if (opt->funcbody) { |
1460 | if (match_funcname(opt, gs, bol, end)) | |
1461 | comment_needed = 1; | |
1462 | else | |
1463 | funcname_needed = 1; | |
6653a01b RS |
1464 | from = opt->last_shown + 1; |
1465 | } | |
49de3216 RS |
1466 | |
1467 | /* Rewind. */ | |
6653a01b | 1468 | while (bol > gs->buf && cur > from) { |
a5dc20b0 | 1469 | char *next_bol = bol; |
2944e4e6 RS |
1470 | char *eol = --bol; |
1471 | ||
e1327023 | 1472 | while (bol > gs->buf && bol[-1] != '\n') |
49de3216 RS |
1473 | bol--; |
1474 | cur--; | |
a5dc20b0 RS |
1475 | if (comment_needed && (is_empty_line(bol, eol) || |
1476 | match_funcname(opt, gs, bol, eol))) { | |
1477 | comment_needed = 0; | |
1478 | from = orig_from; | |
1479 | if (cur < from) { | |
1480 | cur++; | |
1481 | bol = next_bol; | |
1482 | break; | |
1483 | } | |
1484 | } | |
e1327023 | 1485 | if (funcname_needed && match_funcname(opt, gs, bol, eol)) { |
2944e4e6 RS |
1486 | funcname_lno = cur; |
1487 | funcname_needed = 0; | |
a5dc20b0 RS |
1488 | if (opt->funcbody) |
1489 | comment_needed = 1; | |
1490 | else | |
1491 | from = orig_from; | |
2944e4e6 | 1492 | } |
49de3216 RS |
1493 | } |
1494 | ||
2944e4e6 RS |
1495 | /* We need to look even further back to find a function signature. */ |
1496 | if (opt->funcname && funcname_needed) | |
e1327023 | 1497 | show_funcname_line(opt, gs, bol, cur); |
2944e4e6 | 1498 | |
49de3216 RS |
1499 | /* Back forward. */ |
1500 | while (cur < lno) { | |
2944e4e6 | 1501 | char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-'; |
49de3216 RS |
1502 | |
1503 | while (*eol != '\n') | |
1504 | eol++; | |
89252cd0 | 1505 | show_line(opt, bol, eol, gs->name, cur, 0, sign); |
49de3216 RS |
1506 | bol = eol + 1; |
1507 | cur++; | |
1508 | } | |
1509 | } | |
1510 | ||
a26345b6 JH |
1511 | static int should_lookahead(struct grep_opt *opt) |
1512 | { | |
1513 | struct grep_pat *p; | |
1514 | ||
1515 | if (opt->extended) | |
1516 | return 0; /* punt for too complex stuff */ | |
1517 | if (opt->invert) | |
1518 | return 0; | |
1519 | for (p = opt->pattern_list; p; p = p->next) { | |
1520 | if (p->token != GREP_PATTERN) | |
1521 | return 0; /* punt for "header only" and stuff */ | |
1522 | } | |
1523 | return 1; | |
1524 | } | |
1525 | ||
1526 | static int look_ahead(struct grep_opt *opt, | |
1527 | unsigned long *left_p, | |
1528 | unsigned *lno_p, | |
1529 | char **bol_p) | |
1530 | { | |
1531 | unsigned lno = *lno_p; | |
1532 | char *bol = *bol_p; | |
1533 | struct grep_pat *p; | |
1534 | char *sp, *last_bol; | |
1535 | regoff_t earliest = -1; | |
1536 | ||
1537 | for (p = opt->pattern_list; p; p = p->next) { | |
1538 | int hit; | |
1539 | regmatch_t m; | |
1540 | ||
97e77784 | 1541 | hit = patmatch(p, bol, bol + *left_p, &m, 0); |
a26345b6 JH |
1542 | if (!hit || m.rm_so < 0 || m.rm_eo < 0) |
1543 | continue; | |
1544 | if (earliest < 0 || m.rm_so < earliest) | |
1545 | earliest = m.rm_so; | |
1546 | } | |
1547 | ||
1548 | if (earliest < 0) { | |
1549 | *bol_p = bol + *left_p; | |
1550 | *left_p = 0; | |
1551 | return 1; | |
1552 | } | |
1553 | for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--) | |
1554 | ; /* find the beginning of the line */ | |
1555 | last_bol = sp; | |
1556 | ||
1557 | for (sp = bol; sp < last_bol; sp++) { | |
1558 | if (*sp == '\n') | |
1559 | lno++; | |
1560 | } | |
1561 | *left_p -= last_bol - bol; | |
1562 | *bol_p = last_bol; | |
1563 | *lno_p = lno; | |
1564 | return 0; | |
1565 | } | |
1566 | ||
38bbc2ea NTND |
1567 | static int fill_textconv_grep(struct repository *r, |
1568 | struct userdiff_driver *driver, | |
335ec3bf JK |
1569 | struct grep_source *gs) |
1570 | { | |
1571 | struct diff_filespec *df; | |
1572 | char *buf; | |
1573 | size_t size; | |
1574 | ||
1575 | if (!driver || !driver->textconv) | |
1576 | return grep_source_load(gs); | |
1577 | ||
1578 | /* | |
1579 | * The textconv interface is intimately tied to diff_filespecs, so we | |
1580 | * have to pretend to be one. If we could unify the grep_source | |
1581 | * and diff_filespec structs, this mess could just go away. | |
1582 | */ | |
1583 | df = alloc_filespec(gs->path); | |
1584 | switch (gs->type) { | |
1c41c82b | 1585 | case GREP_SOURCE_OID: |
335ec3bf JK |
1586 | fill_filespec(df, gs->identifier, 1, 0100644); |
1587 | break; | |
1588 | case GREP_SOURCE_FILE: | |
f9704c2d | 1589 | fill_filespec(df, &null_oid, 0, 0100644); |
335ec3bf JK |
1590 | break; |
1591 | default: | |
033abf97 | 1592 | BUG("attempt to textconv something without a path?"); |
335ec3bf JK |
1593 | } |
1594 | ||
1595 | /* | |
1d1729ca MT |
1596 | * fill_textconv is not remotely thread-safe; it modifies the global |
1597 | * diff tempfile structure, writes to the_repo's odb and might | |
1598 | * internally call thread-unsafe functions such as the | |
1599 | * prepare_packed_git() lazy-initializator. Because of the last two, we | |
1600 | * must ensure mutual exclusion between this call and the object reading | |
1601 | * API, thus we use obj_read_lock() here. | |
1602 | * | |
1603 | * TODO: allowing text conversion to run in parallel with object | |
1604 | * reading operations might increase performance in the multithreaded | |
1605 | * non-worktreee git-grep with --textconv. | |
335ec3bf | 1606 | */ |
1d1729ca | 1607 | obj_read_lock(); |
38bbc2ea | 1608 | size = fill_textconv(r, driver, df, &buf); |
1d1729ca | 1609 | obj_read_unlock(); |
335ec3bf JK |
1610 | free_filespec(df); |
1611 | ||
1612 | /* | |
1613 | * The normal fill_textconv usage by the diff machinery would just keep | |
1614 | * the textconv'd buf separate from the diff_filespec. But much of the | |
1615 | * grep code passes around a grep_source and assumes that its "buf" | |
1616 | * pointer is the beginning of the thing we are searching. So let's | |
1617 | * install our textconv'd version into the grep_source, taking care not | |
1618 | * to leak any existing buffer. | |
1619 | */ | |
1620 | grep_source_clear_data(gs); | |
1621 | gs->buf = buf; | |
1622 | gs->size = size; | |
1623 | ||
1624 | return 0; | |
1625 | } | |
1626 | ||
4aa2c475 RS |
1627 | static int is_empty_line(const char *bol, const char *eol) |
1628 | { | |
1629 | while (bol < eol && isspace(*bol)) | |
1630 | bol++; | |
1631 | return bol == eol; | |
1632 | } | |
1633 | ||
e1327023 | 1634 | static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits) |
83b5d2f5 | 1635 | { |
e1327023 | 1636 | char *bol; |
4aa2c475 | 1637 | char *peek_bol = NULL; |
e1327023 | 1638 | unsigned long left; |
83b5d2f5 | 1639 | unsigned lno = 1; |
83b5d2f5 | 1640 | unsigned last_hit = 0; |
83b5d2f5 | 1641 | int binary_match_only = 0; |
83b5d2f5 | 1642 | unsigned count = 0; |
a26345b6 | 1643 | int try_lookahead = 0; |
ba8ea749 | 1644 | int show_function = 0; |
335ec3bf | 1645 | struct userdiff_driver *textconv = NULL; |
480c1ca6 | 1646 | enum grep_context ctx = GREP_CONTEXT_HEAD; |
60ecac98 | 1647 | xdemitconf_t xecfg; |
83b5d2f5 | 1648 | |
de99eb0c ES |
1649 | if (!opt->status_only && gs->name == NULL) |
1650 | BUG("grep call which could print a name requires " | |
1651 | "grep_source.name be non-NULL"); | |
1652 | ||
5b594f45 FK |
1653 | if (!opt->output) |
1654 | opt->output = std_output; | |
1655 | ||
ba8ea749 RS |
1656 | if (opt->pre_context || opt->post_context || opt->file_break || |
1657 | opt->funcbody) { | |
08303c36 RS |
1658 | /* Show hunk marks, except for the first file. */ |
1659 | if (opt->last_shown) | |
1660 | opt->show_hunk_mark = 1; | |
1661 | /* | |
1662 | * If we're using threads then we can't easily identify | |
1663 | * the first file. Always put hunk marks in that case | |
1664 | * and skip the very first one later in work_done(). | |
1665 | */ | |
1666 | if (opt->output != std_output) | |
1667 | opt->show_hunk_mark = 1; | |
1668 | } | |
431d6e7b RS |
1669 | opt->last_shown = 0; |
1670 | ||
335ec3bf | 1671 | if (opt->allow_textconv) { |
acd00ea0 | 1672 | grep_source_load_driver(gs, opt->repo->index); |
335ec3bf JK |
1673 | /* |
1674 | * We might set up the shared textconv cache data here, which | |
1d1729ca MT |
1675 | * is not thread-safe. Also, get_oid_with_context() and |
1676 | * parse_object() might be internally called. As they are not | |
84544f2e | 1677 | * currently thread-safe and might be racy with object reading, |
1d1729ca | 1678 | * obj_read_lock() must be called. |
335ec3bf JK |
1679 | */ |
1680 | grep_attr_lock(); | |
1d1729ca | 1681 | obj_read_lock(); |
bd7ad45b | 1682 | textconv = userdiff_get_textconv(opt->repo, gs->driver); |
1d1729ca | 1683 | obj_read_unlock(); |
335ec3bf JK |
1684 | grep_attr_unlock(); |
1685 | } | |
1686 | ||
1687 | /* | |
1688 | * We know the result of a textconv is text, so we only have to care | |
1689 | * about binary handling if we are not using it. | |
1690 | */ | |
1691 | if (!textconv) { | |
1692 | switch (opt->binary) { | |
1693 | case GREP_BINARY_DEFAULT: | |
acd00ea0 | 1694 | if (grep_source_is_binary(gs, opt->repo->index)) |
335ec3bf JK |
1695 | binary_match_only = 1; |
1696 | break; | |
1697 | case GREP_BINARY_NOMATCH: | |
acd00ea0 | 1698 | if (grep_source_is_binary(gs, opt->repo->index)) |
335ec3bf JK |
1699 | return 0; /* Assume unmatch */ |
1700 | break; | |
1701 | case GREP_BINARY_TEXT: | |
1702 | break; | |
1703 | default: | |
033abf97 | 1704 | BUG("unknown binary handling mode"); |
335ec3bf | 1705 | } |
83b5d2f5 JH |
1706 | } |
1707 | ||
60ecac98 | 1708 | memset(&xecfg, 0, sizeof(xecfg)); |
0579f91d TR |
1709 | opt->priv = &xecfg; |
1710 | ||
a26345b6 | 1711 | try_lookahead = should_lookahead(opt); |
60ecac98 | 1712 | |
38bbc2ea | 1713 | if (fill_textconv_grep(opt->repo, textconv, gs) < 0) |
08265798 JK |
1714 | return 0; |
1715 | ||
e1327023 JK |
1716 | bol = gs->buf; |
1717 | left = gs->size; | |
83b5d2f5 JH |
1718 | while (left) { |
1719 | char *eol, ch; | |
0ab7befa | 1720 | int hit; |
89252cd0 | 1721 | ssize_t cno; |
68d686e6 | 1722 | ssize_t col = -1, icol = -1; |
83b5d2f5 | 1723 | |
a26345b6 | 1724 | /* |
8997da38 | 1725 | * look_ahead() skips quickly to the line that possibly |
a26345b6 JH |
1726 | * has the next hit; don't call it if we need to do |
1727 | * something more than just skipping the current line | |
1728 | * in response to an unmatch for the current line. E.g. | |
1729 | * inside a post-context window, we will show the current | |
1730 | * line as a context around the previous hit when it | |
1731 | * doesn't hit. | |
1732 | */ | |
1733 | if (try_lookahead | |
1734 | && !(last_hit | |
ba8ea749 RS |
1735 | && (show_function || |
1736 | lno <= last_hit + opt->post_context)) | |
a26345b6 JH |
1737 | && look_ahead(opt, &left, &lno, &bol)) |
1738 | break; | |
83b5d2f5 JH |
1739 | eol = end_of_line(bol, &left); |
1740 | ch = *eol; | |
1741 | *eol = 0; | |
1742 | ||
480c1ca6 JH |
1743 | if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol)) |
1744 | ctx = GREP_CONTEXT_BODY; | |
1745 | ||
68d686e6 | 1746 | hit = match_line(opt, bol, eol, &col, &icol, ctx, collect_hits); |
83b5d2f5 JH |
1747 | *eol = ch; |
1748 | ||
0ab7befa JH |
1749 | if (collect_hits) |
1750 | goto next_line; | |
1751 | ||
83b5d2f5 JH |
1752 | /* "grep -v -e foo -e bla" should list lines |
1753 | * that do not have either, so inversion should | |
1754 | * be done outside. | |
1755 | */ | |
1756 | if (opt->invert) | |
1757 | hit = !hit; | |
1758 | if (opt->unmatch_name_only) { | |
1759 | if (hit) | |
1760 | return 0; | |
1761 | goto next_line; | |
1762 | } | |
1763 | if (hit) { | |
1764 | count++; | |
1765 | if (opt->status_only) | |
1766 | return 1; | |
321ffcc0 | 1767 | if (opt->name_only) { |
e1327023 | 1768 | show_name(opt, gs->name); |
321ffcc0 RS |
1769 | return 1; |
1770 | } | |
c30c10cf RS |
1771 | if (opt->count) |
1772 | goto next_line; | |
83b5d2f5 | 1773 | if (binary_match_only) { |
5b594f45 | 1774 | opt->output(opt, "Binary file ", 12); |
e1327023 | 1775 | output_color(opt, gs->name, strlen(gs->name), |
fa151dc5 | 1776 | opt->colors[GREP_COLOR_FILENAME]); |
5b594f45 | 1777 | opt->output(opt, " matches\n", 9); |
83b5d2f5 JH |
1778 | return 1; |
1779 | } | |
83b5d2f5 JH |
1780 | /* Hit at this line. If we haven't shown the |
1781 | * pre-context lines, we would need to show them. | |
83b5d2f5 | 1782 | */ |
ba8ea749 | 1783 | if (opt->pre_context || opt->funcbody) |
e1327023 | 1784 | show_pre_context(opt, gs, bol, eol, lno); |
2944e4e6 | 1785 | else if (opt->funcname) |
e1327023 | 1786 | show_funcname_line(opt, gs, bol, lno); |
89252cd0 TB |
1787 | cno = opt->invert ? icol : col; |
1788 | if (cno < 0) { | |
1789 | /* | |
1790 | * A negative cno indicates that there was no | |
1791 | * match on the line. We are thus inverted and | |
1792 | * being asked to show all lines that _don't_ | |
1793 | * match a given expression. Therefore, set cno | |
1794 | * to 0 to suggest the whole line matches. | |
1795 | */ | |
1796 | cno = 0; | |
1797 | } | |
1798 | show_line(opt, bol, eol, gs->name, lno, cno + 1, ':'); | |
5dd06d38 | 1799 | last_hit = lno; |
ba8ea749 RS |
1800 | if (opt->funcbody) |
1801 | show_function = 1; | |
1802 | goto next_line; | |
83b5d2f5 | 1803 | } |
4aa2c475 RS |
1804 | if (show_function && (!peek_bol || peek_bol < bol)) { |
1805 | unsigned long peek_left = left; | |
1806 | char *peek_eol = eol; | |
1807 | ||
1808 | /* | |
1809 | * Trailing empty lines are not interesting. | |
1810 | * Peek past them to see if they belong to the | |
1811 | * body of the current function. | |
1812 | */ | |
1813 | peek_bol = bol; | |
1814 | while (is_empty_line(peek_bol, peek_eol)) { | |
1815 | peek_bol = peek_eol + 1; | |
1816 | peek_eol = end_of_line(peek_bol, &peek_left); | |
1817 | } | |
1818 | ||
1819 | if (match_funcname(opt, gs, peek_bol, peek_eol)) | |
1820 | show_function = 0; | |
1821 | } | |
ba8ea749 RS |
1822 | if (show_function || |
1823 | (last_hit && lno <= last_hit + opt->post_context)) { | |
83b5d2f5 JH |
1824 | /* If the last hit is within the post context, |
1825 | * we need to show this line. | |
1826 | */ | |
89252cd0 | 1827 | show_line(opt, bol, eol, gs->name, lno, col + 1, '-'); |
83b5d2f5 | 1828 | } |
83b5d2f5 JH |
1829 | |
1830 | next_line: | |
1831 | bol = eol + 1; | |
1832 | if (!left) | |
1833 | break; | |
1834 | left--; | |
1835 | lno++; | |
1836 | } | |
1837 | ||
0ab7befa JH |
1838 | if (collect_hits) |
1839 | return 0; | |
b48fb5b6 | 1840 | |
83b5d2f5 | 1841 | if (opt->status_only) |
e1f68c66 | 1842 | return opt->unmatch_name_only; |
83b5d2f5 JH |
1843 | if (opt->unmatch_name_only) { |
1844 | /* We did not see any hit, so we want to show this */ | |
e1327023 | 1845 | show_name(opt, gs->name); |
83b5d2f5 JH |
1846 | return 1; |
1847 | } | |
1848 | ||
60ecac98 RS |
1849 | xdiff_clear_find_func(&xecfg); |
1850 | opt->priv = NULL; | |
1851 | ||
83b5d2f5 JH |
1852 | /* NEEDSWORK: |
1853 | * The real "grep -c foo *.c" gives many "bar.c:0" lines, | |
1854 | * which feels mostly useless but sometimes useful. Maybe | |
1855 | * make it another option? For now suppress them. | |
1856 | */ | |
5b594f45 FK |
1857 | if (opt->count && count) { |
1858 | char buf[32]; | |
f76d947a RS |
1859 | if (opt->pathname) { |
1860 | output_color(opt, gs->name, strlen(gs->name), | |
fa151dc5 | 1861 | opt->colors[GREP_COLOR_FILENAME]); |
f76d947a RS |
1862 | output_sep(opt, ':'); |
1863 | } | |
1a168e5c | 1864 | xsnprintf(buf, sizeof(buf), "%u\n", count); |
5b594f45 | 1865 | opt->output(opt, buf, strlen(buf)); |
c30c10cf | 1866 | return 1; |
5b594f45 | 1867 | } |
83b5d2f5 JH |
1868 | return !!last_hit; |
1869 | } | |
1870 | ||
0ab7befa JH |
1871 | static void clr_hit_marker(struct grep_expr *x) |
1872 | { | |
1873 | /* All-hit markers are meaningful only at the very top level | |
1874 | * OR node. | |
1875 | */ | |
1876 | while (1) { | |
1877 | x->hit = 0; | |
1878 | if (x->node != GREP_NODE_OR) | |
1879 | return; | |
1880 | x->u.binary.left->hit = 0; | |
1881 | x = x->u.binary.right; | |
1882 | } | |
1883 | } | |
1884 | ||
1885 | static int chk_hit_marker(struct grep_expr *x) | |
1886 | { | |
1887 | /* Top level nodes have hit markers. See if they all are hits */ | |
1888 | while (1) { | |
1889 | if (x->node != GREP_NODE_OR) | |
1890 | return x->hit; | |
1891 | if (!x->u.binary.left->hit) | |
1892 | return 0; | |
1893 | x = x->u.binary.right; | |
1894 | } | |
1895 | } | |
1896 | ||
e1327023 | 1897 | int grep_source(struct grep_opt *opt, struct grep_source *gs) |
0ab7befa JH |
1898 | { |
1899 | /* | |
1900 | * we do not have to do the two-pass grep when we do not check | |
1901 | * buffer-wide "all-match". | |
1902 | */ | |
1903 | if (!opt->all_match) | |
e1327023 | 1904 | return grep_source_1(opt, gs, 0); |
0ab7befa JH |
1905 | |
1906 | /* Otherwise the toplevel "or" terms hit a bit differently. | |
1907 | * We first clear hit markers from them. | |
1908 | */ | |
1909 | clr_hit_marker(opt->pattern_expression); | |
e1327023 | 1910 | grep_source_1(opt, gs, 1); |
0ab7befa JH |
1911 | |
1912 | if (!chk_hit_marker(opt->pattern_expression)) | |
1913 | return 0; | |
1914 | ||
e1327023 JK |
1915 | return grep_source_1(opt, gs, 0); |
1916 | } | |
1917 | ||
c876d6da | 1918 | int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size) |
e1327023 JK |
1919 | { |
1920 | struct grep_source gs; | |
1921 | int r; | |
1922 | ||
55c61688 | 1923 | grep_source_init(&gs, GREP_SOURCE_BUF, NULL, NULL, NULL); |
e1327023 JK |
1924 | gs.buf = buf; |
1925 | gs.size = size; | |
1926 | ||
1927 | r = grep_source(opt, &gs); | |
1928 | ||
1929 | grep_source_clear(&gs); | |
1930 | return r; | |
1931 | } | |
1932 | ||
1933 | void grep_source_init(struct grep_source *gs, enum grep_source_type type, | |
55c61688 NTND |
1934 | const char *name, const char *path, |
1935 | const void *identifier) | |
e1327023 JK |
1936 | { |
1937 | gs->type = type; | |
8c53f071 JK |
1938 | gs->name = xstrdup_or_null(name); |
1939 | gs->path = xstrdup_or_null(path); | |
e1327023 JK |
1940 | gs->buf = NULL; |
1941 | gs->size = 0; | |
94ad9d9e | 1942 | gs->driver = NULL; |
e1327023 JK |
1943 | |
1944 | switch (type) { | |
1945 | case GREP_SOURCE_FILE: | |
1946 | gs->identifier = xstrdup(identifier); | |
1947 | break; | |
1c41c82b BW |
1948 | case GREP_SOURCE_OID: |
1949 | gs->identifier = oiddup(identifier); | |
e1327023 JK |
1950 | break; |
1951 | case GREP_SOURCE_BUF: | |
1952 | gs->identifier = NULL; | |
4538eef5 | 1953 | break; |
e1327023 JK |
1954 | } |
1955 | } | |
1956 | ||
1957 | void grep_source_clear(struct grep_source *gs) | |
1958 | { | |
88ce3ef6 ÆAB |
1959 | FREE_AND_NULL(gs->name); |
1960 | FREE_AND_NULL(gs->path); | |
1961 | FREE_AND_NULL(gs->identifier); | |
e1327023 JK |
1962 | grep_source_clear_data(gs); |
1963 | } | |
1964 | ||
1965 | void grep_source_clear_data(struct grep_source *gs) | |
1966 | { | |
1967 | switch (gs->type) { | |
1968 | case GREP_SOURCE_FILE: | |
1c41c82b | 1969 | case GREP_SOURCE_OID: |
6a83d902 | 1970 | FREE_AND_NULL(gs->buf); |
e1327023 JK |
1971 | gs->size = 0; |
1972 | break; | |
1973 | case GREP_SOURCE_BUF: | |
1974 | /* leave user-provided buf intact */ | |
1975 | break; | |
1976 | } | |
1977 | } | |
1978 | ||
1c41c82b | 1979 | static int grep_source_load_oid(struct grep_source *gs) |
e1327023 JK |
1980 | { |
1981 | enum object_type type; | |
1982 | ||
b4f5aca4 | 1983 | gs->buf = read_object_file(gs->identifier, &type, &gs->size); |
e1327023 JK |
1984 | if (!gs->buf) |
1985 | return error(_("'%s': unable to read %s"), | |
1986 | gs->name, | |
1c41c82b | 1987 | oid_to_hex(gs->identifier)); |
e1327023 JK |
1988 | return 0; |
1989 | } | |
1990 | ||
1991 | static int grep_source_load_file(struct grep_source *gs) | |
1992 | { | |
1993 | const char *filename = gs->identifier; | |
1994 | struct stat st; | |
1995 | char *data; | |
1996 | size_t size; | |
1997 | int i; | |
1998 | ||
1999 | if (lstat(filename, &st) < 0) { | |
2000 | err_ret: | |
2001 | if (errno != ENOENT) | |
7645d8f1 | 2002 | error_errno(_("failed to stat '%s'"), filename); |
e1327023 JK |
2003 | return -1; |
2004 | } | |
2005 | if (!S_ISREG(st.st_mode)) | |
2006 | return -1; | |
2007 | size = xsize_t(st.st_size); | |
2008 | i = open(filename, O_RDONLY); | |
2009 | if (i < 0) | |
2010 | goto err_ret; | |
3733e694 | 2011 | data = xmallocz(size); |
e1327023 | 2012 | if (st.st_size != read_in_full(i, data, size)) { |
7645d8f1 | 2013 | error_errno(_("'%s': short read"), filename); |
e1327023 JK |
2014 | close(i); |
2015 | free(data); | |
2016 | return -1; | |
2017 | } | |
2018 | close(i); | |
e1327023 JK |
2019 | |
2020 | gs->buf = data; | |
2021 | gs->size = size; | |
2022 | return 0; | |
2023 | } | |
2024 | ||
3083301e | 2025 | static int grep_source_load(struct grep_source *gs) |
e1327023 JK |
2026 | { |
2027 | if (gs->buf) | |
2028 | return 0; | |
2029 | ||
2030 | switch (gs->type) { | |
2031 | case GREP_SOURCE_FILE: | |
2032 | return grep_source_load_file(gs); | |
1c41c82b BW |
2033 | case GREP_SOURCE_OID: |
2034 | return grep_source_load_oid(gs); | |
e1327023 JK |
2035 | case GREP_SOURCE_BUF: |
2036 | return gs->buf ? 0 : -1; | |
2037 | } | |
033abf97 | 2038 | BUG("invalid grep_source type to load"); |
0ab7befa | 2039 | } |
94ad9d9e | 2040 | |
acd00ea0 NTND |
2041 | void grep_source_load_driver(struct grep_source *gs, |
2042 | struct index_state *istate) | |
94ad9d9e JK |
2043 | { |
2044 | if (gs->driver) | |
2045 | return; | |
2046 | ||
2047 | grep_attr_lock(); | |
1d1729ca | 2048 | if (gs->path) |
acd00ea0 | 2049 | gs->driver = userdiff_find_by_path(istate, gs->path); |
94ad9d9e JK |
2050 | if (!gs->driver) |
2051 | gs->driver = userdiff_find_by_name("default"); | |
2052 | grep_attr_unlock(); | |
2053 | } | |
41b59bfc | 2054 | |
acd00ea0 NTND |
2055 | static int grep_source_is_binary(struct grep_source *gs, |
2056 | struct index_state *istate) | |
41b59bfc | 2057 | { |
acd00ea0 | 2058 | grep_source_load_driver(gs, istate); |
41b59bfc JK |
2059 | if (gs->driver->binary != -1) |
2060 | return gs->driver->binary; | |
2061 | ||
2062 | if (!grep_source_load(gs)) | |
2063 | return buffer_is_binary(gs->buf, gs->size); | |
2064 | ||
2065 | return 0; | |
2066 | } |