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