THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2206 lines
53KB

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