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