]> git.ipfire.org Git - thirdparty/git.git/blob - grep.h
grep tests: move binary pattern tests into their own file
[thirdparty/git.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 uint32_t pcre2_jit_on;
100 kwset_t kws;
101 unsigned fixed:1;
102 unsigned ignore_case:1;
103 unsigned word_regexp:1;
104 };
105
106 enum grep_expr_node {
107 GREP_NODE_ATOM,
108 GREP_NODE_NOT,
109 GREP_NODE_AND,
110 GREP_NODE_TRUE,
111 GREP_NODE_OR
112 };
113
114 enum grep_pattern_type {
115 GREP_PATTERN_TYPE_UNSPECIFIED = 0,
116 GREP_PATTERN_TYPE_BRE,
117 GREP_PATTERN_TYPE_ERE,
118 GREP_PATTERN_TYPE_FIXED,
119 GREP_PATTERN_TYPE_PCRE
120 };
121
122 struct grep_expr {
123 enum grep_expr_node node;
124 unsigned hit;
125 union {
126 struct grep_pat *atom;
127 struct grep_expr *unary;
128 struct {
129 struct grep_expr *left;
130 struct grep_expr *right;
131 } binary;
132 } u;
133 };
134
135 struct grep_opt {
136 struct grep_pat *pattern_list;
137 struct grep_pat **pattern_tail;
138 struct grep_pat *header_list;
139 struct grep_pat **header_tail;
140 struct grep_expr *pattern_expression;
141 struct repository *repo;
142 const char *prefix;
143 int prefix_length;
144 regex_t regexp;
145 int linenum;
146 int columnnum;
147 int invert;
148 int ignore_case;
149 int status_only;
150 int name_only;
151 int unmatch_name_only;
152 int count;
153 int word_regexp;
154 int fixed;
155 int all_match;
156 int debug;
157 #define GREP_BINARY_DEFAULT 0
158 #define GREP_BINARY_NOMATCH 1
159 #define GREP_BINARY_TEXT 2
160 int binary;
161 int allow_textconv;
162 int extended;
163 int use_reflog_filter;
164 int pcre1;
165 int pcre2;
166 int relative;
167 int pathname;
168 int null_following_name;
169 int only_matching;
170 int color;
171 int max_depth;
172 int funcname;
173 int funcbody;
174 int extended_regexp_option;
175 int pattern_type_option;
176 int ignore_locale;
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_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
194
195 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
196 void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
197 void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
198 void compile_grep_patterns(struct grep_opt *opt);
199 void free_grep_patterns(struct grep_opt *opt);
200 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
201
202 struct grep_source {
203 char *name;
204
205 enum grep_source_type {
206 GREP_SOURCE_OID,
207 GREP_SOURCE_FILE,
208 GREP_SOURCE_BUF,
209 } type;
210 void *identifier;
211
212 char *buf;
213 unsigned long size;
214
215 char *path; /* for attribute lookups */
216 struct userdiff_driver *driver;
217 };
218
219 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
220 const char *name, const char *path,
221 const void *identifier);
222 void grep_source_clear_data(struct grep_source *gs);
223 void grep_source_clear(struct grep_source *gs);
224 void grep_source_load_driver(struct grep_source *gs,
225 struct index_state *istate);
226
227
228 int grep_source(struct grep_opt *opt, struct grep_source *gs);
229
230 struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
231 int grep_threads_ok(const struct grep_opt *opt);
232
233 /*
234 * Mutex used around access to the attributes machinery if
235 * opt->use_threads. Must be initialized/destroyed by callers!
236 */
237 extern int grep_use_locks;
238 extern pthread_mutex_t grep_attr_mutex;
239 extern pthread_mutex_t grep_read_mutex;
240
241 static inline void grep_read_lock(void)
242 {
243 if (grep_use_locks)
244 pthread_mutex_lock(&grep_read_mutex);
245 }
246
247 static inline void grep_read_unlock(void)
248 {
249 if (grep_use_locks)
250 pthread_mutex_unlock(&grep_read_mutex);
251 }
252
253 #endif