grep: use PCRE v2 for optimized fixed-string search
[git] / grep.h
1 #ifndef GREP_H
2 #define GREP_H
3 #include "color.h"
4 #ifdef USE_LIBPCRE1
5 #include <pcre.h>
6 #ifdef PCRE_CONFIG_JIT
7 #if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
8 #ifndef NO_LIBPCRE1_JIT
9 #define GIT_PCRE1_USE_JIT
10 #define GIT_PCRE_STUDY_JIT_COMPILE PCRE_STUDY_JIT_COMPILE
11 #endif
12 #endif
13 #endif
14 #ifndef GIT_PCRE_STUDY_JIT_COMPILE
15 #define GIT_PCRE_STUDY_JIT_COMPILE 0
16 #endif
17 #if PCRE_MAJOR <= 8 && PCRE_MINOR < 20
18 typedef int pcre_jit_stack;
19 #endif
20 #else
21 typedef int pcre;
22 typedef int pcre_extra;
23 typedef int pcre_jit_stack;
24 #endif
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
35 #include "thread-utils.h"
36 #include "userdiff.h"
37
38 struct repository;
39
40 enum grep_pat_token {
41         GREP_PATTERN,
42         GREP_PATTERN_HEAD,
43         GREP_PATTERN_BODY,
44         GREP_AND,
45         GREP_OPEN_PAREN,
46         GREP_CLOSE_PAREN,
47         GREP_NOT,
48         GREP_OR
49 };
50
51 enum grep_context {
52         GREP_CONTEXT_HEAD,
53         GREP_CONTEXT_BODY
54 };
55
56 enum grep_header_field {
57         GREP_HEADER_FIELD_MIN = 0,
58         GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN,
59         GREP_HEADER_COMMITTER,
60         GREP_HEADER_REFLOG,
61
62         /* Must be at the end of the enum */
63         GREP_HEADER_FIELD_MAX
64 };
65
66 enum grep_color {
67         GREP_COLOR_CONTEXT,
68         GREP_COLOR_FILENAME,
69         GREP_COLOR_FUNCTION,
70         GREP_COLOR_LINENO,
71         GREP_COLOR_COLUMNNO,
72         GREP_COLOR_MATCH_CONTEXT,
73         GREP_COLOR_MATCH_SELECTED,
74         GREP_COLOR_SELECTED,
75         GREP_COLOR_SEP,
76         NR_GREP_COLORS
77 };
78
79 struct grep_pat {
80         struct grep_pat *next;
81         const char *origin;
82         int no;
83         enum grep_pat_token token;
84         char *pattern;
85         size_t patternlen;
86         enum grep_header_field field;
87         regex_t regexp;
88         pcre *pcre1_regexp;
89         pcre_extra *pcre1_extra_info;
90         pcre_jit_stack *pcre1_jit_stack;
91         const unsigned char *pcre1_tables;
92         int pcre1_jit_on;
93         pcre2_code *pcre2_pattern;
94         pcre2_match_data *pcre2_match_data;
95         pcre2_compile_context *pcre2_compile_context;
96         pcre2_match_context *pcre2_match_context;
97         pcre2_jit_stack *pcre2_jit_stack;
98         uint32_t pcre2_jit_on;
99         unsigned fixed:1;
100         unsigned ignore_case:1;
101         unsigned word_regexp:1;
102 };
103
104 enum grep_expr_node {
105         GREP_NODE_ATOM,
106         GREP_NODE_NOT,
107         GREP_NODE_AND,
108         GREP_NODE_TRUE,
109         GREP_NODE_OR
110 };
111
112 enum grep_pattern_type {
113         GREP_PATTERN_TYPE_UNSPECIFIED = 0,
114         GREP_PATTERN_TYPE_BRE,
115         GREP_PATTERN_TYPE_ERE,
116         GREP_PATTERN_TYPE_FIXED,
117         GREP_PATTERN_TYPE_PCRE
118 };
119
120 struct grep_expr {
121         enum grep_expr_node node;
122         unsigned hit;
123         union {
124                 struct grep_pat *atom;
125                 struct grep_expr *unary;
126                 struct {
127                         struct grep_expr *left;
128                         struct grep_expr *right;
129                 } binary;
130         } u;
131 };
132
133 struct grep_opt {
134         struct grep_pat *pattern_list;
135         struct grep_pat **pattern_tail;
136         struct grep_pat *header_list;
137         struct grep_pat **header_tail;
138         struct grep_expr *pattern_expression;
139         struct repository *repo;
140         const char *prefix;
141         int prefix_length;
142         regex_t regexp;
143         int linenum;
144         int columnnum;
145         int invert;
146         int ignore_case;
147         int status_only;
148         int name_only;
149         int unmatch_name_only;
150         int count;
151         int word_regexp;
152         int fixed;
153         int all_match;
154         int debug;
155 #define GREP_BINARY_DEFAULT     0
156 #define GREP_BINARY_NOMATCH     1
157 #define GREP_BINARY_TEXT        2
158         int binary;
159         int allow_textconv;
160         int extended;
161         int use_reflog_filter;
162         int pcre1;
163         int pcre2;
164         int relative;
165         int pathname;
166         int null_following_name;
167         int only_matching;
168         int color;
169         int max_depth;
170         int funcname;
171         int funcbody;
172         int extended_regexp_option;
173         int pattern_type_option;
174         int ignore_locale;
175         char colors[NR_GREP_COLORS][COLOR_MAXLEN];
176         unsigned pre_context;
177         unsigned post_context;
178         unsigned last_shown;
179         int show_hunk_mark;
180         int file_break;
181         int heading;
182         void *priv;
183
184         void (*output)(struct grep_opt *opt, const void *data, size_t size);
185         void *output_priv;
186 };
187
188 void init_grep_defaults(struct repository *);
189 int grep_config(const char *var, const char *value, void *);
190 void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
191 void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
192
193 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
194 void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
195 void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
196 void compile_grep_patterns(struct grep_opt *opt);
197 void free_grep_patterns(struct grep_opt *opt);
198 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
199
200 struct grep_source {
201         char *name;
202
203         enum grep_source_type {
204                 GREP_SOURCE_OID,
205                 GREP_SOURCE_FILE,
206                 GREP_SOURCE_BUF,
207         } type;
208         void *identifier;
209
210         char *buf;
211         unsigned long size;
212
213         char *path; /* for attribute lookups */
214         struct userdiff_driver *driver;
215 };
216
217 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
218                       const char *name, const char *path,
219                       const void *identifier);
220 void grep_source_clear_data(struct grep_source *gs);
221 void grep_source_clear(struct grep_source *gs);
222 void grep_source_load_driver(struct grep_source *gs,
223                              struct index_state *istate);
224
225
226 int grep_source(struct grep_opt *opt, struct grep_source *gs);
227
228 struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
229 int grep_threads_ok(const struct grep_opt *opt);
230
231 /*
232  * Mutex used around access to the attributes machinery if
233  * opt->use_threads.  Must be initialized/destroyed by callers!
234  */
235 extern int grep_use_locks;
236 extern pthread_mutex_t grep_attr_mutex;
237 extern pthread_mutex_t grep_read_mutex;
238
239 static inline void grep_read_lock(void)
240 {
241         if (grep_use_locks)
242                 pthread_mutex_lock(&grep_read_mutex);
243 }
244
245 static inline void grep_read_unlock(void)
246 {
247         if (grep_use_locks)
248                 pthread_mutex_unlock(&grep_read_mutex);
249 }
250
251 #endif