Remove support for v1 of the PCRE library
[git] / grep.h
1 #ifndef GREP_H
2 #define GREP_H
3 #include "color.h"
4 #ifdef USE_LIBPCRE2
5 #define PCRE2_CODE_UNIT_WIDTH 8
6 #include <pcre2.h>
7 #else
8 typedef int pcre2_code;
9 typedef int pcre2_match_data;
10 typedef int pcre2_compile_context;
11 #endif
12 #include "thread-utils.h"
13 #include "userdiff.h"
14
15 struct repository;
16
17 enum grep_pat_token {
18         GREP_PATTERN,
19         GREP_PATTERN_HEAD,
20         GREP_PATTERN_BODY,
21         GREP_AND,
22         GREP_OPEN_PAREN,
23         GREP_CLOSE_PAREN,
24         GREP_NOT,
25         GREP_OR
26 };
27
28 enum grep_context {
29         GREP_CONTEXT_HEAD,
30         GREP_CONTEXT_BODY
31 };
32
33 enum grep_header_field {
34         GREP_HEADER_FIELD_MIN = 0,
35         GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN,
36         GREP_HEADER_COMMITTER,
37         GREP_HEADER_REFLOG,
38
39         /* Must be at the end of the enum */
40         GREP_HEADER_FIELD_MAX
41 };
42
43 enum grep_color {
44         GREP_COLOR_CONTEXT,
45         GREP_COLOR_FILENAME,
46         GREP_COLOR_FUNCTION,
47         GREP_COLOR_LINENO,
48         GREP_COLOR_COLUMNNO,
49         GREP_COLOR_MATCH_CONTEXT,
50         GREP_COLOR_MATCH_SELECTED,
51         GREP_COLOR_SELECTED,
52         GREP_COLOR_SEP,
53         NR_GREP_COLORS
54 };
55
56 struct grep_pat {
57         struct grep_pat *next;
58         const char *origin;
59         int no;
60         enum grep_pat_token token;
61         char *pattern;
62         size_t patternlen;
63         enum grep_header_field field;
64         regex_t regexp;
65         pcre2_code *pcre2_pattern;
66         pcre2_match_data *pcre2_match_data;
67         pcre2_compile_context *pcre2_compile_context;
68         const uint8_t *pcre2_tables;
69         uint32_t pcre2_jit_on;
70         unsigned fixed:1;
71         unsigned is_fixed:1;
72         unsigned ignore_case:1;
73         unsigned word_regexp:1;
74 };
75
76 enum grep_expr_node {
77         GREP_NODE_ATOM,
78         GREP_NODE_NOT,
79         GREP_NODE_AND,
80         GREP_NODE_TRUE,
81         GREP_NODE_OR
82 };
83
84 enum grep_pattern_type {
85         GREP_PATTERN_TYPE_UNSPECIFIED = 0,
86         GREP_PATTERN_TYPE_BRE,
87         GREP_PATTERN_TYPE_ERE,
88         GREP_PATTERN_TYPE_FIXED,
89         GREP_PATTERN_TYPE_PCRE
90 };
91
92 struct grep_expr {
93         enum grep_expr_node node;
94         unsigned hit;
95         union {
96                 struct grep_pat *atom;
97                 struct grep_expr *unary;
98                 struct {
99                         struct grep_expr *left;
100                         struct grep_expr *right;
101                 } binary;
102         } u;
103 };
104
105 struct grep_opt {
106         struct grep_pat *pattern_list;
107         struct grep_pat **pattern_tail;
108         struct grep_pat *header_list;
109         struct grep_pat **header_tail;
110         struct grep_expr *pattern_expression;
111         struct repository *repo;
112         const char *prefix;
113         int prefix_length;
114         regex_t regexp;
115         int linenum;
116         int columnnum;
117         int invert;
118         int ignore_case;
119         int status_only;
120         int name_only;
121         int unmatch_name_only;
122         int count;
123         int word_regexp;
124         int fixed;
125         int all_match;
126         int debug;
127 #define GREP_BINARY_DEFAULT     0
128 #define GREP_BINARY_NOMATCH     1
129 #define GREP_BINARY_TEXT        2
130         int binary;
131         int allow_textconv;
132         int extended;
133         int use_reflog_filter;
134         int pcre2;
135         int relative;
136         int pathname;
137         int null_following_name;
138         int only_matching;
139         int color;
140         int max_depth;
141         int funcname;
142         int funcbody;
143         int extended_regexp_option;
144         int pattern_type_option;
145         int ignore_locale;
146         char colors[NR_GREP_COLORS][COLOR_MAXLEN];
147         unsigned pre_context;
148         unsigned post_context;
149         unsigned last_shown;
150         int show_hunk_mark;
151         int file_break;
152         int heading;
153         void *priv;
154
155         void (*output)(struct grep_opt *opt, const void *data, size_t size);
156         void *output_priv;
157 };
158
159 int grep_config(const char *var, const char *value, void *);
160 void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
161 void grep_destroy(void);
162 void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
163
164 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
165 void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
166 void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
167 void compile_grep_patterns(struct grep_opt *opt);
168 void free_grep_patterns(struct grep_opt *opt);
169 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
170
171 struct grep_source {
172         char *name;
173
174         enum grep_source_type {
175                 GREP_SOURCE_OID,
176                 GREP_SOURCE_FILE,
177                 GREP_SOURCE_BUF,
178         } type;
179         void *identifier;
180
181         char *buf;
182         unsigned long size;
183
184         char *path; /* for attribute lookups */
185         struct userdiff_driver *driver;
186 };
187
188 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
189                       const char *name, const char *path,
190                       const void *identifier);
191 void grep_source_clear_data(struct grep_source *gs);
192 void grep_source_clear(struct grep_source *gs);
193 void grep_source_load_driver(struct grep_source *gs,
194                              struct index_state *istate);
195
196
197 int grep_source(struct grep_opt *opt, struct grep_source *gs);
198
199 struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
200 int grep_threads_ok(const struct grep_opt *opt);
201
202 /*
203  * Mutex used around access to the attributes machinery if
204  * opt->use_threads.  Must be initialized/destroyed by callers!
205  */
206 extern int grep_use_locks;
207 extern pthread_mutex_t grep_attr_mutex;
208
209 #endif