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