grep.h 5.6 KB
Newer Older
1 2
#ifndef GREP_H
#define GREP_H
R
René Scharfe 已提交
3
#include "color.h"
4
#ifdef USE_LIBPCRE1
M
Michał Kiedrowicz 已提交
5
#include <pcre.h>
6 7
#ifdef PCRE_CONFIG_JIT
#if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
8
#ifndef NO_LIBPCRE1_JIT
9 10 11
#define GIT_PCRE1_USE_JIT
#endif
#endif
12
#endif
13 14 15
#ifndef PCRE_STUDY_JIT_COMPILE
#define PCRE_STUDY_JIT_COMPILE 0
#endif
16 17 18
#if PCRE_MAJOR <= 8 && PCRE_MINOR < 20
typedef int pcre_jit_stack;
#endif
M
Michał Kiedrowicz 已提交
19 20 21
#else
typedef int pcre;
typedef int pcre_extra;
22
typedef int pcre_jit_stack;
M
Michał Kiedrowicz 已提交
23
#endif
24 25 26 27 28 29 30 31 32 33
#ifdef USE_LIBPCRE2
#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>
#else
typedef int pcre2_code;
typedef int pcre2_match_data;
typedef int pcre2_compile_context;
typedef int pcre2_match_context;
typedef int pcre2_jit_stack;
#endif
F
Fredrik Kuivinen 已提交
34
#include "kwset.h"
35
#include "thread-utils.h"
36
#include "userdiff.h"
37 38 39

enum grep_pat_token {
	GREP_PATTERN,
40 41
	GREP_PATTERN_HEAD,
	GREP_PATTERN_BODY,
42 43 44 45
	GREP_AND,
	GREP_OPEN_PAREN,
	GREP_CLOSE_PAREN,
	GREP_NOT,
46
	GREP_OR
47 48
};

49 50
enum grep_context {
	GREP_CONTEXT_HEAD,
51
	GREP_CONTEXT_BODY
52 53
};

54
enum grep_header_field {
55 56
	GREP_HEADER_FIELD_MIN = 0,
	GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN,
57
	GREP_HEADER_COMMITTER,
58
	GREP_HEADER_REFLOG,
59 60 61

	/* Must be at the end of the enum */
	GREP_HEADER_FIELD_MAX
62 63
};

64 65 66 67 68
struct grep_pat {
	struct grep_pat *next;
	const char *origin;
	int no;
	enum grep_pat_token token;
69
	char *pattern;
70
	size_t patternlen;
71
	enum grep_header_field field;
72
	regex_t regexp;
73 74
	pcre *pcre1_regexp;
	pcre_extra *pcre1_extra_info;
75
	pcre_jit_stack *pcre1_jit_stack;
76
	const unsigned char *pcre1_tables;
77
	int pcre1_jit_on;
78 79 80 81 82 83
	pcre2_code *pcre2_pattern;
	pcre2_match_data *pcre2_match_data;
	pcre2_compile_context *pcre2_compile_context;
	pcre2_match_context *pcre2_match_context;
	pcre2_jit_stack *pcre2_jit_stack;
	uint32_t pcre2_jit_on;
F
Fredrik Kuivinen 已提交
84
	kwset_t kws;
85
	unsigned fixed:1;
86
	unsigned ignore_case:1;
87
	unsigned word_regexp:1;
88 89 90 91 92 93
};

enum grep_expr_node {
	GREP_NODE_ATOM,
	GREP_NODE_NOT,
	GREP_NODE_AND,
94
	GREP_NODE_TRUE,
95
	GREP_NODE_OR
96 97
};

98 99 100 101 102 103 104 105
enum grep_pattern_type {
	GREP_PATTERN_TYPE_UNSPECIFIED = 0,
	GREP_PATTERN_TYPE_BRE,
	GREP_PATTERN_TYPE_ERE,
	GREP_PATTERN_TYPE_FIXED,
	GREP_PATTERN_TYPE_PCRE
};

106 107
struct grep_expr {
	enum grep_expr_node node;
J
Junio C Hamano 已提交
108
	unsigned hit;
109 110 111 112 113 114 115 116 117 118 119 120 121
	union {
		struct grep_pat *atom;
		struct grep_expr *unary;
		struct {
			struct grep_expr *left;
			struct grep_expr *right;
		} binary;
	} u;
};

struct grep_opt {
	struct grep_pat *pattern_list;
	struct grep_pat **pattern_tail;
122 123
	struct grep_pat *header_list;
	struct grep_pat **header_tail;
124
	struct grep_expr *pattern_expression;
125
	const char *prefix;
126 127
	int prefix_length;
	regex_t regexp;
R
René Scharfe 已提交
128 129
	int linenum;
	int invert;
130
	int ignore_case;
R
René Scharfe 已提交
131 132 133 134 135 136 137
	int status_only;
	int name_only;
	int unmatch_name_only;
	int count;
	int word_regexp;
	int fixed;
	int all_match;
138
	int debug;
139 140 141
#define GREP_BINARY_DEFAULT	0
#define GREP_BINARY_NOMATCH	1
#define GREP_BINARY_TEXT	2
R
René Scharfe 已提交
142
	int binary;
J
Jeff King 已提交
143
	int allow_textconv;
R
René Scharfe 已提交
144
	int extended;
145
	int use_reflog_filter;
146
	int pcre1;
147
	int pcre2;
R
René Scharfe 已提交
148 149 150
	int relative;
	int pathname;
	int null_following_name;
R
René Scharfe 已提交
151
	int color;
152
	int max_depth;
153
	int funcname;
154
	int funcbody;
155 156
	int extended_regexp_option;
	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 162
	char color_match_context[COLOR_MAXLEN];
	char color_match_selected[COLOR_MAXLEN];
163
	char color_selected[COLOR_MAXLEN];
164
	char color_sep[COLOR_MAXLEN];
165 166 167
	int regflags;
	unsigned pre_context;
	unsigned post_context;
168
	unsigned last_shown;
169
	int show_hunk_mark;
R
René Scharfe 已提交
170
	int file_break;
R
René Scharfe 已提交
171
	int heading;
172
	void *priv;
F
Fredrik Kuivinen 已提交
173 174 175

	void (*output)(struct grep_opt *opt, const void *data, size_t size);
	void *output_priv;
176 177
};

178 179 180
extern void init_grep_defaults(void);
extern int grep_config(const char *var, const char *value, void *);
extern void grep_init(struct grep_opt *, const char *prefix);
181
void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
182

183
extern void append_grep_pat(struct grep_opt *opt, const char *pat, size_t patlen, const char *origin, int no, enum grep_pat_token t);
184
extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
185
extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
186
extern void compile_grep_patterns(struct grep_opt *opt);
187
extern void free_grep_patterns(struct grep_opt *opt);
188
extern int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
189

190 191 192 193 194 195 196
struct grep_source {
	char *name;

	enum grep_source_type {
		GREP_SOURCE_SHA1,
		GREP_SOURCE_FILE,
		GREP_SOURCE_BUF,
197
		GREP_SOURCE_SUBMODULE,
198 199 200 201 202
	} type;
	void *identifier;

	char *buf;
	unsigned long size;
203

204
	char *path; /* for attribute lookups */
205
	struct userdiff_driver *driver;
206 207 208
};

void grep_source_init(struct grep_source *gs, enum grep_source_type type,
209 210
		      const char *name, const char *path,
		      const void *identifier);
211 212
void grep_source_clear_data(struct grep_source *gs);
void grep_source_clear(struct grep_source *gs);
213
void grep_source_load_driver(struct grep_source *gs);
214

215 216 217

int grep_source(struct grep_opt *opt, struct grep_source *gs);

F
Fredrik Kuivinen 已提交
218 219 220
extern struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
extern int grep_threads_ok(const struct grep_opt *opt);

221 222 223 224 225
#ifndef NO_PTHREADS
/*
 * Mutex used around access to the attributes machinery if
 * opt->use_threads.  Must be initialized/destroyed by callers!
 */
J
Jeff King 已提交
226
extern int grep_use_locks;
227
extern pthread_mutex_t grep_attr_mutex;
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
extern pthread_mutex_t grep_read_mutex;

static inline void grep_read_lock(void)
{
	if (grep_use_locks)
		pthread_mutex_lock(&grep_read_mutex);
}

static inline void grep_read_unlock(void)
{
	if (grep_use_locks)
		pthread_mutex_unlock(&grep_read_mutex);
}

#else
#define grep_read_lock()
#define grep_read_unlock()
245 246
#endif

247
#endif