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
18typedef int pcre_jit_stack;
19#endif
20#else
21typedef int pcre;
22typedef int pcre_extra;
23typedef int pcre_jit_stack;
24#endif
25#ifdef USE_LIBPCRE2
26#define PCRE2_CODE_UNIT_WIDTH 8
27#include <pcre2.h>
28#else
29typedef int pcre2_code;
30typedef int pcre2_match_data;
31typedef int pcre2_compile_context;
32typedef int pcre2_match_context;
33typedef int pcre2_jit_stack;
34#endif
35#include "kwset.h"
36#include "thread-utils.h"
37#include "userdiff.h"
38
39struct repository;
40
41enum 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
52enum grep_context {
53 GREP_CONTEXT_HEAD,
54 GREP_CONTEXT_BODY
55};
56
57enum 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
67enum 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
80struct 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
106enum 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
114enum 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
122struct 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
135struct 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;
184
185 void (*output)(struct grep_opt *opt, const void *data, size_t size);
186 void *output_priv;
187};
188
189extern void init_grep_defaults(struct repository *);
190extern int grep_config(const char *var, const char *value, void *);
191extern void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
192void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
193
194extern void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
195extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
196extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
197extern void compile_grep_patterns(struct grep_opt *opt);
198extern void free_grep_patterns(struct grep_opt *opt);
199extern int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
200
201struct grep_source {
202 char *name;
203
204 enum grep_source_type {
205 GREP_SOURCE_OID,
206 GREP_SOURCE_FILE,
207 GREP_SOURCE_BUF,
208 } type;
209 void *identifier;
210
211 char *buf;
212 unsigned long size;
213
214 char *path; /* for attribute lookups */
215 struct userdiff_driver *driver;
216};
217
218void grep_source_init(struct grep_source *gs, enum grep_source_type type,
219 const char *name, const char *path,
220 const void *identifier);
221void grep_source_clear_data(struct grep_source *gs);
222void grep_source_clear(struct grep_source *gs);
223void grep_source_load_driver(struct grep_source *gs,
224 struct index_state *istate);
225
226
227int grep_source(struct grep_opt *opt, struct grep_source *gs);
228
229extern struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
230extern int grep_threads_ok(const struct grep_opt *opt);
231
232/*
233 * Mutex used around access to the attributes machinery if
234 * opt->use_threads. Must be initialized/destroyed by callers!
235 */
236extern int grep_use_locks;
237extern pthread_mutex_t grep_attr_mutex;
238extern pthread_mutex_t grep_read_mutex;
239
240static inline void grep_read_lock(void)
241{
242 if (grep_use_locks)
243 pthread_mutex_lock(&grep_read_mutex);
244}
245
246static inline void grep_read_unlock(void)
247{
248 if (grep_use_locks)
249 pthread_mutex_unlock(&grep_read_mutex);
250}
251
252#endif