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