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#endif
11#endif
12#endif
13#ifndef PCRE_STUDY_JIT_COMPILE
14#define PCRE_STUDY_JIT_COMPILE 0
15#endif
16#if PCRE_MAJOR <= 8 && PCRE_MINOR < 20
17typedef int pcre_jit_stack;
18#endif
19#else
20typedef int pcre;
21typedef int pcre_extra;
22typedef int pcre_jit_stack;
23#endif
24#ifdef USE_LIBPCRE2
25#define PCRE2_CODE_UNIT_WIDTH 8
26#include <pcre2.h>
27#else
28typedef int pcre2_code;
29typedef int pcre2_match_data;
30typedef int pcre2_compile_context;
31typedef int pcre2_match_context;
32typedef int pcre2_jit_stack;
33#endif
34#include "kwset.h"
35#include "thread-utils.h"
36#include "userdiff.h"
37
38enum grep_pat_token {
39 GREP_PATTERN,
40 GREP_PATTERN_HEAD,
41 GREP_PATTERN_BODY,
42 GREP_AND,
43 GREP_OPEN_PAREN,
44 GREP_CLOSE_PAREN,
45 GREP_NOT,
46 GREP_OR
47};
48
49enum grep_context {
50 GREP_CONTEXT_HEAD,
51 GREP_CONTEXT_BODY
52};
53
54enum grep_header_field {
55 GREP_HEADER_FIELD_MIN = 0,
56 GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN,
57 GREP_HEADER_COMMITTER,
58 GREP_HEADER_REFLOG,
59
60 /* Must be at the end of the enum */
61 GREP_HEADER_FIELD_MAX
62};
63
64struct grep_pat {
65 struct grep_pat *next;
66 const char *origin;
67 int no;
68 enum grep_pat_token token;
69 char *pattern;
70 size_t patternlen;
71 enum grep_header_field field;
72 regex_t regexp;
73 pcre *pcre1_regexp;
74 pcre_extra *pcre1_extra_info;
75 pcre_jit_stack *pcre1_jit_stack;
76 const unsigned char *pcre1_tables;
77 int pcre1_jit_on;
78 pcre2_code *pcre2_pattern;
79 pcre2_match_data *pcre2_match_data;
80 pcre2_compile_context *pcre2_compile_context;
81 pcre2_match_context *pcre2_match_context;
82 pcre2_jit_stack *pcre2_jit_stack;
83 uint32_t pcre2_jit_on;
84 kwset_t kws;
85 unsigned fixed:1;
86 unsigned ignore_case:1;
87 unsigned word_regexp:1;
88};
89
90enum grep_expr_node {
91 GREP_NODE_ATOM,
92 GREP_NODE_NOT,
93 GREP_NODE_AND,
94 GREP_NODE_TRUE,
95 GREP_NODE_OR
96};
97
98enum grep_pattern_type {
99 GREP_PATTERN_TYPE_UNSPECIFIED = 0,
100 GREP_PATTERN_TYPE_BRE,
101 GREP_PATTERN_TYPE_ERE,
102 GREP_PATTERN_TYPE_FIXED,
103 GREP_PATTERN_TYPE_PCRE
104};
105
106struct grep_expr {
107 enum grep_expr_node node;
108 unsigned hit;
109 union {
110 struct grep_pat *atom;
111 struct grep_expr *unary;
112 struct {
113 struct grep_expr *left;
114 struct grep_expr *right;
115 } binary;
116 } u;
117};
118
119struct grep_opt {
120 struct grep_pat *pattern_list;
121 struct grep_pat **pattern_tail;
122 struct grep_pat *header_list;
123 struct grep_pat **header_tail;
124 struct grep_expr *pattern_expression;
125 const char *prefix;
126 int prefix_length;
127 regex_t regexp;
128 int linenum;
129 int invert;
130 int ignore_case;
131 int status_only;
132 int name_only;
133 int unmatch_name_only;
134 int count;
135 int word_regexp;
136 int fixed;
137 int all_match;
138 int debug;
139#define GREP_BINARY_DEFAULT 0
140#define GREP_BINARY_NOMATCH 1
141#define GREP_BINARY_TEXT 2
142 int binary;
143 int allow_textconv;
144 int extended;
145 int use_reflog_filter;
146 int pcre1;
147 int pcre2;
148 int relative;
149 int pathname;
150 int null_following_name;
151 int color;
152 int max_depth;
153 int funcname;
154 int funcbody;
155 int extended_regexp_option;
156 int pattern_type_option;
157 char color_context[COLOR_MAXLEN];
158 char color_filename[COLOR_MAXLEN];
159 char color_function[COLOR_MAXLEN];
160 char color_lineno[COLOR_MAXLEN];
161 char color_match_context[COLOR_MAXLEN];
162 char color_match_selected[COLOR_MAXLEN];
163 char color_selected[COLOR_MAXLEN];
164 char color_sep[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
177extern void init_grep_defaults(void);
178extern int grep_config(const char *var, const char *value, void *);
179extern void grep_init(struct grep_opt *, const char *prefix);
180void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
181
182extern void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
183extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
184extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
185extern void compile_grep_patterns(struct grep_opt *opt);
186extern void free_grep_patterns(struct grep_opt *opt);
187extern int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
188
189struct grep_source {
190 char *name;
191
192 enum grep_source_type {
193 GREP_SOURCE_OID,
194 GREP_SOURCE_FILE,
195 GREP_SOURCE_BUF,
196 GREP_SOURCE_SUBMODULE,
197 } type;
198 void *identifier;
199
200 char *buf;
201 unsigned long size;
202
203 char *path; /* for attribute lookups */
204 struct userdiff_driver *driver;
205};
206
207void grep_source_init(struct grep_source *gs, enum grep_source_type type,
208 const char *name, const char *path,
209 const void *identifier);
210void grep_source_clear_data(struct grep_source *gs);
211void grep_source_clear(struct grep_source *gs);
212void grep_source_load_driver(struct grep_source *gs);
213
214
215int grep_source(struct grep_opt *opt, struct grep_source *gs);
216
217extern struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
218extern int grep_threads_ok(const struct grep_opt *opt);
219
220#ifndef NO_PTHREADS
221/*
222 * Mutex used around access to the attributes machinery if
223 * opt->use_threads. Must be initialized/destroyed by callers!
224 */
225extern int grep_use_locks;
226extern pthread_mutex_t grep_attr_mutex;
227extern pthread_mutex_t grep_read_mutex;
228
229static inline void grep_read_lock(void)
230{
231 if (grep_use_locks)
232 pthread_mutex_lock(&grep_read_mutex);
233}
234
235static inline void grep_read_unlock(void)
236{
237 if (grep_use_locks)
238 pthread_mutex_unlock(&grep_read_mutex);
239}
240
241#else
242#define grep_read_lock()
243#define grep_read_unlock()
244#endif
245
246#endif