Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1 | #ifndef GREP_H |
| 2 | #define GREP_H |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 3 | #include "color.h" |
Ævar Arnfjörð Bjarmason | 3485bea | 2017-05-25 19:45:28 +0000 | [diff] [blame] | 4 | #ifdef USE_LIBPCRE1 |
Michał Kiedrowicz | 63e7e9d | 2011-05-09 23:52:05 +0200 | [diff] [blame] | 5 | #include <pcre.h> |
Ævar Arnfjörð Bjarmason | e87de7c | 2017-05-25 20:05:26 +0000 | [diff] [blame] | 6 | #ifdef PCRE_CONFIG_JIT |
| 7 | #if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32 |
Ævar Arnfjörð Bjarmason | fb95e2e | 2017-06-01 18:20:55 +0000 | [diff] [blame] | 8 | #ifndef NO_LIBPCRE1_JIT |
Ævar Arnfjörð Bjarmason | e87de7c | 2017-05-25 20:05:26 +0000 | [diff] [blame] | 9 | #define GIT_PCRE1_USE_JIT |
Charles Bailey | 2fff1e1 | 2017-11-12 16:59:38 +0000 | [diff] [blame] | 10 | #define GIT_PCRE_STUDY_JIT_COMPILE PCRE_STUDY_JIT_COMPILE |
Ævar Arnfjörð Bjarmason | e87de7c | 2017-05-25 20:05:26 +0000 | [diff] [blame] | 11 | #endif |
| 12 | #endif |
Ævar Arnfjörð Bjarmason | fb95e2e | 2017-06-01 18:20:55 +0000 | [diff] [blame] | 13 | #endif |
Charles Bailey | 2fff1e1 | 2017-11-12 16:59:38 +0000 | [diff] [blame] | 14 | #ifndef GIT_PCRE_STUDY_JIT_COMPILE |
| 15 | #define GIT_PCRE_STUDY_JIT_COMPILE 0 |
Ævar Arnfjörð Bjarmason | fbaceaa | 2017-05-25 20:05:25 +0000 | [diff] [blame] | 16 | #endif |
Ævar Arnfjörð Bjarmason | c30cf82 | 2017-05-25 20:05:27 +0000 | [diff] [blame] | 17 | #if PCRE_MAJOR <= 8 && PCRE_MINOR < 20 |
| 18 | typedef int pcre_jit_stack; |
| 19 | #endif |
Michał Kiedrowicz | 63e7e9d | 2011-05-09 23:52:05 +0200 | [diff] [blame] | 20 | #else |
| 21 | typedef int pcre; |
| 22 | typedef int pcre_extra; |
Ævar Arnfjörð Bjarmason | fbaceaa | 2017-05-25 20:05:25 +0000 | [diff] [blame] | 23 | typedef int pcre_jit_stack; |
Michał Kiedrowicz | 63e7e9d | 2011-05-09 23:52:05 +0200 | [diff] [blame] | 24 | #endif |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 25 | #ifdef USE_LIBPCRE2 |
| 26 | #define PCRE2_CODE_UNIT_WIDTH 8 |
| 27 | #include <pcre2.h> |
| 28 | #else |
| 29 | typedef int pcre2_code; |
| 30 | typedef int pcre2_match_data; |
| 31 | typedef int pcre2_compile_context; |
| 32 | typedef int pcre2_match_context; |
| 33 | typedef int pcre2_jit_stack; |
| 34 | #endif |
Fredrik Kuivinen | 9ecedde | 2011-08-21 00:42:18 +0200 | [diff] [blame] | 35 | #include "kwset.h" |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 36 | #include "thread-utils.h" |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 37 | #include "userdiff.h" |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 38 | |
Nguyễn Thái Ngọc Duy | 38bbc2e | 2018-09-21 17:57:23 +0200 | [diff] [blame] | 39 | struct repository; |
| 40 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 41 | enum grep_pat_token { |
| 42 | GREP_PATTERN, |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 43 | GREP_PATTERN_HEAD, |
| 44 | GREP_PATTERN_BODY, |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 45 | GREP_AND, |
| 46 | GREP_OPEN_PAREN, |
| 47 | GREP_CLOSE_PAREN, |
| 48 | GREP_NOT, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 49 | GREP_OR |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 50 | }; |
| 51 | |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 52 | enum grep_context { |
| 53 | GREP_CONTEXT_HEAD, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 54 | GREP_CONTEXT_BODY |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 57 | enum grep_header_field { |
Antoine Pelisse | 3ce3ffb | 2013-02-03 14:37:09 +0000 | [diff] [blame] | 58 | GREP_HEADER_FIELD_MIN = 0, |
| 59 | GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN, |
Nguyễn Thái Ngọc Duy | ad4813b | 2012-09-29 11:41:27 +0700 | [diff] [blame] | 60 | GREP_HEADER_COMMITTER, |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 61 | GREP_HEADER_REFLOG, |
Nguyễn Thái Ngọc Duy | ad4813b | 2012-09-29 11:41:27 +0700 | [diff] [blame] | 62 | |
| 63 | /* Must be at the end of the enum */ |
| 64 | GREP_HEADER_FIELD_MAX |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 67 | enum grep_color { |
| 68 | GREP_COLOR_CONTEXT, |
| 69 | GREP_COLOR_FILENAME, |
| 70 | GREP_COLOR_FUNCTION, |
| 71 | GREP_COLOR_LINENO, |
Junio C Hamano | d036d66 | 2018-07-18 12:20:31 -0700 | [diff] [blame] | 72 | GREP_COLOR_COLUMNNO, |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 73 | GREP_COLOR_MATCH_CONTEXT, |
| 74 | GREP_COLOR_MATCH_SELECTED, |
| 75 | GREP_COLOR_SELECTED, |
| 76 | GREP_COLOR_SEP, |
| 77 | NR_GREP_COLORS |
| 78 | }; |
| 79 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 80 | struct grep_pat { |
| 81 | struct grep_pat *next; |
| 82 | const char *origin; |
| 83 | int no; |
| 84 | enum grep_pat_token token; |
René Scharfe | 526a858 | 2012-05-20 16:33:07 +0200 | [diff] [blame] | 85 | char *pattern; |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 86 | size_t patternlen; |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 87 | enum grep_header_field field; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 88 | regex_t regexp; |
Ævar Arnfjörð Bjarmason | 6d4b574 | 2017-05-25 19:45:29 +0000 | [diff] [blame] | 89 | pcre *pcre1_regexp; |
| 90 | pcre_extra *pcre1_extra_info; |
Ævar Arnfjörð Bjarmason | fbaceaa | 2017-05-25 20:05:25 +0000 | [diff] [blame] | 91 | pcre_jit_stack *pcre1_jit_stack; |
Ævar Arnfjörð Bjarmason | 6d4b574 | 2017-05-25 19:45:29 +0000 | [diff] [blame] | 92 | const unsigned char *pcre1_tables; |
Ævar Arnfjörð Bjarmason | fbaceaa | 2017-05-25 20:05:25 +0000 | [diff] [blame] | 93 | int pcre1_jit_on; |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 94 | pcre2_code *pcre2_pattern; |
| 95 | pcre2_match_data *pcre2_match_data; |
| 96 | pcre2_compile_context *pcre2_compile_context; |
| 97 | pcre2_match_context *pcre2_match_context; |
| 98 | pcre2_jit_stack *pcre2_jit_stack; |
| 99 | uint32_t pcre2_jit_on; |
Fredrik Kuivinen | 9ecedde | 2011-08-21 00:42:18 +0200 | [diff] [blame] | 100 | kwset_t kws; |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 101 | unsigned fixed:1; |
Brian Collins | 5183bf6 | 2009-11-06 01:22:35 -0800 | [diff] [blame] | 102 | unsigned ignore_case:1; |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 103 | unsigned word_regexp:1; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | enum grep_expr_node { |
| 107 | GREP_NODE_ATOM, |
| 108 | GREP_NODE_NOT, |
| 109 | GREP_NODE_AND, |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 110 | GREP_NODE_TRUE, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 111 | GREP_NODE_OR |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
J Smith | 84befcd | 2012-08-03 10:53:50 -0400 | [diff] [blame] | 114 | enum grep_pattern_type { |
| 115 | GREP_PATTERN_TYPE_UNSPECIFIED = 0, |
| 116 | GREP_PATTERN_TYPE_BRE, |
| 117 | GREP_PATTERN_TYPE_ERE, |
| 118 | GREP_PATTERN_TYPE_FIXED, |
| 119 | GREP_PATTERN_TYPE_PCRE |
| 120 | }; |
| 121 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 122 | struct grep_expr { |
| 123 | enum grep_expr_node node; |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 124 | unsigned hit; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 125 | union { |
| 126 | struct grep_pat *atom; |
| 127 | struct grep_expr *unary; |
| 128 | struct { |
| 129 | struct grep_expr *left; |
| 130 | struct grep_expr *right; |
| 131 | } binary; |
| 132 | } u; |
| 133 | }; |
| 134 | |
| 135 | struct grep_opt { |
| 136 | struct grep_pat *pattern_list; |
| 137 | struct grep_pat **pattern_tail; |
Junio C Hamano | 80235ba | 2010-01-17 20:09:06 -0800 | [diff] [blame] | 138 | struct grep_pat *header_list; |
| 139 | struct grep_pat **header_tail; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 140 | struct grep_expr *pattern_expression; |
Nguyễn Thái Ngọc Duy | 38bbc2e | 2018-09-21 17:57:23 +0200 | [diff] [blame] | 141 | struct repository *repo; |
Clemens Buchacher | 493b7a0 | 2009-09-05 14:31:17 +0200 | [diff] [blame] | 142 | const char *prefix; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 143 | int prefix_length; |
| 144 | regex_t regexp; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 145 | int linenum; |
Taylor Blau | 017c0fc | 2018-06-22 10:49:39 -0500 | [diff] [blame] | 146 | int columnnum; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 147 | int invert; |
Brian Collins | 5183bf6 | 2009-11-06 01:22:35 -0800 | [diff] [blame] | 148 | int ignore_case; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 149 | int status_only; |
| 150 | int name_only; |
| 151 | int unmatch_name_only; |
| 152 | int count; |
| 153 | int word_regexp; |
| 154 | int fixed; |
| 155 | int all_match; |
Junio C Hamano | 17bf35a | 2012-09-13 14:21:44 -0700 | [diff] [blame] | 156 | int debug; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 157 | #define GREP_BINARY_DEFAULT 0 |
| 158 | #define GREP_BINARY_NOMATCH 1 |
| 159 | #define GREP_BINARY_TEXT 2 |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 160 | int binary; |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 161 | int allow_textconv; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 162 | int extended; |
Junio C Hamano | baa6378 | 2012-09-29 11:59:52 -0700 | [diff] [blame] | 163 | int use_reflog_filter; |
Ævar Arnfjörð Bjarmason | 6d4b574 | 2017-05-25 19:45:29 +0000 | [diff] [blame] | 164 | int pcre1; |
Ævar Arnfjörð Bjarmason | 94da919 | 2017-06-01 18:20:56 +0000 | [diff] [blame] | 165 | int pcre2; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 166 | int relative; |
| 167 | int pathname; |
| 168 | int null_following_name; |
Taylor Blau | 9d8db06 | 2018-07-09 15:33:47 -0500 | [diff] [blame] | 169 | int only_matching; |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 170 | int color; |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame] | 171 | int max_depth; |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 172 | int funcname; |
René Scharfe | ba8ea74 | 2011-08-01 19:20:53 +0200 | [diff] [blame] | 173 | int funcbody; |
J Smith | 84befcd | 2012-08-03 10:53:50 -0400 | [diff] [blame] | 174 | int extended_regexp_option; |
| 175 | int pattern_type_option; |
Nguyễn Thái Ngọc Duy | fa151dc | 2018-05-26 15:55:22 +0200 | [diff] [blame] | 176 | char colors[NR_GREP_COLORS][COLOR_MAXLEN]; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 177 | unsigned pre_context; |
| 178 | unsigned post_context; |
René Scharfe | 5dd06d3 | 2009-07-02 00:02:38 +0200 | [diff] [blame] | 179 | unsigned last_shown; |
René Scharfe | 046802d | 2009-07-02 00:03:44 +0200 | [diff] [blame] | 180 | int show_hunk_mark; |
René Scharfe | a8f0e76 | 2011-06-05 17:24:25 +0200 | [diff] [blame] | 181 | int file_break; |
René Scharfe | 1d84f72 | 2011-06-05 17:24:36 +0200 | [diff] [blame] | 182 | int heading; |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 183 | void *priv; |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 184 | |
| 185 | void (*output)(struct grep_opt *opt, const void *data, size_t size); |
| 186 | void *output_priv; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 187 | }; |
| 188 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 189 | void init_grep_defaults(struct repository *); |
| 190 | int grep_config(const char *var, const char *value, void *); |
| 191 | void grep_init(struct grep_opt *, struct repository *repo, const char *prefix); |
Junio C Hamano | c5c31d3 | 2012-10-03 14:47:48 -0700 | [diff] [blame] | 192 | void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt); |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 193 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 194 | void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t); |
| 195 | void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t); |
| 196 | void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *); |
| 197 | void compile_grep_patterns(struct grep_opt *opt); |
| 198 | void free_grep_patterns(struct grep_opt *opt); |
| 199 | int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 200 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 201 | struct grep_source { |
| 202 | char *name; |
| 203 | |
| 204 | enum grep_source_type { |
Brandon Williams | 1c41c82 | 2017-05-30 10:30:44 -0700 | [diff] [blame] | 205 | GREP_SOURCE_OID, |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 206 | GREP_SOURCE_FILE, |
| 207 | GREP_SOURCE_BUF, |
| 208 | } type; |
| 209 | void *identifier; |
| 210 | |
| 211 | char *buf; |
| 212 | unsigned long size; |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 213 | |
Nguyễn Thái Ngọc Duy | 55c6168 | 2012-10-12 17:49:38 +0700 | [diff] [blame] | 214 | char *path; /* for attribute lookups */ |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 215 | struct userdiff_driver *driver; |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 216 | }; |
| 217 | |
| 218 | void grep_source_init(struct grep_source *gs, enum grep_source_type type, |
Nguyễn Thái Ngọc Duy | 55c6168 | 2012-10-12 17:49:38 +0700 | [diff] [blame] | 219 | const char *name, const char *path, |
| 220 | const void *identifier); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 221 | void grep_source_clear_data(struct grep_source *gs); |
| 222 | void grep_source_clear(struct grep_source *gs); |
Nguyễn Thái Ngọc Duy | acd00ea | 2018-09-21 17:57:33 +0200 | [diff] [blame] | 223 | void grep_source_load_driver(struct grep_source *gs, |
| 224 | struct index_state *istate); |
Junio C Hamano | 07a7d65 | 2012-09-15 14:04:36 -0700 | [diff] [blame] | 225 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 226 | |
| 227 | int grep_source(struct grep_opt *opt, struct grep_source *gs); |
| 228 | |
Denton Liu | 5545442 | 2019-04-29 04:28:14 -0400 | [diff] [blame] | 229 | struct grep_opt *grep_opt_dup(const struct grep_opt *opt); |
| 230 | int grep_threads_ok(const struct grep_opt *opt); |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 231 | |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 232 | /* |
| 233 | * Mutex used around access to the attributes machinery if |
| 234 | * opt->use_threads. Must be initialized/destroyed by callers! |
| 235 | */ |
Jeff King | 78db6ea | 2012-02-02 03:18:29 -0500 | [diff] [blame] | 236 | extern int grep_use_locks; |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 237 | extern pthread_mutex_t grep_attr_mutex; |
Jeff King | b3aeb28 | 2012-02-02 03:18:41 -0500 | [diff] [blame] | 238 | extern pthread_mutex_t grep_read_mutex; |
| 239 | |
| 240 | static inline void grep_read_lock(void) |
| 241 | { |
| 242 | if (grep_use_locks) |
| 243 | pthread_mutex_lock(&grep_read_mutex); |
| 244 | } |
| 245 | |
| 246 | static inline void grep_read_unlock(void) |
| 247 | { |
| 248 | if (grep_use_locks) |
| 249 | pthread_mutex_unlock(&grep_read_mutex); |
| 250 | } |
| 251 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 252 | #endif |