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