🌐 AI搜索 & 代理 主页
blob: c7e1dc1e0ee4fea88f9e768ea04401320a497ea9 [file] [log] [blame]
Patrick Steinhardt41f43b82024-12-06 10:27:191#define DISABLE_SIGN_COMPARE_WARNINGS
2
Elijah Newren4f6728d2023-03-21 06:25:563#include "git-compat-util.h"
Brandon Williamsb2141fc2017-06-14 18:07:364#include "config.h"
Elijah Newrenf394e092023-03-21 06:25:545#include "gettext.h"
Junio C Hamano83b5d2f2006-09-17 23:02:526#include "grep.h"
Elijah Newren41771fa2023-02-24 00:09:277#include "hex.h"
Patrick Steinhardt8f491512025-07-01 12:22:158#include "odb.h"
Elijah Newrend4a4f922023-04-22 20:17:269#include "pretty.h"
René Scharfe60ecac92009-07-01 22:07:2410#include "userdiff.h"
Johannes Schindelin6bfce932007-06-05 02:36:1111#include "xdiff-interface.h"
Jeff King335ec3b2013-05-10 15:10:1512#include "diff.h"
13#include "diffcore.h"
Nguyễn Thái Ngọc Duy793dc672016-06-25 05:22:3114#include "quote.h"
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 13:55:2415#include "help.h"
Junio C Hamano83b5d2f2006-09-17 23:02:5216
Junio C Hamano07a7d652012-09-15 21:04:3617static int grep_source_load(struct grep_source *gs);
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:3318static int grep_source_is_binary(struct grep_source *gs,
19 struct index_state *istate);
Junio C Hamano07a7d652012-09-15 21:04:3620
Jeff Kingbcba4462023-08-29 23:45:2721static void std_output(struct grep_opt *opt UNUSED, const void *buf, size_t size)
Martin Ågren96313422020-11-21 18:31:0822{
23 fwrite(buf, size, 1, stdout);
24}
25
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:2226static const char *color_grep_slots[] = {
27 [GREP_COLOR_CONTEXT] = "context",
28 [GREP_COLOR_FILENAME] = "filename",
29 [GREP_COLOR_FUNCTION] = "function",
30 [GREP_COLOR_LINENO] = "lineNumber",
Junio C Hamanod036d662018-07-18 19:20:3131 [GREP_COLOR_COLUMNNO] = "column",
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:2232 [GREP_COLOR_MATCH_CONTEXT] = "matchContext",
33 [GREP_COLOR_MATCH_SELECTED] = "matchSelected",
34 [GREP_COLOR_SELECTED] = "selected",
35 [GREP_COLOR_SEP] = "separator",
36};
37
Junio C Hamano7687a052012-10-09 23:17:5038static int parse_pattern_type_arg(const char *opt, const char *arg)
39{
40 if (!strcmp(arg, "default"))
41 return GREP_PATTERN_TYPE_UNSPECIFIED;
42 else if (!strcmp(arg, "basic"))
43 return GREP_PATTERN_TYPE_BRE;
44 else if (!strcmp(arg, "extended"))
45 return GREP_PATTERN_TYPE_ERE;
46 else if (!strcmp(arg, "fixed"))
47 return GREP_PATTERN_TYPE_FIXED;
48 else if (!strcmp(arg, "perl"))
49 return GREP_PATTERN_TYPE_PCRE;
50 die("bad %s argument: %s", opt, arg);
51}
52
Nguyễn Thái Ngọc Duy3ac68a92018-05-26 13:55:2453define_list_config_array_extra(color_grep_slots, {"match"});
54
Junio C Hamano7687a052012-10-09 23:17:5055/*
56 * Read the configuration file once and store it in
57 * the grep_defaults template.
58 */
Glen Chooa4e7e312023-06-28 19:26:2259int grep_config(const char *var, const char *value,
60 const struct config_context *ctx, void *cb)
Junio C Hamano7687a052012-10-09 23:17:5061{
Ævar Arnfjörð Bjarmason72365bb2022-02-16 00:00:3662 struct grep_opt *opt = cb;
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:2263 const char *slot;
Junio C Hamano7687a052012-10-09 23:17:5064
65 if (userdiff_config(var, value) < 0)
66 return -1;
67
68 if (!strcmp(var, "grep.extendedregexp")) {
Ævar Arnfjörð Bjarmasonc7e38552017-06-29 22:22:1869 opt->extended_regexp_option = git_config_bool(var, value);
Junio C Hamano7687a052012-10-09 23:17:5070 return 0;
71 }
72
73 if (!strcmp(var, "grep.patterntype")) {
74 opt->pattern_type_option = parse_pattern_type_arg(var, value);
75 return 0;
76 }
77
78 if (!strcmp(var, "grep.linenumber")) {
79 opt->linenum = git_config_bool(var, value);
80 return 0;
81 }
Taylor Blau6653fec2018-06-22 15:49:4982 if (!strcmp(var, "grep.column")) {
83 opt->columnnum = git_config_bool(var, value);
84 return 0;
85 }
Junio C Hamano7687a052012-10-09 23:17:5086
Andreas Schwab6453f7b2014-03-17 19:16:0587 if (!strcmp(var, "grep.fullname")) {
88 opt->relative = !git_config_bool(var, value);
89 return 0;
90 }
91
Junio C Hamano7687a052012-10-09 23:17:5092 if (!strcmp(var, "color.grep"))
93 opt->color = git_config_colorbool(var, value);
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:2294 if (!strcmp(var, "color.grep.match")) {
Glen Chooa4e7e312023-06-28 19:26:2295 if (grep_config("color.grep.matchcontext", value, ctx, cb) < 0)
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:2296 return -1;
Glen Chooa4e7e312023-06-28 19:26:2297 if (grep_config("color.grep.matchselected", value, ctx, cb) < 0)
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:2298 return -1;
99 } else if (skip_prefix(var, "color.grep.", &slot)) {
100 int i = LOOKUP_CONFIG(color_grep_slots, slot);
101 char *color;
Junio C Hamano7687a052012-10-09 23:17:50102
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:22103 if (i < 0)
104 return -1;
105 color = opt->colors[i];
Junio C Hamano7687a052012-10-09 23:17:50106 if (!value)
107 return config_error_nonbool(var);
Jeff Kingf6c5a292014-10-07 19:33:09108 return color_parse(value, color);
Junio C Hamano7687a052012-10-09 23:17:50109 }
110 return 0;
111}
112
Ævar Arnfjörð Bjarmason9725c8d2022-02-16 00:00:34113void grep_init(struct grep_opt *opt, struct repository *repo)
Junio C Hamano7687a052012-10-09 23:17:50114{
Ævar Arnfjörð Bjarmason72365bb2022-02-16 00:00:36115 struct grep_opt blank = GREP_OPT_INIT;
116 memcpy(opt, &blank, sizeof(*opt));
Martin Ågren6ba9bb72020-11-29 19:52:21117
Nguyễn Thái Ngọc Duy38bbc2e2018-09-21 15:57:23118 opt->repo = repo;
Junio C Hamano7687a052012-10-09 23:17:50119 opt->pattern_tail = &opt->pattern_list;
120 opt->header_tail = &opt->header_list;
Junio C Hamano7687a052012-10-09 23:17:50121}
Junio C Hamano07a7d652012-09-15 21:04:36122
René Scharfefc456752012-05-20 14:32:39123static struct grep_pat *create_grep_pat(const char *pat, size_t patlen,
124 const char *origin, int no,
125 enum grep_pat_token t,
126 enum grep_header_field field)
Junio C Hamanoa4d7d2c2008-09-05 05:15:02127{
128 struct grep_pat *p = xcalloc(1, sizeof(*p));
René Scharfe526a8582012-05-20 14:33:07129 p->pattern = xmemdupz(pat, patlen);
René Scharfefc456752012-05-20 14:32:39130 p->patternlen = patlen;
131 p->origin = origin;
132 p->no = no;
133 p->token = t;
Junio C Hamanoa4d7d2c2008-09-05 05:15:02134 p->field = field;
René Scharfefc456752012-05-20 14:32:39135 return p;
136}
137
René Scharfe2b3873f2012-05-20 14:32:54138static void do_append_grep_pat(struct grep_pat ***tail, struct grep_pat *p)
139{
140 **tail = p;
141 *tail = &p->next;
Junio C Hamanoa4d7d2c2008-09-05 05:15:02142 p->next = NULL;
René Scharfe526a8582012-05-20 14:33:07143
144 switch (p->token) {
145 case GREP_PATTERN: /* atom */
146 case GREP_PATTERN_HEAD:
147 case GREP_PATTERN_BODY:
148 for (;;) {
149 struct grep_pat *new_pat;
150 size_t len = 0;
151 char *cp = p->pattern + p->patternlen, *nl = NULL;
152 while (++len <= p->patternlen) {
153 if (*(--cp) == '\n') {
154 nl = cp;
155 break;
156 }
157 }
158 if (!nl)
159 break;
160 new_pat = create_grep_pat(nl + 1, len - 1, p->origin,
161 p->no, p->token, p->field);
162 new_pat->next = p->next;
163 if (!p->next)
164 *tail = &new_pat->next;
165 p->next = new_pat;
166 *nl = '\0';
167 p->patternlen -= len;
168 }
169 break;
170 default:
171 break;
172 }
René Scharfe2b3873f2012-05-20 14:32:54173}
174
René Scharfefc456752012-05-20 14:32:39175void append_header_grep_pattern(struct grep_opt *opt,
176 enum grep_header_field field, const char *pat)
177{
178 struct grep_pat *p = create_grep_pat(pat, strlen(pat), "header", 0,
179 GREP_PATTERN_HEAD, field);
Junio C Hamanobaa63782012-09-29 18:59:52180 if (field == GREP_HEADER_REFLOG)
181 opt->use_reflog_filter = 1;
René Scharfe2b3873f2012-05-20 14:32:54182 do_append_grep_pat(&opt->header_tail, p);
Junio C Hamanoa4d7d2c2008-09-05 05:15:02183}
184
Junio C Hamano83b5d2f2006-09-17 23:02:52185void append_grep_pattern(struct grep_opt *opt, const char *pat,
186 const char *origin, int no, enum grep_pat_token t)
187{
René Scharfeed40a092010-05-22 21:43:43188 append_grep_pat(opt, pat, strlen(pat), origin, no, t);
189}
190
191void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen,
192 const char *origin, int no, enum grep_pat_token t)
193{
René Scharfefc456752012-05-20 14:32:39194 struct grep_pat *p = create_grep_pat(pat, patlen, origin, no, t, 0);
René Scharfe2b3873f2012-05-20 14:32:54195 do_append_grep_pat(&opt->pattern_tail, p);
Junio C Hamano83b5d2f2006-09-17 23:02:52196}
197
Fredrik Kuivinen5b594f42010-01-25 22:51:39198struct grep_opt *grep_opt_dup(const struct grep_opt *opt)
199{
200 struct grep_pat *pat;
201 struct grep_opt *ret = xmalloc(sizeof(struct grep_opt));
202 *ret = *opt;
203
204 ret->pattern_list = NULL;
205 ret->pattern_tail = &ret->pattern_list;
206
207 for(pat = opt->pattern_list; pat != NULL; pat = pat->next)
208 {
209 if(pat->token == GREP_PATTERN_HEAD)
210 append_header_grep_pattern(ret, pat->field,
211 pat->pattern);
212 else
René Scharfeed40a092010-05-22 21:43:43213 append_grep_pat(ret, pat->pattern, pat->patternlen,
214 pat->origin, pat->no, pat->token);
Fredrik Kuivinen5b594f42010-01-25 22:51:39215 }
216
217 return ret;
218}
219
Michał Kiedrowicza30c1482011-05-09 21:52:04220static NORETURN void compile_regexp_failed(const struct grep_pat *p,
221 const char *error)
222{
223 char where[1024];
224
225 if (p->no)
Jeff King19bdd3e2015-09-24 21:06:51226 xsnprintf(where, sizeof(where), "In '%s' at %d, ", p->origin, p->no);
Michał Kiedrowicza30c1482011-05-09 21:52:04227 else if (p->origin)
Jeff King19bdd3e2015-09-24 21:06:51228 xsnprintf(where, sizeof(where), "%s, ", p->origin);
Michał Kiedrowicza30c1482011-05-09 21:52:04229 else
230 where[0] = 0;
231
232 die("%s'%s': %s", where, p->pattern, error);
233}
234
Ævar Arnfjörð Bjarmason543f1c02017-05-25 19:45:30235static int is_fixed(const char *s, size_t len)
236{
237 size_t i;
238
239 for (i = 0; i < len; i++) {
240 if (is_regex_special(s[i]))
241 return 0;
242 }
243
244 return 1;
245}
246
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56247#ifdef USE_LIBPCRE2
Ævar Arnfjörð Bjarmasonc1760352021-02-18 00:07:28248#define GREP_PCRE2_DEBUG_MALLOC 0
249
Jeff King516a9ec2024-08-29 20:09:53250static void *pcre2_malloc(PCRE2_SIZE size, void *memory_data UNUSED)
Ævar Arnfjörð Bjarmasonc1760352021-02-18 00:07:28251{
252 void *pointer = malloc(size);
253#if GREP_PCRE2_DEBUG_MALLOC
254 static int count = 1;
255 fprintf(stderr, "PCRE2:%p -> #%02d: alloc(%lu)\n", pointer, count++, size);
256#endif
257 return pointer;
258}
259
Jeff King516a9ec2024-08-29 20:09:53260static void pcre2_free(void *pointer, void *memory_data UNUSED)
Ævar Arnfjörð Bjarmasonc1760352021-02-18 00:07:28261{
262#if GREP_PCRE2_DEBUG_MALLOC
263 static int count = 1;
264 if (pointer)
265 fprintf(stderr, "PCRE2:%p -> #%02d: free()\n", pointer, count++);
266#endif
267 free(pointer);
268}
269
Mathias Krause50b6ad52023-01-31 18:56:11270static int pcre2_jit_functional(void)
271{
272 static int jit_working = -1;
273 pcre2_code *code;
274 size_t off;
275 int err;
276
277 if (jit_working != -1)
278 return jit_working;
279
280 /*
281 * Try to JIT compile a simple pattern to probe if the JIT is
282 * working in general. It might fail for systems where creating
283 * memory mappings for runtime code generation is restricted.
284 */
285 code = pcre2_compile((PCRE2_SPTR)".", 1, 0, &err, &off, NULL);
286 if (!code)
287 return 0;
288
289 jit_working = pcre2_jit_compile(code, PCRE2_JIT_COMPLETE) == 0;
290 pcre2_code_free(code);
291
292 return jit_working;
293}
294
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56295static void compile_pcre2_pattern(struct grep_pat *p, const struct grep_opt *opt)
296{
297 int error;
298 PCRE2_UCHAR errbuf[256];
299 PCRE2_SIZE erroffset;
300 int options = PCRE2_MULTILINE;
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56301 int jitret;
Ævar Arnfjörð Bjarmasona25b9082017-11-23 14:16:58302 int patinforet;
303 size_t jitsizearg;
René Scharfe32e3e8b2021-12-18 19:53:15304 int literal = !opt->ignore_case && (p->fixed || p->is_fixed);
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56305
Ævar Arnfjörð Bjarmasoncbe81e62021-02-18 00:07:27306 /*
307 * Call pcre2_general_context_create() before calling any
308 * other pcre2_*(). It sets up our malloc()/free() functions
309 * with which everything else is allocated.
310 */
311 p->pcre2_general_context = pcre2_general_context_create(
312 pcre2_malloc, pcre2_free, NULL);
313 if (!p->pcre2_general_context)
314 die("Couldn't allocate PCRE2 general context");
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56315
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56316 if (opt->ignore_case) {
Ævar Arnfjörð Bjarmason44570182019-06-27 23:39:05317 if (!opt->ignore_locale && has_non_ascii(p->pattern)) {
Ævar Arnfjörð Bjarmasoncbe81e62021-02-18 00:07:27318 p->pcre2_tables = pcre2_maketables(p->pcre2_general_context);
319 p->pcre2_compile_context = pcre2_compile_context_create(p->pcre2_general_context);
Carlo Marcelo Arenas Belón10da0302019-10-16 12:10:24320 pcre2_set_character_tables(p->pcre2_compile_context,
321 p->pcre2_tables);
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56322 }
323 options |= PCRE2_CASELESS;
324 }
René Scharfe32e3e8b2021-12-18 19:53:15325 if (!opt->ignore_locale && is_utf8_locale() && !literal)
Carlo Marcelo Arenas Belónacabd202023-01-08 15:52:17326 options |= (PCRE2_UTF | PCRE2_UCP | PCRE2_MATCH_INVALID_UTF);
Ævar Arnfjörð Bjarmason95ca1f92021-01-24 17:28:13327
Mathias Krause14b9a042023-03-23 17:25:39328#ifndef GIT_PCRE2_VERSION_10_35_OR_HIGHER
329 /*
330 * Work around a JIT bug related to invalid Unicode character handling
331 * fixed in 10.35:
332 * https://github.com/PCRE2Project/pcre2/commit/c21bd977547d
333 */
334 options &= ~PCRE2_UCP;
335#endif
336
René Scharfe97169fc2022-02-17 21:14:29337#ifndef GIT_PCRE2_VERSION_10_36_OR_HIGHER
Ævar Arnfjörð Bjarmason95ca1f92021-01-24 17:28:13338 /* Work around https://bugs.exim.org/show_bug.cgi?id=2642 fixed in 10.36 */
Ævar Arnfjörð Bjarmason797c3592021-02-18 00:07:24339 if (PCRE2_MATCH_INVALID_UTF && options & (PCRE2_UTF | PCRE2_CASELESS))
340 options |= PCRE2_NO_START_OPTIMIZE;
341#endif
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56342
343 p->pcre2_pattern = pcre2_compile((PCRE2_SPTR)p->pattern,
344 p->patternlen, options, &error, &erroffset,
345 p->pcre2_compile_context);
346
347 if (p->pcre2_pattern) {
Ævar Arnfjörð Bjarmasoncbe81e62021-02-18 00:07:27348 p->pcre2_match_data = pcre2_match_data_create_from_pattern(p->pcre2_pattern, p->pcre2_general_context);
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56349 if (!p->pcre2_match_data)
350 die("Couldn't allocate PCRE2 match data");
351 } else {
352 pcre2_get_error_message(error, errbuf, sizeof(errbuf));
353 compile_regexp_failed(p, (const char *)&errbuf);
354 }
355
356 pcre2_config(PCRE2_CONFIG_JIT, &p->pcre2_jit_on);
Ævar Arnfjörð Bjarmason04bef502019-07-26 15:08:11357 if (p->pcre2_jit_on) {
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56358 jitret = pcre2_jit_compile(p->pcre2_pattern, PCRE2_JIT_COMPLETE);
Mathias Krause50b6ad52023-01-31 18:56:11359 if (jitret == PCRE2_ERROR_NOMEMORY && !pcre2_jit_functional()) {
360 /*
361 * Even though pcre2_config(PCRE2_CONFIG_JIT, ...)
362 * indicated JIT support, the library might still
363 * fail to generate JIT code for various reasons,
364 * e.g. when SELinux's 'deny_execmem' or PaX's
365 * MPROTECT prevent creating W|X memory mappings.
366 *
367 * Instead of faling hard, fall back to interpreter
368 * mode, just as if the pattern was prefixed with
369 * '(*NO_JIT)'.
370 */
371 p->pcre2_jit_on = 0;
372 return;
373 } else if (jitret) {
374 int need_clip = p->patternlen > 64;
375 int clip_len = need_clip ? 64 : p->patternlen;
376 die("Couldn't JIT the PCRE2 pattern '%.*s'%s, got '%d'%s",
377 clip_len, p->pattern, need_clip ? "..." : "", jitret,
378 pcre2_jit_functional()
379 ? "\nPerhaps prefix (*NO_JIT) to your pattern?"
380 : "");
381 }
Ævar Arnfjörð Bjarmasona25b9082017-11-23 14:16:58382
383 /*
384 * The pcre2_config(PCRE2_CONFIG_JIT, ...) call just
385 * tells us whether the library itself supports JIT,
386 * but to see whether we're going to be actually using
387 * JIT we need to extract PCRE2_INFO_JITSIZE from the
388 * pattern *after* we do pcre2_jit_compile() above.
389 *
390 * This is because if the pattern contains the
391 * (*NO_JIT) verb (see pcre2syntax(3))
392 * pcre2_jit_compile() will exit early with 0. If we
393 * then proceed to call pcre2_jit_match() further down
394 * the line instead of pcre2_match() we'll either
395 * segfault (pre PCRE 10.31) or run into a fatal error
396 * (post PCRE2 10.31)
397 */
398 patinforet = pcre2_pattern_info(p->pcre2_pattern, PCRE2_INFO_JITSIZE, &jitsizearg);
399 if (patinforet)
400 BUG("pcre2_pattern_info() failed: %d", patinforet);
401 if (jitsizearg == 0) {
402 p->pcre2_jit_on = 0;
403 return;
404 }
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56405 }
406}
407
408static int pcre2match(struct grep_pat *p, const char *line, const char *eol,
409 regmatch_t *match, int eflags)
410{
411 int ret, flags = 0;
412 PCRE2_SIZE *ovector;
413 PCRE2_UCHAR errbuf[256];
414
415 if (eflags & REG_NOTBOL)
416 flags |= PCRE2_NOTBOL;
417
418 if (p->pcre2_jit_on)
419 ret = pcre2_jit_match(p->pcre2_pattern, (unsigned char *)line,
420 eol - line, 0, flags, p->pcre2_match_data,
421 NULL);
422 else
423 ret = pcre2_match(p->pcre2_pattern, (unsigned char *)line,
424 eol - line, 0, flags, p->pcre2_match_data,
425 NULL);
426
427 if (ret < 0 && ret != PCRE2_ERROR_NOMATCH) {
428 pcre2_get_error_message(ret, errbuf, sizeof(errbuf));
429 die("%s failed with error code %d: %s",
430 (p->pcre2_jit_on ? "pcre2_jit_match" : "pcre2_match"), ret,
431 errbuf);
432 }
433 if (ret > 0) {
434 ovector = pcre2_get_ovector_pointer(p->pcre2_match_data);
435 ret = 0;
436 match->rm_so = (int)ovector[0];
437 match->rm_eo = (int)ovector[1];
438 }
439
440 return ret;
441}
442
443static void free_pcre2_pattern(struct grep_pat *p)
444{
445 pcre2_compile_context_free(p->pcre2_compile_context);
446 pcre2_code_free(p->pcre2_pattern);
447 pcre2_match_data_free(p->pcre2_match_data);
Ævar Arnfjörð Bjarmasonb76bf272021-02-18 00:07:25448#ifdef GIT_PCRE2_VERSION_10_34_OR_HIGHER
Ævar Arnfjörð Bjarmasoncbe81e62021-02-18 00:07:27449 pcre2_maketables_free(p->pcre2_general_context, p->pcre2_tables);
Ævar Arnfjörð Bjarmasonb76bf272021-02-18 00:07:25450#else
Carlo Marcelo Arenas Belón10da0302019-10-16 12:10:24451 free((void *)p->pcre2_tables);
Ævar Arnfjörð Bjarmasonb76bf272021-02-18 00:07:25452#endif
Ævar Arnfjörð Bjarmasoncbe81e62021-02-18 00:07:27453 pcre2_general_context_free(p->pcre2_general_context);
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56454}
455#else /* !USE_LIBPCRE2 */
Jeff King4548b012023-08-29 23:45:34456static void compile_pcre2_pattern(struct grep_pat *p UNUSED,
457 const struct grep_opt *opt UNUSED)
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56458{
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56459 die("cannot use Perl-compatible regexes when not compiled with USE_LIBPCRE");
460}
461
Jeff King4548b012023-08-29 23:45:34462static int pcre2match(struct grep_pat *p UNUSED, const char *line UNUSED,
463 const char *eol UNUSED, regmatch_t *match UNUSED,
464 int eflags UNUSED)
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56465{
466 return 1;
467}
468
Jeff King4548b012023-08-29 23:45:34469static void free_pcre2_pattern(struct grep_pat *p UNUSED)
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56470{
471}
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56472
Nguyễn Thái Ngọc Duy793dc672016-06-25 05:22:31473static void compile_fixed_regexp(struct grep_pat *p, struct grep_opt *opt)
474{
475 struct strbuf sb = STRBUF_INIT;
476 int err;
Ævar Arnfjörð Bjarmason1ceabab2017-06-29 22:22:22477 int regflags = 0;
Nguyễn Thái Ngọc Duy793dc672016-06-25 05:22:31478
479 basic_regex_quote_buf(&sb, p->pattern);
Nguyễn Thái Ngọc Duy793dc672016-06-25 05:22:31480 if (opt->ignore_case)
481 regflags |= REG_ICASE;
482 err = regcomp(&p->regexp, sb.buf, regflags);
Nguyễn Thái Ngọc Duy793dc672016-06-25 05:22:31483 strbuf_release(&sb);
484 if (err) {
485 char errbuf[1024];
486 regerror(err, &p->regexp, errbuf, sizeof(errbuf));
Nguyễn Thái Ngọc Duy793dc672016-06-25 05:22:31487 compile_regexp_failed(p, errbuf);
488 }
489}
Ævar Arnfjörð Bjarmasonb65abca2019-07-01 21:21:00490#endif /* !USE_LIBPCRE2 */
Nguyễn Thái Ngọc Duy793dc672016-06-25 05:22:31491
Junio C Hamano83b5d2f2006-09-17 23:02:52492static void compile_regexp(struct grep_pat *p, struct grep_opt *opt)
493{
René Scharfec8222552009-01-09 23:18:34494 int err;
Ævar Arnfjörð Bjarmason07a3d412017-06-29 22:22:21495 int regflags = REG_NEWLINE;
René Scharfec8222552009-01-09 23:18:34496
Ævar Arnfjörð Bjarmason04bf0522022-02-16 00:00:39497 if (opt->pattern_type_option == GREP_PATTERN_TYPE_UNSPECIFIED)
498 opt->pattern_type_option = (opt->extended_regexp_option
499 ? GREP_PATTERN_TYPE_ERE
500 : GREP_PATTERN_TYPE_BRE);
501
René Scharfed7eb5272009-03-07 12:28:40502 p->word_regexp = opt->word_regexp;
Brian Collins5183bf62009-11-06 09:22:35503 p->ignore_case = opt->ignore_case;
Ævar Arnfjörð Bjarmason04bf0522022-02-16 00:00:39504 p->fixed = opt->pattern_type_option == GREP_PATTERN_TYPE_FIXED;
René Scharfed7eb5272009-03-07 12:28:40505
Ævar Arnfjörð Bjarmason04bf0522022-02-16 00:00:39506 if (opt->pattern_type_option != GREP_PATTERN_TYPE_PCRE &&
Ævar Arnfjörð Bjarmasonae807d72022-02-16 00:00:38507 memchr(p->pattern, 0, p->patternlen))
Ævar Arnfjörð Bjarmason45d1f372019-07-01 21:20:58508 die(_("given pattern contains NULL byte (via -f <file>). This is only supported with -P under PCRE v2"));
Fredrik Kuivinen9ecedde2011-08-20 22:42:18509
Ævar Arnfjörð Bjarmason09872f62019-07-26 15:08:15510 p->is_fixed = is_fixed(p->pattern, p->patternlen);
Ævar Arnfjörð Bjarmason8a599982019-07-26 15:08:16511#ifdef USE_LIBPCRE2
512 if (!p->fixed && !p->is_fixed) {
513 const char *no_jit = "(*NO_JIT)";
514 const int no_jit_len = strlen(no_jit);
515 if (starts_with(p->pattern, no_jit) &&
516 is_fixed(p->pattern + no_jit_len,
517 p->patternlen - no_jit_len))
518 p->is_fixed = 1;
519 }
520#endif
Ævar Arnfjörð Bjarmason09872f62019-07-26 15:08:15521 if (p->fixed || p->is_fixed) {
Ævar Arnfjörð Bjarmasonb65abca2019-07-01 21:21:00522#ifdef USE_LIBPCRE2
Ævar Arnfjörð Bjarmason09872f62019-07-26 15:08:15523 if (p->is_fixed) {
Ævar Arnfjörð Bjarmasonb65abca2019-07-01 21:21:00524 compile_pcre2_pattern(p, opt);
525 } else {
526 /*
527 * E.g. t7811-grep-open.sh relies on the
528 * pattern being restored.
529 */
530 char *old_pattern = p->pattern;
531 size_t old_patternlen = p->patternlen;
532 struct strbuf sb = STRBUF_INIT;
533
534 /*
535 * There is the PCRE2_LITERAL flag, but it's
536 * only in PCRE v2 10.30 and later. Needing to
537 * ifdef our way around that and dealing with
538 * it + PCRE2_MULTILINE being an error is more
539 * complex than just quoting this ourselves.
540 */
541 strbuf_add(&sb, "\\Q", 2);
542 strbuf_add(&sb, p->pattern, p->patternlen);
543 strbuf_add(&sb, "\\E", 2);
544
545 p->pattern = sb.buf;
546 p->patternlen = sb.len;
547 compile_pcre2_pattern(p, opt);
548 p->pattern = old_pattern;
549 p->patternlen = old_patternlen;
550 strbuf_release(&sb);
551 }
552#else /* !USE_LIBPCRE2 */
Nguyễn Thái Ngọc Duy793dc672016-06-25 05:22:31553 compile_fixed_regexp(p, opt);
Ævar Arnfjörð Bjarmasonb65abca2019-07-01 21:21:00554#endif /* !USE_LIBPCRE2 */
Nguyễn Thái Ngọc Duy793dc672016-06-25 05:22:31555 return;
Fredrik Kuivinen9ecedde2011-08-20 22:42:18556 }
René Scharfec8222552009-01-09 23:18:34557
Ævar Arnfjörð Bjarmason04bf0522022-02-16 00:00:39558 if (opt->pattern_type_option == GREP_PATTERN_TYPE_PCRE) {
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56559 compile_pcre2_pattern(p, opt);
560 return;
561 }
562
Ævar Arnfjörð Bjarmason07a3d412017-06-29 22:22:21563 if (p->ignore_case)
564 regflags |= REG_ICASE;
Ævar Arnfjörð Bjarmason04bf0522022-02-16 00:00:39565 if (opt->pattern_type_option == GREP_PATTERN_TYPE_ERE)
Ævar Arnfjörð Bjarmason07a3d412017-06-29 22:22:21566 regflags |= REG_EXTENDED;
567 err = regcomp(&p->regexp, p->pattern, regflags);
Junio C Hamano83b5d2f2006-09-17 23:02:52568 if (err) {
569 char errbuf[1024];
Junio C Hamano83b5d2f2006-09-17 23:02:52570 regerror(err, &p->regexp, errbuf, 1024);
Michał Kiedrowicza30c1482011-05-09 21:52:04571 compile_regexp_failed(p, errbuf);
Junio C Hamano83b5d2f2006-09-17 23:02:52572 }
573}
574
René Scharfee2b15422022-01-06 09:54:19575static struct grep_expr *grep_not_expr(struct grep_expr *expr)
576{
577 struct grep_expr *z = xcalloc(1, sizeof(*z));
578 z->node = GREP_NODE_NOT;
579 z->u.unary = expr;
580 return z;
581}
582
Taylor Blauf2d27592022-01-06 19:50:12583static struct grep_expr *grep_binexp(enum grep_expr_node kind,
584 struct grep_expr *left,
585 struct grep_expr *right)
René Scharfe9dbf00b2022-01-06 09:51:00586{
587 struct grep_expr *z = xcalloc(1, sizeof(*z));
Taylor Blauf2d27592022-01-06 19:50:12588 z->node = kind;
René Scharfe9dbf00b2022-01-06 09:51:00589 z->u.binary.left = left;
590 z->u.binary.right = right;
591 return z;
592}
593
Taylor Blauf2d27592022-01-06 19:50:12594static struct grep_expr *grep_or_expr(struct grep_expr *left, struct grep_expr *right)
595{
596 return grep_binexp(GREP_NODE_OR, left, right);
597}
598
Taylor Blau0a6adc22022-01-06 19:50:15599static struct grep_expr *grep_and_expr(struct grep_expr *left, struct grep_expr *right)
600{
601 return grep_binexp(GREP_NODE_AND, left, right);
602}
603
Junio C Hamano0ab7bef2006-09-28 00:50:52604static struct grep_expr *compile_pattern_or(struct grep_pat **);
Junio C Hamano83b5d2f2006-09-17 23:02:52605static struct grep_expr *compile_pattern_atom(struct grep_pat **list)
606{
607 struct grep_pat *p;
608 struct grep_expr *x;
609
610 p = *list;
Linus Torvaldsc922b012009-04-27 18:10:24611 if (!p)
612 return NULL;
Junio C Hamano83b5d2f2006-09-17 23:02:52613 switch (p->token) {
614 case GREP_PATTERN: /* atom */
Junio C Hamano480c1ca2006-09-20 19:39:46615 case GREP_PATTERN_HEAD:
616 case GREP_PATTERN_BODY:
René Scharfeca56dad2021-03-13 16:17:22617 CALLOC_ARRAY(x, 1);
Junio C Hamano83b5d2f2006-09-17 23:02:52618 x->node = GREP_NODE_ATOM;
619 x->u.atom = p;
620 *list = p->next;
621 return x;
622 case GREP_OPEN_PAREN:
623 *list = p->next;
Junio C Hamano0ab7bef2006-09-28 00:50:52624 x = compile_pattern_or(list);
Junio C Hamano83b5d2f2006-09-17 23:02:52625 if (!*list || (*list)->token != GREP_CLOSE_PAREN)
Ahelenia Ziemiańska0d527842024-03-23 13:18:08626 die("unmatched ( for expression group");
Junio C Hamano83b5d2f2006-09-17 23:02:52627 *list = (*list)->next;
628 return x;
629 default:
630 return NULL;
631 }
632}
633
634static struct grep_expr *compile_pattern_not(struct grep_pat **list)
635{
636 struct grep_pat *p;
637 struct grep_expr *x;
638
639 p = *list;
Linus Torvaldsc922b012009-04-27 18:10:24640 if (!p)
641 return NULL;
Junio C Hamano83b5d2f2006-09-17 23:02:52642 switch (p->token) {
643 case GREP_NOT:
644 if (!p->next)
645 die("--not not followed by pattern expression");
646 *list = p->next;
René Scharfee2b15422022-01-06 09:54:19647 x = compile_pattern_not(list);
648 if (!x)
Junio C Hamano83b5d2f2006-09-17 23:02:52649 die("--not followed by non pattern expression");
René Scharfee2b15422022-01-06 09:54:19650 return grep_not_expr(x);
Junio C Hamano83b5d2f2006-09-17 23:02:52651 default:
652 return compile_pattern_atom(list);
653 }
654}
655
656static struct grep_expr *compile_pattern_and(struct grep_pat **list)
657{
658 struct grep_pat *p;
Taylor Blau0a6adc22022-01-06 19:50:15659 struct grep_expr *x, *y;
Junio C Hamano83b5d2f2006-09-17 23:02:52660
661 x = compile_pattern_not(list);
662 p = *list;
663 if (p && p->token == GREP_AND) {
René Scharfefe7fe622021-06-30 16:12:43664 if (!x)
665 die("--and not preceded by pattern expression");
Junio C Hamano83b5d2f2006-09-17 23:02:52666 if (!p->next)
667 die("--and not followed by pattern expression");
668 *list = p->next;
669 y = compile_pattern_and(list);
670 if (!y)
671 die("--and not followed by pattern expression");
Taylor Blau0a6adc22022-01-06 19:50:15672 return grep_and_expr(x, y);
Junio C Hamano83b5d2f2006-09-17 23:02:52673 }
674 return x;
675}
676
677static struct grep_expr *compile_pattern_or(struct grep_pat **list)
678{
679 struct grep_pat *p;
René Scharfe9dbf00b2022-01-06 09:51:00680 struct grep_expr *x, *y;
Junio C Hamano83b5d2f2006-09-17 23:02:52681
682 x = compile_pattern_and(list);
683 p = *list;
684 if (x && p && p->token != GREP_CLOSE_PAREN) {
685 y = compile_pattern_or(list);
686 if (!y)
687 die("not a pattern expression %s", p->pattern);
René Scharfe9dbf00b2022-01-06 09:51:00688 return grep_or_expr(x, y);
Junio C Hamano83b5d2f2006-09-17 23:02:52689 }
690 return x;
691}
692
693static struct grep_expr *compile_pattern_expr(struct grep_pat **list)
694{
695 return compile_pattern_or(list);
696}
697
Junio C Hamano5aaeb732010-09-13 05:15:35698static struct grep_expr *grep_true_expr(void)
699{
700 struct grep_expr *z = xcalloc(1, sizeof(*z));
701 z->node = GREP_NODE_TRUE;
702 return z;
703}
704
Junio C Hamano95ce9ce2010-09-13 02:30:48705static struct grep_expr *prep_header_patterns(struct grep_opt *opt)
706{
707 struct grep_pat *p;
708 struct grep_expr *header_expr;
Junio C Hamano5aaeb732010-09-13 05:15:35709 struct grep_expr *(header_group[GREP_HEADER_FIELD_MAX]);
710 enum grep_header_field fld;
Junio C Hamano95ce9ce2010-09-13 02:30:48711
712 if (!opt->header_list)
713 return NULL;
Angus Hammond2385f242012-05-06 18:17:15714
Junio C Hamano95ce9ce2010-09-13 02:30:48715 for (p = opt->header_list; p; p = p->next) {
716 if (p->token != GREP_PATTERN_HEAD)
Johannes Schindelin033abf92018-05-02 09:38:39717 BUG("a non-header pattern in grep header list.");
Antoine Pelisse3ce3ffb2013-02-03 14:37:09718 if (p->field < GREP_HEADER_FIELD_MIN ||
719 GREP_HEADER_FIELD_MAX <= p->field)
Johannes Schindelin033abf92018-05-02 09:38:39720 BUG("unknown header field %d", p->field);
Junio C Hamano95ce9ce2010-09-13 02:30:48721 compile_regexp(p, opt);
722 }
Junio C Hamano5aaeb732010-09-13 05:15:35723
724 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++)
725 header_group[fld] = NULL;
726
727 for (p = opt->header_list; p; p = p->next) {
728 struct grep_expr *h;
729 struct grep_pat *pp = p;
730
731 h = compile_pattern_atom(&pp);
732 if (!h || pp != p->next)
Johannes Schindelin033abf92018-05-02 09:38:39733 BUG("malformed header expr");
Junio C Hamano5aaeb732010-09-13 05:15:35734 if (!header_group[p->field]) {
735 header_group[p->field] = h;
736 continue;
737 }
738 header_group[p->field] = grep_or_expr(h, header_group[p->field]);
739 }
740
741 header_expr = NULL;
742
743 for (fld = 0; fld < GREP_HEADER_FIELD_MAX; fld++) {
744 if (!header_group[fld])
745 continue;
746 if (!header_expr)
747 header_expr = grep_true_expr();
748 header_expr = grep_or_expr(header_group[fld], header_expr);
749 }
Junio C Hamano95ce9ce2010-09-13 02:30:48750 return header_expr;
751}
752
Junio C Hamano13e4fc72012-09-13 23:26:57753static struct grep_expr *grep_splice_or(struct grep_expr *x, struct grep_expr *y)
754{
755 struct grep_expr *z = x;
756
757 while (x) {
758 assert(x->node == GREP_NODE_OR);
759 if (x->u.binary.right &&
760 x->u.binary.right->node == GREP_NODE_TRUE) {
Patrick Steinhardta6590cc2024-11-05 06:16:50761 free(x->u.binary.right);
Junio C Hamano13e4fc72012-09-13 23:26:57762 x->u.binary.right = y;
763 break;
764 }
765 x = x->u.binary.right;
766 }
767 return z;
768}
769
Ævar Arnfjörð Bjarmason15c96492021-01-25 23:36:51770void compile_grep_patterns(struct grep_opt *opt)
Junio C Hamano83b5d2f2006-09-17 23:02:52771{
772 struct grep_pat *p;
Junio C Hamano95ce9ce2010-09-13 02:30:48773 struct grep_expr *header_expr = prep_header_patterns(opt);
Ævar Arnfjörð Bjarmasondb843762022-10-11 09:48:45774 int extended = 0;
Junio C Hamano0ab7bef2006-09-28 00:50:52775
Junio C Hamano83b5d2f2006-09-17 23:02:52776 for (p = opt->pattern_list; p; p = p->next) {
Junio C Hamano480c1ca2006-09-20 19:39:46777 switch (p->token) {
778 case GREP_PATTERN: /* atom */
779 case GREP_PATTERN_HEAD:
780 case GREP_PATTERN_BODY:
René Scharfec8222552009-01-09 23:18:34781 compile_regexp(p, opt);
Junio C Hamano480c1ca2006-09-20 19:39:46782 break;
783 default:
Ævar Arnfjörð Bjarmasondb843762022-10-11 09:48:45784 extended = 1;
Junio C Hamano480c1ca2006-09-20 19:39:46785 break;
786 }
Junio C Hamano83b5d2f2006-09-17 23:02:52787 }
788
René Scharfe794c0002021-12-17 16:48:49789 if (opt->all_match || opt->no_body_match || header_expr)
Ævar Arnfjörð Bjarmasondb843762022-10-11 09:48:45790 extended = 1;
791 else if (!extended)
Junio C Hamano83b5d2f2006-09-17 23:02:52792 return;
793
Junio C Hamano83b5d2f2006-09-17 23:02:52794 p = opt->pattern_list;
Michele Ballabioba150a32009-03-18 20:53:27795 if (p)
796 opt->pattern_expression = compile_pattern_expr(&p);
Junio C Hamano83b5d2f2006-09-17 23:02:52797 if (p)
Ahelenia Ziemiańska0d527842024-03-23 13:18:08798 die("incomplete pattern expression group: %s", p->pattern);
Junio C Hamano80235ba2010-01-18 04:09:06799
René Scharfe794c0002021-12-17 16:48:49800 if (opt->no_body_match && opt->pattern_expression)
801 opt->pattern_expression = grep_not_expr(opt->pattern_expression);
802
Junio C Hamano80235ba2010-01-18 04:09:06803 if (!header_expr)
804 return;
805
Junio C Hamano5aaeb732010-09-13 05:15:35806 if (!opt->pattern_expression)
Junio C Hamano80235ba2010-01-18 04:09:06807 opt->pattern_expression = header_expr;
Junio C Hamano13e4fc72012-09-13 23:26:57808 else if (opt->all_match)
809 opt->pattern_expression = grep_splice_or(header_expr,
810 opt->pattern_expression);
Junio C Hamano5aaeb732010-09-13 05:15:35811 else
812 opt->pattern_expression = grep_or_expr(opt->pattern_expression,
813 header_expr);
Junio C Hamano80235ba2010-01-18 04:09:06814 opt->all_match = 1;
Junio C Hamano83b5d2f2006-09-17 23:02:52815}
816
Junio C Hamanob48fb5b2006-09-27 23:27:10817static void free_pattern_expr(struct grep_expr *x)
818{
819 switch (x->node) {
Junio C Hamano5aaeb732010-09-13 05:15:35820 case GREP_NODE_TRUE:
Junio C Hamanob48fb5b2006-09-27 23:27:10821 case GREP_NODE_ATOM:
822 break;
823 case GREP_NODE_NOT:
824 free_pattern_expr(x->u.unary);
825 break;
826 case GREP_NODE_AND:
827 case GREP_NODE_OR:
828 free_pattern_expr(x->u.binary.left);
829 free_pattern_expr(x->u.binary.right);
830 break;
831 }
832 free(x);
833}
834
Ævar Arnfjörð Bjarmason891c9962023-02-06 23:07:50835static void free_grep_pat(struct grep_pat *pattern)
Junio C Hamanob48fb5b2006-09-27 23:27:10836{
837 struct grep_pat *p, *n;
838
Ævar Arnfjörð Bjarmason891c9962023-02-06 23:07:50839 for (p = pattern; p; p = n) {
Junio C Hamanob48fb5b2006-09-27 23:27:10840 n = p->next;
841 switch (p->token) {
842 case GREP_PATTERN: /* atom */
843 case GREP_PATTERN_HEAD:
844 case GREP_PATTERN_BODY:
Ævar Arnfjörð Bjarmason75997302021-01-24 01:58:33845 if (p->pcre2_pattern)
Ævar Arnfjörð Bjarmason94da9192017-06-01 18:20:56846 free_pcre2_pattern(p);
Michał Kiedrowicz63e7e9d2011-05-09 21:52:05847 else
848 regfree(&p->regexp);
Junio C Hamanob48fb5b2006-09-27 23:27:10849 break;
850 default:
851 break;
852 }
Patrick Steinhardt6d824372024-09-26 11:46:54853 free(p->pattern);
Junio C Hamanob48fb5b2006-09-27 23:27:10854 free(p);
855 }
Ævar Arnfjörð Bjarmason891c9962023-02-06 23:07:50856}
Junio C Hamanob48fb5b2006-09-27 23:27:10857
Ævar Arnfjörð Bjarmason891c9962023-02-06 23:07:50858void free_grep_patterns(struct grep_opt *opt)
859{
860 free_grep_pat(opt->pattern_list);
Ævar Arnfjörð Bjarmasonfb2ebe72023-02-06 23:07:51861 free_grep_pat(opt->header_list);
Ævar Arnfjörð Bjarmason891c9962023-02-06 23:07:50862
863 if (opt->pattern_expression)
864 free_pattern_expr(opt->pattern_expression);
Junio C Hamanob48fb5b2006-09-27 23:27:10865}
866
Jeff King1a845fb2021-09-21 03:49:49867static const char *end_of_line(const char *cp, unsigned long *left)
Junio C Hamano83b5d2f2006-09-17 23:02:52868{
869 unsigned long l = *left;
870 while (l && *cp != '\n') {
871 l--;
872 cp++;
873 }
874 *left = l;
875 return cp;
876}
877
878static int word_char(char ch)
879{
880 return isalnum(ch) || ch == '_';
881}
882
Mark Lodato55f638b2010-03-07 16:52:46883static void output_color(struct grep_opt *opt, const void *data, size_t size,
884 const char *color)
885{
Jeff Kingdaa0c3d2011-08-18 05:04:23886 if (want_color(opt->color) && color && color[0]) {
Mark Lodato55f638b2010-03-07 16:52:46887 opt->output(opt, color, strlen(color));
888 opt->output(opt, data, size);
889 opt->output(opt, GIT_COLOR_RESET, strlen(GIT_COLOR_RESET));
890 } else
891 opt->output(opt, data, size);
892}
893
894static void output_sep(struct grep_opt *opt, char sign)
895{
896 if (opt->null_following_name)
897 opt->output(opt, "\0", 1);
898 else
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:22899 output_color(opt, &sign, 1, opt->colors[GREP_COLOR_SEP]);
Mark Lodato55f638b2010-03-07 16:52:46900}
901
Raphael Zimmerer83caecc2008-10-01 16:11:15902static void show_name(struct grep_opt *opt, const char *name)
903{
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:22904 output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
Fredrik Kuivinen5b594f42010-01-25 22:51:39905 opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
Raphael Zimmerer83caecc2008-10-01 16:11:15906}
907
Jeff King1a845fb2021-09-21 03:49:49908static int patmatch(struct grep_pat *p,
909 const char *line, const char *eol,
Michał Kiedrowicz97e77782011-05-04 22:00:19910 regmatch_t *match, int eflags)
911{
Ævar Arnfjörð Bjarmason75997302021-01-24 01:58:33912 if (p->pcre2_pattern)
René Scharfece025ae2024-10-20 11:02:32913 return !pcre2match(p, line, eol, match, eflags);
Michał Kiedrowicz97e77782011-05-04 22:00:19914
René Scharfece025ae2024-10-20 11:02:32915 switch (regexec_buf(&p->regexp, line, eol - line, 1, match, eflags)) {
916 case 0:
917 return 1;
918 case REG_NOMATCH:
919 return 0;
920 default:
921 return -1;
922 }
Michał Kiedrowicz97e77782011-05-04 22:00:19923}
924
Jeff King1a845fb2021-09-21 03:49:49925static void strip_timestamp(const char *bol, const char **eol_p)
Junio C Hamanoa4d7d2c2008-09-05 05:15:02926{
Jeff King1a845fb2021-09-21 03:49:49927 const char *eol = *eol_p;
Junio C Hamanoa4d7d2c2008-09-05 05:15:02928
929 while (bol < --eol) {
930 if (*eol != '>')
931 continue;
932 *eol_p = ++eol;
Jeff Kingcc8e26e2021-09-21 03:46:56933 break;
Junio C Hamanoa4d7d2c2008-09-05 05:15:02934 }
Junio C Hamanoa4d7d2c2008-09-05 05:15:02935}
936
937static struct {
938 const char *field;
939 size_t len;
940} header_field[] = {
941 { "author ", 7 },
942 { "committer ", 10 },
Nguyễn Thái Ngọc Duy72fd13f2012-09-29 04:41:28943 { "reflog ", 7 },
Junio C Hamanoa4d7d2c2008-09-05 05:15:02944};
945
Hamza Mahfooz3f566c42021-09-29 11:57:15946static int headerless_match_one_pattern(struct grep_pat *p,
947 const char *bol, const char *eol,
948 enum grep_context ctx,
949 regmatch_t *pmatch, int eflags)
Junio C Hamano83b5d2f2006-09-17 23:02:52950{
951 int hit = 0;
René Scharfee701fad2009-05-20 21:31:53952 const char *start = bol;
Junio C Hamano83b5d2f2006-09-17 23:02:52953
Junio C Hamano480c1ca2006-09-20 19:39:46954 if ((p->token != GREP_PATTERN) &&
955 ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD)))
956 return 0;
957
Junio C Hamano83b5d2f2006-09-17 23:02:52958 again:
Michał Kiedrowicz97e77782011-05-04 22:00:19959 hit = patmatch(p, bol, eol, pmatch, eflags);
René Scharfece025ae2024-10-20 11:02:32960 if (hit < 0)
961 hit = 0;
Junio C Hamano83b5d2f2006-09-17 23:02:52962
René Scharfed7eb5272009-03-07 12:28:40963 if (hit && p->word_regexp) {
Junio C Hamano83b5d2f2006-09-17 23:02:52964 if ((pmatch[0].rm_so < 0) ||
René Scharfe84201ea2009-06-03 16:19:01965 (eol - bol) < pmatch[0].rm_so ||
Junio C Hamano83b5d2f2006-09-17 23:02:52966 (pmatch[0].rm_eo < 0) ||
967 (eol - bol) < pmatch[0].rm_eo)
968 die("regexp returned nonsense");
969
970 /* Match beginning must be either beginning of the
971 * line, or at word boundary (i.e. the last char must
972 * not be a word char). Similarly, match end must be
973 * either end of the line, or at word boundary
974 * (i.e. the next char must not be a word char).
975 */
René Scharfefb62eb72009-01-09 23:08:40976 if ( ((pmatch[0].rm_so == 0) ||
Junio C Hamano83b5d2f2006-09-17 23:02:52977 !word_char(bol[pmatch[0].rm_so-1])) &&
978 ((pmatch[0].rm_eo == (eol-bol)) ||
979 !word_char(bol[pmatch[0].rm_eo])) )
980 ;
981 else
982 hit = 0;
983
René Scharfe84201ea2009-06-03 16:19:01984 /* Words consist of at least one character. */
985 if (pmatch->rm_so == pmatch->rm_eo)
986 hit = 0;
987
Junio C Hamano83b5d2f2006-09-17 23:02:52988 if (!hit && pmatch[0].rm_so + bol + 1 < eol) {
989 /* There could be more than one match on the
990 * line, and the first match might not be
991 * strict word match. But later ones could be!
René Scharfefb62eb72009-01-09 23:08:40992 * Forward to the next possible start, i.e. the
993 * next position following a non-word char.
Junio C Hamano83b5d2f2006-09-17 23:02:52994 */
995 bol = pmatch[0].rm_so + bol + 1;
René Scharfefb62eb72009-01-09 23:08:40996 while (word_char(bol[-1]) && bol < eol)
997 bol++;
René Scharfedbb6a4a2009-05-23 11:45:26998 eflags |= REG_NOTBOL;
René Scharfefb62eb72009-01-09 23:08:40999 if (bol < eol)
1000 goto again;
Junio C Hamano83b5d2f2006-09-17 23:02:521001 }
1002 }
René Scharfee701fad2009-05-20 21:31:531003 if (hit) {
1004 pmatch[0].rm_so += bol - start;
1005 pmatch[0].rm_eo += bol - start;
1006 }
Junio C Hamano83b5d2f2006-09-17 23:02:521007 return hit;
1008}
1009
Hamza Mahfooz3f566c42021-09-29 11:57:151010static int match_one_pattern(struct grep_pat *p,
1011 const char *bol, const char *eol,
1012 enum grep_context ctx, regmatch_t *pmatch,
1013 int eflags)
1014{
1015 const char *field;
1016 size_t len;
1017
1018 if (p->token == GREP_PATTERN_HEAD) {
1019 assert(p->field < ARRAY_SIZE(header_field));
1020 field = header_field[p->field].field;
1021 len = header_field[p->field].len;
1022 if (strncmp(bol, field, len))
1023 return 0;
1024 bol += len;
1025
1026 switch (p->field) {
1027 case GREP_HEADER_AUTHOR:
1028 case GREP_HEADER_COMMITTER:
1029 strip_timestamp(bol, &eol);
1030 break;
1031 default:
1032 break;
1033 }
1034 }
1035
1036 return headerless_match_one_pattern(p, bol, eol, ctx, pmatch, eflags);
1037}
1038
1039
Jeff King1a845fb2021-09-21 03:49:491040static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x,
1041 const char *bol, const char *eol,
1042 enum grep_context ctx, ssize_t *col,
Taylor Blau68d686e2018-06-22 15:49:351043 ssize_t *icol, int collect_hits)
Junio C Hamano83b5d2f2006-09-17 23:02:521044{
Junio C Hamano0ab7bef2006-09-28 00:50:521045 int h = 0;
Junio C Hamano0ab7bef2006-09-28 00:50:521046
Junio C Hamano83b5d2f2006-09-17 23:02:521047 switch (x->node) {
Junio C Hamano5aaeb732010-09-13 05:15:351048 case GREP_NODE_TRUE:
1049 h = 1;
1050 break;
Junio C Hamano83b5d2f2006-09-17 23:02:521051 case GREP_NODE_ATOM:
Taylor Blau68d686e2018-06-22 15:49:351052 {
1053 regmatch_t tmp;
1054 h = match_one_pattern(x->u.atom, bol, eol, ctx,
1055 &tmp, 0);
1056 if (h && (*col < 0 || tmp.rm_so < *col))
1057 *col = tmp.rm_so;
1058 }
René Scharfe794c0002021-12-17 16:48:491059 if (x->u.atom->token == GREP_PATTERN_BODY)
1060 opt->body_hit |= h;
Junio C Hamano83b5d2f2006-09-17 23:02:521061 break;
1062 case GREP_NODE_NOT:
Taylor Blau68d686e2018-06-22 15:49:351063 /*
1064 * Upon visiting a GREP_NODE_NOT, col and icol become swapped.
1065 */
1066 h = !match_expr_eval(opt, x->u.unary, bol, eol, ctx, icol, col,
1067 0);
Junio C Hamano0ab7bef2006-09-28 00:50:521068 break;
Junio C Hamano83b5d2f2006-09-17 23:02:521069 case GREP_NODE_AND:
Taylor Blau017c0fc2018-06-22 15:49:391070 h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col,
Taylor Blau68d686e2018-06-22 15:49:351071 icol, 0);
Taylor Blau017c0fc2018-06-22 15:49:391072 if (h || opt->columnnum) {
1073 /*
1074 * Don't short-circuit AND when given --column, since a
1075 * NOT earlier in the tree may turn this into an OR. In
1076 * this case, see the below comment.
1077 */
1078 h &= match_expr_eval(opt, x->u.binary.right, bol, eol,
1079 ctx, col, icol, 0);
1080 }
Junio C Hamano0ab7bef2006-09-28 00:50:521081 break;
Junio C Hamano83b5d2f2006-09-17 23:02:521082 case GREP_NODE_OR:
Taylor Blau017c0fc2018-06-22 15:49:391083 if (!(collect_hits || opt->columnnum)) {
1084 /*
1085 * Don't short-circuit OR when given --column (or
1086 * collecting hits) to ensure we don't skip a later
1087 * child that would produce an earlier match.
1088 */
Taylor Blau68d686e2018-06-22 15:49:351089 return (match_expr_eval(opt, x->u.binary.left, bol, eol,
1090 ctx, col, icol, 0) ||
1091 match_expr_eval(opt, x->u.binary.right, bol,
1092 eol, ctx, col, icol, 0));
Taylor Blau017c0fc2018-06-22 15:49:391093 }
Taylor Blau68d686e2018-06-22 15:49:351094 h = match_expr_eval(opt, x->u.binary.left, bol, eol, ctx, col,
1095 icol, 0);
Taylor Blau017c0fc2018-06-22 15:49:391096 if (collect_hits)
1097 x->u.binary.left->hit |= h;
Taylor Blau68d686e2018-06-22 15:49:351098 h |= match_expr_eval(opt, x->u.binary.right, bol, eol, ctx, col,
Taylor Blau017c0fc2018-06-22 15:49:391099 icol, collect_hits);
Junio C Hamano0ab7bef2006-09-28 00:50:521100 break;
1101 default:
Alexander Potashevd7530702009-01-04 18:38:411102 die("Unexpected node type (internal error) %d", x->node);
Junio C Hamano83b5d2f2006-09-17 23:02:521103 }
Junio C Hamano0ab7bef2006-09-28 00:50:521104 if (collect_hits)
1105 x->hit |= h;
1106 return h;
Junio C Hamano83b5d2f2006-09-17 23:02:521107}
1108
Jeff King1a845fb2021-09-21 03:49:491109static int match_expr(struct grep_opt *opt,
1110 const char *bol, const char *eol,
Taylor Blau68d686e2018-06-22 15:49:351111 enum grep_context ctx, ssize_t *col,
1112 ssize_t *icol, int collect_hits)
Junio C Hamano83b5d2f2006-09-17 23:02:521113{
1114 struct grep_expr *x = opt->pattern_expression;
Taylor Blau68d686e2018-06-22 15:49:351115 return match_expr_eval(opt, x, bol, eol, ctx, col, icol, collect_hits);
Junio C Hamano83b5d2f2006-09-17 23:02:521116}
1117
Jeff King1a845fb2021-09-21 03:49:491118static int match_line(struct grep_opt *opt,
1119 const char *bol, const char *eol,
Taylor Blau68d686e2018-06-22 15:49:351120 ssize_t *col, ssize_t *icol,
Junio C Hamano0ab7bef2006-09-28 00:50:521121 enum grep_context ctx, int collect_hits)
Junio C Hamano83b5d2f2006-09-17 23:02:521122{
1123 struct grep_pat *p;
Taylor Blau017c0fc2018-06-22 15:49:391124 int hit = 0;
René Scharfe79212772009-03-07 12:30:271125
Ævar Arnfjörð Bjarmasondb843762022-10-11 09:48:451126 if (opt->pattern_expression)
Taylor Blau68d686e2018-06-22 15:49:351127 return match_expr(opt, bol, eol, ctx, col, icol,
1128 collect_hits);
Junio C Hamano0ab7bef2006-09-28 00:50:521129
1130 /* we do not call with collect_hits without being extended */
Junio C Hamano83b5d2f2006-09-17 23:02:521131 for (p = opt->pattern_list; p; p = p->next) {
Taylor Blau68d686e2018-06-22 15:49:351132 regmatch_t tmp;
1133 if (match_one_pattern(p, bol, eol, ctx, &tmp, 0)) {
Taylor Blau017c0fc2018-06-22 15:49:391134 hit |= 1;
1135 if (!opt->columnnum) {
1136 /*
1137 * Without --column, any single match on a line
1138 * is enough to know that it needs to be
1139 * printed. With --column, scan _all_ patterns
1140 * to find the earliest.
1141 */
1142 break;
1143 }
1144 if (*col < 0 || tmp.rm_so < *col)
1145 *col = tmp.rm_so;
Taylor Blau68d686e2018-06-22 15:49:351146 }
Junio C Hamano83b5d2f2006-09-17 23:02:521147 }
Taylor Blau017c0fc2018-06-22 15:49:391148 return hit;
Junio C Hamano83b5d2f2006-09-17 23:02:521149}
1150
Jeff King1a845fb2021-09-21 03:49:491151static int match_next_pattern(struct grep_pat *p,
1152 const char *bol, const char *eol,
René Scharfe7e8f59d2009-03-07 12:32:321153 enum grep_context ctx,
1154 regmatch_t *pmatch, int eflags)
1155{
1156 regmatch_t match;
1157
Hamza Mahfooz3f566c42021-09-29 11:57:151158 if (!headerless_match_one_pattern(p, bol, eol, ctx, &match, eflags))
René Scharfe7e8f59d2009-03-07 12:32:321159 return 0;
1160 if (match.rm_so < 0 || match.rm_eo < 0)
1161 return 0;
1162 if (pmatch->rm_so >= 0 && pmatch->rm_eo >= 0) {
1163 if (match.rm_so > pmatch->rm_so)
1164 return 1;
1165 if (match.rm_so == pmatch->rm_so && match.rm_eo < pmatch->rm_eo)
1166 return 1;
1167 }
1168 pmatch->rm_so = match.rm_so;
1169 pmatch->rm_eo = match.rm_eo;
1170 return 1;
1171}
1172
Hamza Mahfooz3f566c42021-09-29 11:57:151173int grep_next_match(struct grep_opt *opt,
1174 const char *bol, const char *eol,
1175 enum grep_context ctx, regmatch_t *pmatch,
1176 enum grep_header_field field, int eflags)
René Scharfe7e8f59d2009-03-07 12:32:321177{
1178 struct grep_pat *p;
1179 int hit = 0;
1180
1181 pmatch->rm_so = pmatch->rm_eo = -1;
1182 if (bol < eol) {
Hamza Mahfooz3f566c42021-09-29 11:57:151183 for (p = ((ctx == GREP_CONTEXT_HEAD)
1184 ? opt->header_list : opt->pattern_list);
1185 p; p = p->next) {
René Scharfe7e8f59d2009-03-07 12:32:321186 switch (p->token) {
René Scharfe7e8f59d2009-03-07 12:32:321187 case GREP_PATTERN_HEAD:
Hamza Mahfooz3f566c42021-09-29 11:57:151188 if ((field != GREP_HEADER_FIELD_MAX) &&
1189 (p->field != field))
1190 continue;
1191 /* fall thru */
1192 case GREP_PATTERN: /* atom */
René Scharfe7e8f59d2009-03-07 12:32:321193 case GREP_PATTERN_BODY:
1194 hit |= match_next_pattern(p, bol, eol, ctx,
1195 pmatch, eflags);
1196 break;
1197 default:
1198 break;
1199 }
1200 }
1201 }
1202 return hit;
1203}
1204
Taylor Blauc707ded2018-07-03 21:51:561205static void show_line_header(struct grep_opt *opt, const char *name,
1206 unsigned lno, ssize_t cno, char sign)
René Scharfe7e8f59d2009-03-07 12:32:321207{
René Scharfe1d84f722011-06-05 15:24:361208 if (opt->heading && opt->last_shown == 0) {
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:221209 output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
René Scharfe1d84f722011-06-05 15:24:361210 opt->output(opt, "\n", 1);
1211 }
René Scharfe5dd06d32009-07-01 22:02:381212 opt->last_shown = lno;
1213
René Scharfe1d84f722011-06-05 15:24:361214 if (!opt->heading && opt->pathname) {
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:221215 output_color(opt, name, strlen(name), opt->colors[GREP_COLOR_FILENAME]);
Mark Lodato55f638b2010-03-07 16:52:461216 output_sep(opt, sign);
Fredrik Kuivinen5b594f42010-01-25 22:51:391217 }
1218 if (opt->linenum) {
1219 char buf[32];
Jeff King1a168e52017-03-28 19:46:561220 xsnprintf(buf, sizeof(buf), "%d", lno);
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:221221 output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_LINENO]);
Mark Lodato55f638b2010-03-07 16:52:461222 output_sep(opt, sign);
Fredrik Kuivinen5b594f42010-01-25 22:51:391223 }
Taylor Blau89252cd2018-06-22 15:49:421224 /*
1225 * Treat 'cno' as the 1-indexed offset from the start of a non-context
1226 * line to its first match. Otherwise, 'cno' is 0 indicating that we are
1227 * being called with a context line.
1228 */
1229 if (opt->columnnum && cno) {
1230 char buf[32];
1231 xsnprintf(buf, sizeof(buf), "%"PRIuMAX, (uintmax_t)cno);
Junio C Hamanod036d662018-07-18 19:20:311232 output_color(opt, buf, strlen(buf), opt->colors[GREP_COLOR_COLUMNNO]);
Taylor Blau89252cd2018-06-22 15:49:421233 output_sep(opt, sign);
1234 }
Taylor Blauc707ded2018-07-03 21:51:561235}
1236
Jeff King1a845fb2021-09-21 03:49:491237static void show_line(struct grep_opt *opt,
1238 const char *bol, const char *eol,
Taylor Blauc707ded2018-07-03 21:51:561239 const char *name, unsigned lno, ssize_t cno, char sign)
1240{
1241 int rest = eol - bol;
Taylor Blau9d8db062018-07-09 20:33:471242 const char *match_color = NULL;
1243 const char *line_color = NULL;
Taylor Blauc707ded2018-07-03 21:51:561244
1245 if (opt->file_break && opt->last_shown == 0) {
1246 if (opt->show_hunk_mark)
1247 opt->output(opt, "\n", 1);
1248 } else if (opt->pre_context || opt->post_context || opt->funcbody) {
1249 if (opt->last_shown == 0) {
1250 if (opt->show_hunk_mark) {
Junio C Hamano87ece7c2018-08-02 22:30:441251 output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]);
Taylor Blauc707ded2018-07-03 21:51:561252 opt->output(opt, "\n", 1);
1253 }
1254 } else if (lno > opt->last_shown + 1) {
Junio C Hamano87ece7c2018-08-02 22:30:441255 output_color(opt, "--", 2, opt->colors[GREP_COLOR_SEP]);
Taylor Blauc707ded2018-07-03 21:51:561256 opt->output(opt, "\n", 1);
1257 }
1258 }
Taylor Blau9d8db062018-07-09 20:33:471259 if (!opt->only_matching) {
1260 /*
1261 * In case the line we're being called with contains more than
1262 * one match, leave printing each header to the loop below.
1263 */
1264 show_line_header(opt, name, lno, cno, sign);
1265 }
Jeff King8ee24762025-09-16 20:16:001266 if (want_color(opt->color) || opt->only_matching) {
René Scharfe7e8f59d2009-03-07 12:32:321267 regmatch_t match;
1268 enum grep_context ctx = GREP_CONTEXT_BODY;
René Scharfe7e8f59d2009-03-07 12:32:321269 int eflags = 0;
1270
Jeff King8ee24762025-09-16 20:16:001271 if (want_color(opt->color)) {
Taylor Blau9d8db062018-07-09 20:33:471272 if (sign == ':')
Junio C Hamano87ece7c2018-08-02 22:30:441273 match_color = opt->colors[GREP_COLOR_MATCH_SELECTED];
Taylor Blau9d8db062018-07-09 20:33:471274 else
Junio C Hamano87ece7c2018-08-02 22:30:441275 match_color = opt->colors[GREP_COLOR_MATCH_CONTEXT];
Taylor Blau9d8db062018-07-09 20:33:471276 if (sign == ':')
Junio C Hamano87ece7c2018-08-02 22:30:441277 line_color = opt->colors[GREP_COLOR_SELECTED];
Taylor Blau9d8db062018-07-09 20:33:471278 else if (sign == '-')
Junio C Hamano87ece7c2018-08-02 22:30:441279 line_color = opt->colors[GREP_COLOR_CONTEXT];
Taylor Blau9d8db062018-07-09 20:33:471280 else if (sign == '=')
Junio C Hamano87ece7c2018-08-02 22:30:441281 line_color = opt->colors[GREP_COLOR_FUNCTION];
Taylor Blau9d8db062018-07-09 20:33:471282 }
Hamza Mahfooz3f566c42021-09-29 11:57:151283 while (grep_next_match(opt, bol, eol, ctx, &match,
1284 GREP_HEADER_FIELD_MAX, eflags)) {
René Scharfe1f5b9cc2009-06-01 21:53:051285 if (match.rm_so == match.rm_eo)
1286 break;
Fredrik Kuivinen5b594f42010-01-25 22:51:391287
Taylor Blau9d8db062018-07-09 20:33:471288 if (opt->only_matching)
1289 show_line_header(opt, name, lno, cno, sign);
1290 else
1291 output_color(opt, bol, match.rm_so, line_color);
Mark Lodato55f638b2010-03-07 16:52:461292 output_color(opt, bol + match.rm_so,
René Scharfe79a77102014-10-27 18:23:051293 match.rm_eo - match.rm_so, match_color);
Taylor Blau9d8db062018-07-09 20:33:471294 if (opt->only_matching)
1295 opt->output(opt, "\n", 1);
René Scharfe7e8f59d2009-03-07 12:32:321296 bol += match.rm_eo;
Taylor Blau9d8db062018-07-09 20:33:471297 cno += match.rm_eo;
René Scharfe7e8f59d2009-03-07 12:32:321298 rest -= match.rm_eo;
1299 eflags = REG_NOTBOL;
1300 }
René Scharfe7e8f59d2009-03-07 12:32:321301 }
Taylor Blau9d8db062018-07-09 20:33:471302 if (!opt->only_matching) {
1303 output_color(opt, bol, rest, line_color);
1304 opt->output(opt, "\n", 1);
1305 }
René Scharfe7e8f59d2009-03-07 12:32:321306}
1307
Jeff King78db6ea2012-02-02 08:18:291308int grep_use_locks;
1309
Thomas Rast0579f912011-12-12 21:16:071310/*
1311 * This lock protects access to the gitattributes machinery, which is
1312 * not thread-safe.
1313 */
1314pthread_mutex_t grep_attr_mutex;
1315
Jeff King78db6ea2012-02-02 08:18:291316static inline void grep_attr_lock(void)
Thomas Rast0579f912011-12-12 21:16:071317{
Jeff King78db6ea2012-02-02 08:18:291318 if (grep_use_locks)
Thomas Rast0579f912011-12-12 21:16:071319 pthread_mutex_lock(&grep_attr_mutex);
1320}
1321
Jeff King78db6ea2012-02-02 08:18:291322static inline void grep_attr_unlock(void)
Thomas Rast0579f912011-12-12 21:16:071323{
Jeff King78db6ea2012-02-02 08:18:291324 if (grep_use_locks)
Thomas Rast0579f912011-12-12 21:16:071325 pthread_mutex_unlock(&grep_attr_mutex);
1326}
Jeff Kingb3aeb282012-02-02 08:18:411327
Jeff King1a845fb2021-09-21 03:49:491328static int match_funcname(struct grep_opt *opt, struct grep_source *gs,
1329 const char *bol, const char *eol)
René Scharfe2944e4e2009-07-01 22:06:341330{
René Scharfe60ecac92009-07-01 22:07:241331 xdemitconf_t *xecfg = opt->priv;
Thomas Rast0579f912011-12-12 21:16:071332 if (xecfg && !xecfg->find_func) {
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:331333 grep_source_load_driver(gs, opt->repo->index);
Jeff King94ad9d92012-02-02 08:20:431334 if (gs->driver->funcname.pattern) {
1335 const struct userdiff_funcname *pe = &gs->driver->funcname;
Thomas Rast0579f912011-12-12 21:16:071336 xdiff_set_find_func(xecfg, pe->pattern, pe->cflags);
1337 } else {
1338 xecfg = opt->priv = NULL;
1339 }
1340 }
1341
1342 if (xecfg) {
René Scharfe60ecac92009-07-01 22:07:241343 char buf[1];
1344 return xecfg->find_func(bol, eol - bol, buf, 1,
1345 xecfg->find_func_priv) >= 0;
1346 }
1347
René Scharfe2944e4e2009-07-01 22:06:341348 if (bol == eol)
1349 return 0;
1350 if (isalpha(*bol) || *bol == '_' || *bol == '$')
1351 return 1;
1352 return 0;
1353}
1354
Jeff Kinge1327022012-02-02 08:19:281355static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
Jeff King1a845fb2021-09-21 03:49:491356 const char *bol, unsigned lno)
René Scharfe2944e4e2009-07-01 22:06:341357{
Jeff Kinge1327022012-02-02 08:19:281358 while (bol > gs->buf) {
Jeff King1a845fb2021-09-21 03:49:491359 const char *eol = --bol;
René Scharfe2944e4e2009-07-01 22:06:341360
Jeff Kinge1327022012-02-02 08:19:281361 while (bol > gs->buf && bol[-1] != '\n')
René Scharfe2944e4e2009-07-01 22:06:341362 bol--;
1363 lno--;
1364
1365 if (lno <= opt->last_shown)
1366 break;
1367
Jeff Kinge1327022012-02-02 08:19:281368 if (match_funcname(opt, gs, bol, eol)) {
Taylor Blau89252cd2018-06-22 15:49:421369 show_line(opt, bol, eol, gs->name, lno, 0, '=');
René Scharfe2944e4e2009-07-01 22:06:341370 break;
1371 }
1372 }
1373}
1374
René Scharfea5dc20b2017-11-18 18:08:081375static int is_empty_line(const char *bol, const char *eol);
1376
Jeff Kinge1327022012-02-02 08:19:281377static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
Jeff King1a845fb2021-09-21 03:49:491378 const char *bol, const char *end, unsigned lno)
René Scharfe49de3212009-07-01 22:05:171379{
René Scharfe6653a012017-11-18 18:07:131380 unsigned cur = lno, from = 1, funcname_lno = 0, orig_from;
René Scharfea5dc20b2017-11-18 18:08:081381 int funcname_needed = !!opt->funcname, comment_needed = 0;
René Scharfeba8ea742011-08-01 17:20:531382
René Scharfe49de3212009-07-01 22:05:171383 if (opt->pre_context < lno)
1384 from = lno - opt->pre_context;
1385 if (from <= opt->last_shown)
1386 from = opt->last_shown + 1;
René Scharfe6653a012017-11-18 18:07:131387 orig_from = from;
René Scharfea5dc20b2017-11-18 18:08:081388 if (opt->funcbody) {
1389 if (match_funcname(opt, gs, bol, end))
1390 comment_needed = 1;
1391 else
1392 funcname_needed = 1;
René Scharfe6653a012017-11-18 18:07:131393 from = opt->last_shown + 1;
1394 }
René Scharfe49de3212009-07-01 22:05:171395
1396 /* Rewind. */
René Scharfe6653a012017-11-18 18:07:131397 while (bol > gs->buf && cur > from) {
Jeff King1a845fb2021-09-21 03:49:491398 const char *next_bol = bol;
1399 const char *eol = --bol;
René Scharfe2944e4e2009-07-01 22:06:341400
Jeff Kinge1327022012-02-02 08:19:281401 while (bol > gs->buf && bol[-1] != '\n')
René Scharfe49de3212009-07-01 22:05:171402 bol--;
1403 cur--;
René Scharfea5dc20b2017-11-18 18:08:081404 if (comment_needed && (is_empty_line(bol, eol) ||
1405 match_funcname(opt, gs, bol, eol))) {
1406 comment_needed = 0;
1407 from = orig_from;
1408 if (cur < from) {
1409 cur++;
1410 bol = next_bol;
1411 break;
1412 }
1413 }
Jeff Kinge1327022012-02-02 08:19:281414 if (funcname_needed && match_funcname(opt, gs, bol, eol)) {
René Scharfe2944e4e2009-07-01 22:06:341415 funcname_lno = cur;
1416 funcname_needed = 0;
René Scharfea5dc20b2017-11-18 18:08:081417 if (opt->funcbody)
1418 comment_needed = 1;
1419 else
1420 from = orig_from;
René Scharfe2944e4e2009-07-01 22:06:341421 }
René Scharfe49de3212009-07-01 22:05:171422 }
1423
René Scharfe2944e4e2009-07-01 22:06:341424 /* We need to look even further back to find a function signature. */
1425 if (opt->funcname && funcname_needed)
Jeff Kinge1327022012-02-02 08:19:281426 show_funcname_line(opt, gs, bol, cur);
René Scharfe2944e4e2009-07-01 22:06:341427
René Scharfe49de3212009-07-01 22:05:171428 /* Back forward. */
1429 while (cur < lno) {
Jeff King1a845fb2021-09-21 03:49:491430 const char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
René Scharfe49de3212009-07-01 22:05:171431
1432 while (*eol != '\n')
1433 eol++;
Taylor Blau89252cd2018-06-22 15:49:421434 show_line(opt, bol, eol, gs->name, cur, 0, sign);
René Scharfe49de3212009-07-01 22:05:171435 bol = eol + 1;
1436 cur++;
1437 }
1438}
1439
Junio C Hamanoa26345b2010-01-11 06:39:361440static int should_lookahead(struct grep_opt *opt)
1441{
1442 struct grep_pat *p;
1443
Ævar Arnfjörð Bjarmasondb843762022-10-11 09:48:451444 if (opt->pattern_expression)
Junio C Hamanoa26345b2010-01-11 06:39:361445 return 0; /* punt for too complex stuff */
1446 if (opt->invert)
1447 return 0;
1448 for (p = opt->pattern_list; p; p = p->next) {
1449 if (p->token != GREP_PATTERN)
1450 return 0; /* punt for "header only" and stuff */
1451 }
1452 return 1;
1453}
1454
1455static int look_ahead(struct grep_opt *opt,
1456 unsigned long *left_p,
1457 unsigned *lno_p,
Jeff King1a845fb2021-09-21 03:49:491458 const char **bol_p)
Junio C Hamanoa26345b2010-01-11 06:39:361459{
1460 unsigned lno = *lno_p;
Jeff King1a845fb2021-09-21 03:49:491461 const char *bol = *bol_p;
Junio C Hamanoa26345b2010-01-11 06:39:361462 struct grep_pat *p;
Jeff King1a845fb2021-09-21 03:49:491463 const char *sp, *last_bol;
Junio C Hamanoa26345b2010-01-11 06:39:361464 regoff_t earliest = -1;
1465
1466 for (p = opt->pattern_list; p; p = p->next) {
1467 int hit;
1468 regmatch_t m;
1469
Michał Kiedrowicz97e77782011-05-04 22:00:191470 hit = patmatch(p, bol, bol + *left_p, &m, 0);
René Scharfece025ae2024-10-20 11:02:321471 if (hit < 0)
1472 return -1;
Junio C Hamanoa26345b2010-01-11 06:39:361473 if (!hit || m.rm_so < 0 || m.rm_eo < 0)
1474 continue;
1475 if (earliest < 0 || m.rm_so < earliest)
1476 earliest = m.rm_so;
1477 }
1478
1479 if (earliest < 0) {
1480 *bol_p = bol + *left_p;
1481 *left_p = 0;
1482 return 1;
1483 }
1484 for (sp = bol + earliest; bol < sp && sp[-1] != '\n'; sp--)
1485 ; /* find the beginning of the line */
1486 last_bol = sp;
1487
1488 for (sp = bol; sp < last_bol; sp++) {
1489 if (*sp == '\n')
1490 lno++;
1491 }
1492 *left_p -= last_bol - bol;
1493 *bol_p = last_bol;
1494 *lno_p = lno;
1495 return 0;
1496}
1497
Nguyễn Thái Ngọc Duy38bbc2e2018-09-21 15:57:231498static int fill_textconv_grep(struct repository *r,
1499 struct userdiff_driver *driver,
Jeff King335ec3b2013-05-10 15:10:151500 struct grep_source *gs)
1501{
1502 struct diff_filespec *df;
1503 char *buf;
1504 size_t size;
1505
1506 if (!driver || !driver->textconv)
1507 return grep_source_load(gs);
1508
1509 /*
1510 * The textconv interface is intimately tied to diff_filespecs, so we
1511 * have to pretend to be one. If we could unify the grep_source
1512 * and diff_filespec structs, this mess could just go away.
1513 */
1514 df = alloc_filespec(gs->path);
1515 switch (gs->type) {
Brandon Williams1c41c822017-05-30 17:30:441516 case GREP_SOURCE_OID:
Jeff King335ec3b2013-05-10 15:10:151517 fill_filespec(df, gs->identifier, 1, 0100644);
1518 break;
1519 case GREP_SOURCE_FILE:
Patrick Steinhardt7d70b292025-03-10 07:13:311520 fill_filespec(df, null_oid(r->hash_algo), 0, 0100644);
Jeff King335ec3b2013-05-10 15:10:151521 break;
1522 default:
Johannes Schindelin033abf92018-05-02 09:38:391523 BUG("attempt to textconv something without a path?");
Jeff King335ec3b2013-05-10 15:10:151524 }
1525
1526 /*
Matheus Tavares1d1729c2020-01-16 02:39:541527 * fill_textconv is not remotely thread-safe; it modifies the global
1528 * diff tempfile structure, writes to the_repo's odb and might
1529 * internally call thread-unsafe functions such as the
1530 * prepare_packed_git() lazy-initializator. Because of the last two, we
1531 * must ensure mutual exclusion between this call and the object reading
1532 * API, thus we use obj_read_lock() here.
1533 *
1534 * TODO: allowing text conversion to run in parallel with object
1535 * reading operations might increase performance in the multithreaded
1536 * non-worktreee git-grep with --textconv.
Jeff King335ec3b2013-05-10 15:10:151537 */
Matheus Tavares1d1729c2020-01-16 02:39:541538 obj_read_lock();
Nguyễn Thái Ngọc Duy38bbc2e2018-09-21 15:57:231539 size = fill_textconv(r, driver, df, &buf);
Matheus Tavares1d1729c2020-01-16 02:39:541540 obj_read_unlock();
Jeff King335ec3b2013-05-10 15:10:151541 free_filespec(df);
1542
1543 /*
1544 * The normal fill_textconv usage by the diff machinery would just keep
1545 * the textconv'd buf separate from the diff_filespec. But much of the
1546 * grep code passes around a grep_source and assumes that its "buf"
1547 * pointer is the beginning of the thing we are searching. So let's
1548 * install our textconv'd version into the grep_source, taking care not
1549 * to leak any existing buffer.
1550 */
1551 grep_source_clear_data(gs);
1552 gs->buf = buf;
1553 gs->size = size;
1554
1555 return 0;
1556}
1557
René Scharfe4aa2c472016-05-28 15:06:191558static int is_empty_line(const char *bol, const char *eol)
1559{
1560 while (bol < eol && isspace(*bol))
1561 bol++;
1562 return bol == eol;
1563}
1564
Jeff Kinge1327022012-02-02 08:19:281565static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
Junio C Hamano83b5d2f2006-09-17 23:02:521566{
Jeff King1a845fb2021-09-21 03:49:491567 const char *bol;
1568 const char *peek_bol = NULL;
Jeff Kinge1327022012-02-02 08:19:281569 unsigned long left;
Junio C Hamano83b5d2f2006-09-17 23:02:521570 unsigned lno = 1;
Junio C Hamano83b5d2f2006-09-17 23:02:521571 unsigned last_hit = 0;
Junio C Hamano83b5d2f2006-09-17 23:02:521572 int binary_match_only = 0;
Junio C Hamano83b5d2f2006-09-17 23:02:521573 unsigned count = 0;
Junio C Hamanoa26345b2010-01-11 06:39:361574 int try_lookahead = 0;
René Scharfeba8ea742011-08-01 17:20:531575 int show_function = 0;
Jeff King335ec3b2013-05-10 15:10:151576 struct userdiff_driver *textconv = NULL;
Junio C Hamano480c1ca2006-09-20 19:39:461577 enum grep_context ctx = GREP_CONTEXT_HEAD;
René Scharfe60ecac92009-07-01 22:07:241578 xdemitconf_t xecfg;
Junio C Hamano83b5d2f2006-09-17 23:02:521579
Emily Shafferde99eb02019-05-23 20:23:561580 if (!opt->status_only && gs->name == NULL)
1581 BUG("grep call which could print a name requires "
1582 "grep_source.name be non-NULL");
1583
Fredrik Kuivinen5b594f42010-01-25 22:51:391584 if (!opt->output)
1585 opt->output = std_output;
1586
René Scharfeba8ea742011-08-01 17:20:531587 if (opt->pre_context || opt->post_context || opt->file_break ||
1588 opt->funcbody) {
René Scharfe08303c32011-06-05 15:24:151589 /* Show hunk marks, except for the first file. */
1590 if (opt->last_shown)
1591 opt->show_hunk_mark = 1;
1592 /*
1593 * If we're using threads then we can't easily identify
1594 * the first file. Always put hunk marks in that case
1595 * and skip the very first one later in work_done().
1596 */
1597 if (opt->output != std_output)
1598 opt->show_hunk_mark = 1;
1599 }
René Scharfe431d6e72010-03-15 16:21:101600 opt->last_shown = 0;
1601
Jeff King335ec3b2013-05-10 15:10:151602 if (opt->allow_textconv) {
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:331603 grep_source_load_driver(gs, opt->repo->index);
Jeff King335ec3b2013-05-10 15:10:151604 /*
1605 * We might set up the shared textconv cache data here, which
Matheus Tavares1d1729c2020-01-16 02:39:541606 * is not thread-safe. Also, get_oid_with_context() and
1607 * parse_object() might be internally called. As they are not
Steve Kemp84544f22020-07-29 03:33:281608 * currently thread-safe and might be racy with object reading,
Matheus Tavares1d1729c2020-01-16 02:39:541609 * obj_read_lock() must be called.
Jeff King335ec3b2013-05-10 15:10:151610 */
1611 grep_attr_lock();
Matheus Tavares1d1729c2020-01-16 02:39:541612 obj_read_lock();
Nguyễn Thái Ngọc Duybd7ad452018-11-10 05:49:061613 textconv = userdiff_get_textconv(opt->repo, gs->driver);
Matheus Tavares1d1729c2020-01-16 02:39:541614 obj_read_unlock();
Jeff King335ec3b2013-05-10 15:10:151615 grep_attr_unlock();
1616 }
1617
1618 /*
1619 * We know the result of a textconv is text, so we only have to care
1620 * about binary handling if we are not using it.
1621 */
1622 if (!textconv) {
1623 switch (opt->binary) {
1624 case GREP_BINARY_DEFAULT:
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:331625 if (grep_source_is_binary(gs, opt->repo->index))
Jeff King335ec3b2013-05-10 15:10:151626 binary_match_only = 1;
1627 break;
1628 case GREP_BINARY_NOMATCH:
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:331629 if (grep_source_is_binary(gs, opt->repo->index))
Jeff King335ec3b2013-05-10 15:10:151630 return 0; /* Assume unmatch */
1631 break;
1632 case GREP_BINARY_TEXT:
1633 break;
1634 default:
Johannes Schindelin033abf92018-05-02 09:38:391635 BUG("unknown binary handling mode");
Jeff King335ec3b2013-05-10 15:10:151636 }
Junio C Hamano83b5d2f2006-09-17 23:02:521637 }
1638
René Scharfe60ecac92009-07-01 22:07:241639 memset(&xecfg, 0, sizeof(xecfg));
Thomas Rast0579f912011-12-12 21:16:071640 opt->priv = &xecfg;
1641
Junio C Hamanoa26345b2010-01-11 06:39:361642 try_lookahead = should_lookahead(opt);
René Scharfe60ecac92009-07-01 22:07:241643
Nguyễn Thái Ngọc Duy38bbc2e2018-09-21 15:57:231644 if (fill_textconv_grep(opt->repo, textconv, gs) < 0)
Jeff King08265792012-02-02 08:21:111645 return 0;
1646
Jeff Kinge1327022012-02-02 08:19:281647 bol = gs->buf;
1648 left = gs->size;
Junio C Hamano83b5d2f2006-09-17 23:02:521649 while (left) {
Jeff King1a845fb2021-09-21 03:49:491650 const char *eol;
Junio C Hamano0ab7bef2006-09-28 00:50:521651 int hit;
Taylor Blau89252cd2018-06-22 15:49:421652 ssize_t cno;
Taylor Blau68d686e2018-06-22 15:49:351653 ssize_t col = -1, icol = -1;
Junio C Hamano83b5d2f2006-09-17 23:02:521654
Junio C Hamanoa26345b2010-01-11 06:39:361655 /*
Michał Kiedrowicz8997da32011-05-09 21:52:031656 * look_ahead() skips quickly to the line that possibly
Junio C Hamanoa26345b2010-01-11 06:39:361657 * has the next hit; don't call it if we need to do
1658 * something more than just skipping the current line
1659 * in response to an unmatch for the current line. E.g.
1660 * inside a post-context window, we will show the current
1661 * line as a context around the previous hit when it
1662 * doesn't hit.
1663 */
1664 if (try_lookahead
1665 && !(last_hit
René Scharfeba8ea742011-08-01 17:20:531666 && (show_function ||
René Scharfece025ae2024-10-20 11:02:321667 lno <= last_hit + opt->post_context))) {
1668 hit = look_ahead(opt, &left, &lno, &bol);
1669 if (hit < 0)
1670 try_lookahead = 0;
1671 else if (hit)
1672 break;
1673 }
Junio C Hamano83b5d2f2006-09-17 23:02:521674 eol = end_of_line(bol, &left);
Junio C Hamano83b5d2f2006-09-17 23:02:521675
Junio C Hamano480c1ca2006-09-20 19:39:461676 if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1677 ctx = GREP_CONTEXT_BODY;
1678
Taylor Blau68d686e2018-06-22 15:49:351679 hit = match_line(opt, bol, eol, &col, &icol, ctx, collect_hits);
Junio C Hamano83b5d2f2006-09-17 23:02:521680
Junio C Hamano0ab7bef2006-09-28 00:50:521681 if (collect_hits)
1682 goto next_line;
1683
Junio C Hamano83b5d2f2006-09-17 23:02:521684 /* "grep -v -e foo -e bla" should list lines
1685 * that do not have either, so inversion should
1686 * be done outside.
1687 */
1688 if (opt->invert)
1689 hit = !hit;
1690 if (opt->unmatch_name_only) {
1691 if (hit)
1692 return 0;
1693 goto next_line;
1694 }
Carlos López68437ed2022-06-22 19:47:321695 if (hit && (opt->max_count < 0 || count < opt->max_count)) {
Junio C Hamano83b5d2f2006-09-17 23:02:521696 count++;
1697 if (opt->status_only)
1698 return 1;
René Scharfe321ffcc2010-05-22 21:30:481699 if (opt->name_only) {
Jeff Kinge1327022012-02-02 08:19:281700 show_name(opt, gs->name);
René Scharfe321ffcc2010-05-22 21:30:481701 return 1;
1702 }
René Scharfec30c10c2010-05-22 21:29:351703 if (opt->count)
1704 goto next_line;
Junio C Hamano83b5d2f2006-09-17 23:02:521705 if (binary_match_only) {
Fredrik Kuivinen5b594f42010-01-25 22:51:391706 opt->output(opt, "Binary file ", 12);
Jeff Kinge1327022012-02-02 08:19:281707 output_color(opt, gs->name, strlen(gs->name),
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:221708 opt->colors[GREP_COLOR_FILENAME]);
Fredrik Kuivinen5b594f42010-01-25 22:51:391709 opt->output(opt, " matches\n", 9);
Junio C Hamano83b5d2f2006-09-17 23:02:521710 return 1;
1711 }
Junio C Hamano83b5d2f2006-09-17 23:02:521712 /* Hit at this line. If we haven't shown the
1713 * pre-context lines, we would need to show them.
Junio C Hamano83b5d2f2006-09-17 23:02:521714 */
René Scharfeba8ea742011-08-01 17:20:531715 if (opt->pre_context || opt->funcbody)
Jeff Kinge1327022012-02-02 08:19:281716 show_pre_context(opt, gs, bol, eol, lno);
René Scharfe2944e4e2009-07-01 22:06:341717 else if (opt->funcname)
Jeff Kinge1327022012-02-02 08:19:281718 show_funcname_line(opt, gs, bol, lno);
Taylor Blau89252cd2018-06-22 15:49:421719 cno = opt->invert ? icol : col;
1720 if (cno < 0) {
1721 /*
1722 * A negative cno indicates that there was no
1723 * match on the line. We are thus inverted and
1724 * being asked to show all lines that _don't_
1725 * match a given expression. Therefore, set cno
1726 * to 0 to suggest the whole line matches.
1727 */
1728 cno = 0;
1729 }
1730 show_line(opt, bol, eol, gs->name, lno, cno + 1, ':');
René Scharfe5dd06d32009-07-01 22:02:381731 last_hit = lno;
René Scharfeba8ea742011-08-01 17:20:531732 if (opt->funcbody)
1733 show_function = 1;
1734 goto next_line;
Junio C Hamano83b5d2f2006-09-17 23:02:521735 }
René Scharfe4aa2c472016-05-28 15:06:191736 if (show_function && (!peek_bol || peek_bol < bol)) {
1737 unsigned long peek_left = left;
Jeff King1a845fb2021-09-21 03:49:491738 const char *peek_eol = eol;
René Scharfe4aa2c472016-05-28 15:06:191739
1740 /*
1741 * Trailing empty lines are not interesting.
1742 * Peek past them to see if they belong to the
1743 * body of the current function.
1744 */
1745 peek_bol = bol;
1746 while (is_empty_line(peek_bol, peek_eol)) {
1747 peek_bol = peek_eol + 1;
1748 peek_eol = end_of_line(peek_bol, &peek_left);
1749 }
1750
René Scharfe8e5dd942024-07-30 14:18:541751 if (peek_bol >= gs->buf + gs->size ||
1752 match_funcname(opt, gs, peek_bol, peek_eol))
René Scharfe4aa2c472016-05-28 15:06:191753 show_function = 0;
1754 }
René Scharfeba8ea742011-08-01 17:20:531755 if (show_function ||
1756 (last_hit && lno <= last_hit + opt->post_context)) {
Junio C Hamano83b5d2f2006-09-17 23:02:521757 /* If the last hit is within the post context,
1758 * we need to show this line.
1759 */
Taylor Blau89252cd2018-06-22 15:49:421760 show_line(opt, bol, eol, gs->name, lno, col + 1, '-');
Junio C Hamano83b5d2f2006-09-17 23:02:521761 }
Junio C Hamano83b5d2f2006-09-17 23:02:521762
1763 next_line:
1764 bol = eol + 1;
1765 if (!left)
1766 break;
1767 left--;
1768 lno++;
1769 }
1770
Junio C Hamano0ab7bef2006-09-28 00:50:521771 if (collect_hits)
1772 return 0;
Junio C Hamanob48fb5b2006-09-27 23:27:101773
Junio C Hamano83b5d2f2006-09-17 23:02:521774 if (opt->status_only)
Anthony Sottilee1f68c62017-08-18 01:38:511775 return opt->unmatch_name_only;
Junio C Hamano83b5d2f2006-09-17 23:02:521776 if (opt->unmatch_name_only) {
1777 /* We did not see any hit, so we want to show this */
Jeff Kinge1327022012-02-02 08:19:281778 show_name(opt, gs->name);
Junio C Hamano83b5d2f2006-09-17 23:02:521779 return 1;
1780 }
1781
René Scharfe60ecac92009-07-01 22:07:241782 xdiff_clear_find_func(&xecfg);
1783 opt->priv = NULL;
1784
Junio C Hamano83b5d2f2006-09-17 23:02:521785 /* NEEDSWORK:
1786 * The real "grep -c foo *.c" gives many "bar.c:0" lines,
1787 * which feels mostly useless but sometimes useful. Maybe
1788 * make it another option? For now suppress them.
1789 */
Fredrik Kuivinen5b594f42010-01-25 22:51:391790 if (opt->count && count) {
1791 char buf[32];
René Scharfef76d9472014-03-11 21:15:491792 if (opt->pathname) {
1793 output_color(opt, gs->name, strlen(gs->name),
Nguyễn Thái Ngọc Duyfa151dc2018-05-26 13:55:221794 opt->colors[GREP_COLOR_FILENAME]);
René Scharfef76d9472014-03-11 21:15:491795 output_sep(opt, ':');
1796 }
Jeff King1a168e52017-03-28 19:46:561797 xsnprintf(buf, sizeof(buf), "%u\n", count);
Fredrik Kuivinen5b594f42010-01-25 22:51:391798 opt->output(opt, buf, strlen(buf));
René Scharfec30c10c2010-05-22 21:29:351799 return 1;
Fredrik Kuivinen5b594f42010-01-25 22:51:391800 }
Junio C Hamano83b5d2f2006-09-17 23:02:521801 return !!last_hit;
1802}
1803
Junio C Hamano0ab7bef2006-09-28 00:50:521804static void clr_hit_marker(struct grep_expr *x)
1805{
1806 /* All-hit markers are meaningful only at the very top level
1807 * OR node.
1808 */
1809 while (1) {
1810 x->hit = 0;
1811 if (x->node != GREP_NODE_OR)
1812 return;
1813 x->u.binary.left->hit = 0;
1814 x = x->u.binary.right;
1815 }
1816}
1817
1818static int chk_hit_marker(struct grep_expr *x)
1819{
1820 /* Top level nodes have hit markers. See if they all are hits */
1821 while (1) {
1822 if (x->node != GREP_NODE_OR)
1823 return x->hit;
1824 if (!x->u.binary.left->hit)
1825 return 0;
1826 x = x->u.binary.right;
1827 }
1828}
1829
Jeff Kinge1327022012-02-02 08:19:281830int grep_source(struct grep_opt *opt, struct grep_source *gs)
Junio C Hamano0ab7bef2006-09-28 00:50:521831{
1832 /*
1833 * we do not have to do the two-pass grep when we do not check
1834 * buffer-wide "all-match".
1835 */
René Scharfe794c0002021-12-17 16:48:491836 if (!opt->all_match && !opt->no_body_match)
Jeff Kinge1327022012-02-02 08:19:281837 return grep_source_1(opt, gs, 0);
Junio C Hamano0ab7bef2006-09-28 00:50:521838
1839 /* Otherwise the toplevel "or" terms hit a bit differently.
1840 * We first clear hit markers from them.
1841 */
1842 clr_hit_marker(opt->pattern_expression);
René Scharfe794c0002021-12-17 16:48:491843 opt->body_hit = 0;
Jeff Kinge1327022012-02-02 08:19:281844 grep_source_1(opt, gs, 1);
Junio C Hamano0ab7bef2006-09-28 00:50:521845
René Scharfe794c0002021-12-17 16:48:491846 if (opt->all_match && !chk_hit_marker(opt->pattern_expression))
1847 return 0;
1848 if (opt->no_body_match && opt->body_hit)
Junio C Hamano0ab7bef2006-09-28 00:50:521849 return 0;
1850
Jeff Kinge1327022012-02-02 08:19:281851 return grep_source_1(opt, gs, 0);
1852}
1853
Jeff King1e668712021-09-21 03:51:281854static void grep_source_init_buf(struct grep_source *gs,
1855 const char *buf,
Jonathan Tan50d92b52021-08-16 21:09:531856 unsigned long size)
1857{
1858 gs->type = GREP_SOURCE_BUF;
1859 gs->name = NULL;
1860 gs->path = NULL;
1861 gs->buf = buf;
1862 gs->size = size;
1863 gs->driver = NULL;
1864 gs->identifier = NULL;
1865}
1866
Jeff King1e668712021-09-21 03:51:281867int grep_buffer(struct grep_opt *opt, const char *buf, unsigned long size)
Jeff Kinge1327022012-02-02 08:19:281868{
1869 struct grep_source gs;
1870 int r;
1871
Jonathan Tan50d92b52021-08-16 21:09:531872 grep_source_init_buf(&gs, buf, size);
Jeff Kinge1327022012-02-02 08:19:281873
1874 r = grep_source(opt, &gs);
1875
1876 grep_source_clear(&gs);
1877 return r;
1878}
1879
Jonathan Tan50d92b52021-08-16 21:09:531880void grep_source_init_file(struct grep_source *gs, const char *name,
1881 const char *path)
Jeff Kinge1327022012-02-02 08:19:281882{
Jonathan Tan50d92b52021-08-16 21:09:531883 gs->type = GREP_SOURCE_FILE;
Jeff King8c53f072015-01-13 01:59:091884 gs->name = xstrdup_or_null(name);
1885 gs->path = xstrdup_or_null(path);
Jeff Kinge1327022012-02-02 08:19:281886 gs->buf = NULL;
1887 gs->size = 0;
Jeff King94ad9d92012-02-02 08:20:431888 gs->driver = NULL;
Jonathan Tan50d92b52021-08-16 21:09:531889 gs->identifier = xstrdup(path);
1890}
Jeff Kinge1327022012-02-02 08:19:281891
Jonathan Tan50d92b52021-08-16 21:09:531892void grep_source_init_oid(struct grep_source *gs, const char *name,
Jonathan Tan06938062021-08-16 21:09:561893 const char *path, const struct object_id *oid,
1894 struct repository *repo)
Jonathan Tan50d92b52021-08-16 21:09:531895{
1896 gs->type = GREP_SOURCE_OID;
1897 gs->name = xstrdup_or_null(name);
1898 gs->path = xstrdup_or_null(path);
1899 gs->buf = NULL;
1900 gs->size = 0;
1901 gs->driver = NULL;
1902 gs->identifier = oiddup(oid);
Jonathan Tan06938062021-08-16 21:09:561903 gs->repo = repo;
Jeff Kinge1327022012-02-02 08:19:281904}
1905
1906void grep_source_clear(struct grep_source *gs)
1907{
Ævar Arnfjörð Bjarmason88ce3ef2017-06-15 23:15:491908 FREE_AND_NULL(gs->name);
1909 FREE_AND_NULL(gs->path);
1910 FREE_AND_NULL(gs->identifier);
Jeff Kinge1327022012-02-02 08:19:281911 grep_source_clear_data(gs);
1912}
1913
1914void grep_source_clear_data(struct grep_source *gs)
1915{
1916 switch (gs->type) {
1917 case GREP_SOURCE_FILE:
Brandon Williams1c41c822017-05-30 17:30:441918 case GREP_SOURCE_OID:
Jeff King1e668712021-09-21 03:51:281919 /* these types own the buffer */
1920 free((char *)gs->buf);
1921 gs->buf = NULL;
Jeff Kinge1327022012-02-02 08:19:281922 gs->size = 0;
1923 break;
1924 case GREP_SOURCE_BUF:
1925 /* leave user-provided buf intact */
1926 break;
1927 }
1928}
1929
Brandon Williams1c41c822017-05-30 17:30:441930static int grep_source_load_oid(struct grep_source *gs)
Jeff Kinge1327022012-02-02 08:19:281931{
1932 enum object_type type;
1933
Patrick Steinhardtd4ff88a2025-07-01 12:22:261934 gs->buf = odb_read_object(gs->repo->objects, gs->identifier,
1935 &type, &gs->size);
Jeff Kinge1327022012-02-02 08:19:281936 if (!gs->buf)
1937 return error(_("'%s': unable to read %s"),
1938 gs->name,
Brandon Williams1c41c822017-05-30 17:30:441939 oid_to_hex(gs->identifier));
Jeff Kinge1327022012-02-02 08:19:281940 return 0;
1941}
1942
1943static int grep_source_load_file(struct grep_source *gs)
1944{
1945 const char *filename = gs->identifier;
1946 struct stat st;
1947 char *data;
1948 size_t size;
1949 int i;
1950
1951 if (lstat(filename, &st) < 0) {
1952 err_ret:
1953 if (errno != ENOENT)
Nguyễn Thái Ngọc Duy7645d8f2016-05-08 09:47:471954 error_errno(_("failed to stat '%s'"), filename);
Jeff Kinge1327022012-02-02 08:19:281955 return -1;
1956 }
1957 if (!S_ISREG(st.st_mode))
1958 return -1;
1959 size = xsize_t(st.st_size);
1960 i = open(filename, O_RDONLY);
1961 if (i < 0)
1962 goto err_ret;
Jeff King3733e692016-02-22 22:44:281963 data = xmallocz(size);
Jeff Kinge1327022012-02-02 08:19:281964 if (st.st_size != read_in_full(i, data, size)) {
Nguyễn Thái Ngọc Duy7645d8f2016-05-08 09:47:471965 error_errno(_("'%s': short read"), filename);
Jeff Kinge1327022012-02-02 08:19:281966 close(i);
1967 free(data);
1968 return -1;
1969 }
1970 close(i);
Jeff Kinge1327022012-02-02 08:19:281971
1972 gs->buf = data;
1973 gs->size = size;
1974 return 0;
1975}
1976
Junio C Hamano30833012012-09-20 21:20:091977static int grep_source_load(struct grep_source *gs)
Jeff Kinge1327022012-02-02 08:19:281978{
1979 if (gs->buf)
1980 return 0;
1981
1982 switch (gs->type) {
1983 case GREP_SOURCE_FILE:
1984 return grep_source_load_file(gs);
Brandon Williams1c41c822017-05-30 17:30:441985 case GREP_SOURCE_OID:
1986 return grep_source_load_oid(gs);
Jeff Kinge1327022012-02-02 08:19:281987 case GREP_SOURCE_BUF:
1988 return gs->buf ? 0 : -1;
Jeff Kinge1327022012-02-02 08:19:281989 }
Johannes Schindelin033abf92018-05-02 09:38:391990 BUG("invalid grep_source type to load");
Junio C Hamano0ab7bef2006-09-28 00:50:521991}
Jeff King94ad9d92012-02-02 08:20:431992
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:331993void grep_source_load_driver(struct grep_source *gs,
1994 struct index_state *istate)
Jeff King94ad9d92012-02-02 08:20:431995{
1996 if (gs->driver)
1997 return;
1998
1999 grep_attr_lock();
Matheus Tavares1d1729c2020-01-16 02:39:542000 if (gs->path)
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:332001 gs->driver = userdiff_find_by_path(istate, gs->path);
Jeff King94ad9d92012-02-02 08:20:432002 if (!gs->driver)
2003 gs->driver = userdiff_find_by_name("default");
2004 grep_attr_unlock();
2005}
Jeff King41b59bf2012-02-02 08:21:022006
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:332007static int grep_source_is_binary(struct grep_source *gs,
2008 struct index_state *istate)
Jeff King41b59bf2012-02-02 08:21:022009{
Nguyễn Thái Ngọc Duyacd00ea2018-09-21 15:57:332010 grep_source_load_driver(gs, istate);
Jeff King41b59bf2012-02-02 08:21:022011 if (gs->driver->binary != -1)
2012 return gs->driver->binary;
2013
2014 if (!grep_source_load(gs))
2015 return buffer_is_binary(gs->buf, gs->size);
2016
2017 return 0;
2018}