grep.h 5.1 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 8 9 10
#ifdef PCRE_CONFIG_JIT
#if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
#define GIT_PCRE1_USE_JIT
#endif
#endif
11 12 13
#ifndef PCRE_STUDY_JIT_COMPILE
#define PCRE_STUDY_JIT_COMPILE 0
#endif
14 15 16
#if PCRE_MAJOR <= 8 && PCRE_MINOR < 20
typedef int pcre_jit_stack;
#endif
M
Michał Kiedrowicz 已提交
17 18 19
#else
typedef int pcre;
typedef int pcre_extra;
20
typedef int pcre_jit_stack;
M
Michał Kiedrowicz 已提交
21
#endif
F
Fredrik Kuivinen 已提交
22
#include "kwset.h"
23
#include "thread-utils.h"
24
#include "userdiff.h"
25 26 27

enum grep_pat_token {
	GREP_PATTERN,
28 29
	GREP_PATTERN_HEAD,
	GREP_PATTERN_BODY,
30 31 32 33
	GREP_AND,
	GREP_OPEN_PAREN,
	GREP_CLOSE_PAREN,
	GREP_NOT,
34
	GREP_OR
35 36
};

37 38
enum grep_context {
	GREP_CONTEXT_HEAD,
39
	GREP_CONTEXT_BODY
40 41
};

42
enum grep_header_field {
43 44
	GREP_HEADER_FIELD_MIN = 0,
	GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN,
45
	GREP_HEADER_COMMITTER,
46
	GREP_HEADER_REFLOG,
47 48 49

	/* Must be at the end of the enum */
	GREP_HEADER_FIELD_MAX
50 51
};

52 53 54 55 56
struct grep_pat {
	struct grep_pat *next;
	const char *origin;
	int no;
	enum grep_pat_token token;
57
	char *pattern;
58
	size_t patternlen;
59
	enum grep_header_field field;
60
	regex_t regexp;
61 62
	pcre *pcre1_regexp;
	pcre_extra *pcre1_extra_info;
63
	pcre_jit_stack *pcre1_jit_stack;
64
	const unsigned char *pcre1_tables;
65
	int pcre1_jit_on;
F
Fredrik Kuivinen 已提交
66
	kwset_t kws;
67
	unsigned fixed:1;
68
	unsigned ignore_case:1;
69
	unsigned word_regexp:1;
70 71 72 73 74 75
};

enum grep_expr_node {
	GREP_NODE_ATOM,
	GREP_NODE_NOT,
	GREP_NODE_AND,
76
	GREP_NODE_TRUE,
77
	GREP_NODE_OR
78 79
};

80 81 82 83 84 85 86 87
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
};

88 89
struct grep_expr {
	enum grep_expr_node node;
J
Junio C Hamano 已提交
90
	unsigned hit;
91 92 93 94 95 96 97 98 99 100 101 102 103
	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;
104 105
	struct grep_pat *header_list;
	struct grep_pat **header_tail;
106
	struct grep_expr *pattern_expression;
107
	const char *prefix;
108 109
	int prefix_length;
	regex_t regexp;
R
René Scharfe 已提交
110 111
	int linenum;
	int invert;
112
	int ignore_case;
R
René Scharfe 已提交
113 114 115 116 117 118 119
	int status_only;
	int name_only;
	int unmatch_name_only;
	int count;
	int word_regexp;
	int fixed;
	int all_match;
120
	int debug;
121 122 123
#define GREP_BINARY_DEFAULT	0
#define GREP_BINARY_NOMATCH	1
#define GREP_BINARY_TEXT	2
R
René Scharfe 已提交
124
	int binary;
J
Jeff King 已提交
125
	int allow_textconv;
R
René Scharfe 已提交
126
	int extended;
127
	int use_reflog_filter;
128
	int pcre1;
R
René Scharfe 已提交
129 130 131
	int relative;
	int pathname;
	int null_following_name;
R
René Scharfe 已提交
132
	int color;
133
	int max_depth;
134
	int funcname;
135
	int funcbody;
136 137
	int extended_regexp_option;
	int pattern_type_option;
138
	char color_context[COLOR_MAXLEN];
139
	char color_filename[COLOR_MAXLEN];
140
	char color_function[COLOR_MAXLEN];
141
	char color_lineno[COLOR_MAXLEN];
142 143
	char color_match_context[COLOR_MAXLEN];
	char color_match_selected[COLOR_MAXLEN];
144
	char color_selected[COLOR_MAXLEN];
145
	char color_sep[COLOR_MAXLEN];
146 147 148
	int regflags;
	unsigned pre_context;
	unsigned post_context;
149
	unsigned last_shown;
150
	int show_hunk_mark;
R
René Scharfe 已提交
151
	int file_break;
R
René Scharfe 已提交
152
	int heading;
153
	void *priv;
F
Fredrik Kuivinen 已提交
154 155 156

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

159 160 161
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);
162
void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
163

164
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);
165
extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
166
extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
167
extern void compile_grep_patterns(struct grep_opt *opt);
168
extern void free_grep_patterns(struct grep_opt *opt);
169
extern int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
170

171 172 173 174 175 176 177
struct grep_source {
	char *name;

	enum grep_source_type {
		GREP_SOURCE_SHA1,
		GREP_SOURCE_FILE,
		GREP_SOURCE_BUF,
178
		GREP_SOURCE_SUBMODULE,
179 180 181 182 183
	} type;
	void *identifier;

	char *buf;
	unsigned long size;
184

185
	char *path; /* for attribute lookups */
186
	struct userdiff_driver *driver;
187 188 189
};

void grep_source_init(struct grep_source *gs, enum grep_source_type type,
190 191
		      const char *name, const char *path,
		      const void *identifier);
192 193
void grep_source_clear_data(struct grep_source *gs);
void grep_source_clear(struct grep_source *gs);
194
void grep_source_load_driver(struct grep_source *gs);
195

196 197 198

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

F
Fredrik Kuivinen 已提交
199 200 201
extern struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
extern int grep_threads_ok(const struct grep_opt *opt);

202 203 204 205 206
#ifndef NO_PTHREADS
/*
 * Mutex used around access to the attributes machinery if
 * opt->use_threads.  Must be initialized/destroyed by callers!
 */
J
Jeff King 已提交
207
extern int grep_use_locks;
208
extern pthread_mutex_t grep_attr_mutex;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
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()
226 227
#endif

228
#endif