Merge branch 'master' of git://git.kernel.org/pub/scm/git/git
[git.git] / grep.h
blob1875880f37990f981865b0fab332cc6c6c4e5971
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"
39 struct repository;
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
52 enum grep_context {
53 GREP_CONTEXT_HEAD,
54 GREP_CONTEXT_BODY
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,
63 /* Must be at the end of the enum */
64 GREP_HEADER_FIELD_MAX
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
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;
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
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
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;
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 char colors[NR_GREP_COLORS][COLOR_MAXLEN];
177 unsigned pre_context;
178 unsigned post_context;
179 unsigned last_shown;
180 int show_hunk_mark;
181 int file_break;
182 int heading;
183 void *priv;
185 void (*output)(struct grep_opt *opt, const void *data, size_t size);
186 void *output_priv;
189 void init_grep_defaults(struct repository *);
190 int grep_config(const char *var, const char *value, void *);
191 void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
192 void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
194 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
195 void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
196 void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
197 void compile_grep_patterns(struct grep_opt *opt);
198 void free_grep_patterns(struct grep_opt *opt);
199 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
201 struct grep_source {
202 char *name;
204 enum grep_source_type {
205 GREP_SOURCE_OID,
206 GREP_SOURCE_FILE,
207 GREP_SOURCE_BUF,
208 } type;
209 void *identifier;
211 char *buf;
212 unsigned long size;
214 char *path; /* for attribute lookups */
215 struct userdiff_driver *driver;
218 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
219 const char *name, const char *path,
220 const void *identifier);
221 void grep_source_clear_data(struct grep_source *gs);
222 void grep_source_clear(struct grep_source *gs);
223 void grep_source_load_driver(struct grep_source *gs,
224 struct index_state *istate);
227 int grep_source(struct grep_opt *opt, struct grep_source *gs);
229 struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
230 int grep_threads_ok(const struct grep_opt *opt);
233 * Mutex used around access to the attributes machinery if
234 * opt->use_threads. Must be initialized/destroyed by callers!
236 extern int grep_use_locks;
237 extern pthread_mutex_t grep_attr_mutex;
238 extern pthread_mutex_t grep_read_mutex;
240 static inline void grep_read_lock(void)
242 if (grep_use_locks)
243 pthread_mutex_lock(&grep_read_mutex);
246 static inline void grep_read_unlock(void)
248 if (grep_use_locks)
249 pthread_mutex_unlock(&grep_read_mutex);
252 #endif