grep: create a "is_fixed" member in "grep_pat"
[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 is_fixed:1;
92         unsigned ignore_case:1;
93         unsigned word_regexp:1;
94 };
95
96 enum grep_expr_node {
97         GREP_NODE_ATOM,
98         GREP_NODE_NOT,
99         GREP_NODE_AND,
100         GREP_NODE_TRUE,
101         GREP_NODE_OR
102 };
103
104 enum grep_pattern_type {
105         GREP_PATTERN_TYPE_UNSPECIFIED = 0,
106         GREP_PATTERN_TYPE_BRE,
107         GREP_PATTERN_TYPE_ERE,
108         GREP_PATTERN_TYPE_FIXED,
109         GREP_PATTERN_TYPE_PCRE
110 };
111
112 struct grep_expr {
113         enum grep_expr_node node;
114         unsigned hit;
115         union {
116                 struct grep_pat *atom;
117                 struct grep_expr *unary;
118                 struct {
119                         struct grep_expr *left;
120                         struct grep_expr *right;
121                 } binary;
122         } u;
123 };
124
125 struct grep_opt {
126         struct grep_pat *pattern_list;
127         struct grep_pat **pattern_tail;
128         struct grep_pat *header_list;
129         struct grep_pat **header_tail;
130         struct grep_expr *pattern_expression;
131         struct repository *repo;
132         const char *prefix;
133         int prefix_length;
134         regex_t regexp;
135         int linenum;
136         int columnnum;
137         int invert;
138         int ignore_case;
139         int status_only;
140         int name_only;
141         int unmatch_name_only;
142         int count;
143         int word_regexp;
144         int fixed;
145         int all_match;
146         int debug;
147 #define GREP_BINARY_DEFAULT     0
148 #define GREP_BINARY_NOMATCH     1
149 #define GREP_BINARY_TEXT        2
150         int binary;
151         int allow_textconv;
152         int extended;
153         int use_reflog_filter;
154         int pcre1;
155         int pcre2;
156         int relative;
157         int pathname;
158         int null_following_name;
159         int only_matching;
160         int color;
161         int max_depth;
162         int funcname;
163         int funcbody;
164         int extended_regexp_option;
165         int pattern_type_option;
166         int ignore_locale;
167         char colors[NR_GREP_COLORS][COLOR_MAXLEN];
168         unsigned pre_context;
169         unsigned post_context;
170         unsigned last_shown;
171         int show_hunk_mark;
172         int file_break;
173         int heading;
174         void *priv;
175
176         void (*output)(struct grep_opt *opt, const void *data, size_t size);
177         void *output_priv;
178 };
179
180 void init_grep_defaults(struct repository *);
181 int grep_config(const char *var, const char *value, void *);
182 void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
183 void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
184
185 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
186 void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
187 void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
188 void compile_grep_patterns(struct grep_opt *opt);
189 void free_grep_patterns(struct grep_opt *opt);
190 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
191
192 struct grep_source {
193         char *name;
194
195         enum grep_source_type {
196                 GREP_SOURCE_OID,
197                 GREP_SOURCE_FILE,
198                 GREP_SOURCE_BUF,
199         } type;
200         void *identifier;
201
202         char *buf;
203         unsigned long size;
204
205         char *path; /* for attribute lookups */
206         struct userdiff_driver *driver;
207 };
208
209 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
210                       const char *name, const char *path,
211                       const void *identifier);
212 void grep_source_clear_data(struct grep_source *gs);
213 void grep_source_clear(struct grep_source *gs);
214 void grep_source_load_driver(struct grep_source *gs,
215                              struct index_state *istate);
216
217
218 int grep_source(struct grep_opt *opt, struct grep_source *gs);
219
220 struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
221 int grep_threads_ok(const struct grep_opt *opt);
222
223 /*
224  * Mutex used around access to the attributes machinery if
225  * opt->use_threads.  Must be initialized/destroyed by callers!
226  */
227 extern int grep_use_locks;
228 extern pthread_mutex_t grep_attr_mutex;
229 extern pthread_mutex_t grep_read_mutex;
230
231 static inline void grep_read_lock(void)
232 {
233         if (grep_use_locks)
234                 pthread_mutex_lock(&grep_read_mutex);
235 }
236
237 static inline void grep_read_unlock(void)
238 {
239         if (grep_use_locks)
240                 pthread_mutex_unlock(&grep_read_mutex);
241 }
242
243 #endif