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