Merge branch 'ab/retire-pcre1'
[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 #define GREP_BINARY_DEFAULT     0
127 #define GREP_BINARY_NOMATCH     1
128 #define GREP_BINARY_TEXT        2
129         int binary;
130         int allow_textconv;
131         int extended;
132         int use_reflog_filter;
133         int pcre2;
134         int relative;
135         int pathname;
136         int null_following_name;
137         int only_matching;
138         int color;
139         int max_depth;
140         int funcname;
141         int funcbody;
142         int extended_regexp_option;
143         int pattern_type_option;
144         int ignore_locale;
145         char colors[NR_GREP_COLORS][COLOR_MAXLEN];
146         unsigned pre_context;
147         unsigned post_context;
148         unsigned last_shown;
149         int show_hunk_mark;
150         int file_break;
151         int heading;
152         void *priv;
153
154         void (*output)(struct grep_opt *opt, const void *data, size_t size);
155         void *output_priv;
156 };
157
158 int grep_config(const char *var, const char *value, void *);
159 void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
160 void grep_destroy(void);
161 void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
162
163 void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
164 void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
165 void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
166 void compile_grep_patterns(struct grep_opt *opt);
167 void free_grep_patterns(struct grep_opt *opt);
168 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
169
170 struct grep_source {
171         char *name;
172
173         enum grep_source_type {
174                 GREP_SOURCE_OID,
175                 GREP_SOURCE_FILE,
176                 GREP_SOURCE_BUF,
177         } type;
178         void *identifier;
179
180         char *buf;
181         unsigned long size;
182
183         char *path; /* for attribute lookups */
184         struct userdiff_driver *driver;
185 };
186
187 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
188                       const char *name, const char *path,
189                       const void *identifier);
190 void grep_source_clear_data(struct grep_source *gs);
191 void grep_source_clear(struct grep_source *gs);
192 void grep_source_load_driver(struct grep_source *gs,
193                              struct index_state *istate);
194
195
196 int grep_source(struct grep_opt *opt, struct grep_source *gs);
197
198 struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
199 int grep_threads_ok(const struct grep_opt *opt);
200
201 /*
202  * Mutex used around access to the attributes machinery if
203  * opt->use_threads.  Must be initialized/destroyed by callers!
204  */
205 extern int grep_use_locks;
206 extern pthread_mutex_t grep_attr_mutex;
207
208 #endif