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