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