grep.h 5.65 KB
Newer Older
1 2
#ifndef GREP_H
#define GREP_H
3
#include "color.h"
4
#ifdef USE_LIBPCRE1
5
#include <pcre.h>
6 7
#ifdef PCRE_CONFIG_JIT
#if PCRE_MAJOR >= 8 && PCRE_MINOR >= 32
8
#ifndef NO_LIBPCRE1_JIT
9
#define GIT_PCRE1_USE_JIT
10
#define GIT_PCRE_STUDY_JIT_COMPILE PCRE_STUDY_JIT_COMPILE
11 12
#endif
#endif
13
#endif
14 15
#ifndef GIT_PCRE_STUDY_JIT_COMPILE
#define GIT_PCRE_STUDY_JIT_COMPILE 0
16
#endif
17 18 19
#if PCRE_MAJOR <= 8 && PCRE_MINOR < 20
typedef int pcre_jit_stack;
#endif
20 21 22
#else
typedef int pcre;
typedef int pcre_extra;
23
typedef int pcre_jit_stack;
24
#endif
25 26 27 28 29 30 31 32 33 34
#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
Fredrik K's avatar
Fredrik K committed
35
#include "kwset.h"
36
#include "thread-utils.h"
37
#include "userdiff.h"
38

39 40
struct repository;

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

52 53
enum grep_context {
	GREP_CONTEXT_HEAD,
54
	GREP_CONTEXT_BODY
55 56
};

57
enum grep_header_field {
58 59
	GREP_HEADER_FIELD_MIN = 0,
	GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN,
60
	GREP_HEADER_COMMITTER,
61
	GREP_HEADER_REFLOG,
62 63 64

	/* Must be at the end of the enum */
	GREP_HEADER_FIELD_MAX
65 66
};

67 68 69 70 71
enum grep_color {
	GREP_COLOR_CONTEXT,
	GREP_COLOR_FILENAME,
	GREP_COLOR_FUNCTION,
	GREP_COLOR_LINENO,
72
	GREP_COLOR_COLUMNNO,
73 74 75 76 77 78 79
	GREP_COLOR_MATCH_CONTEXT,
	GREP_COLOR_MATCH_SELECTED,
	GREP_COLOR_SELECTED,
	GREP_COLOR_SEP,
	NR_GREP_COLORS
};

80 81 82 83 84
struct grep_pat {
	struct grep_pat *next;
	const char *origin;
	int no;
	enum grep_pat_token token;
85
	char *pattern;
86
	size_t patternlen;
87
	enum grep_header_field field;
88
	regex_t regexp;
89 90
	pcre *pcre1_regexp;
	pcre_extra *pcre1_extra_info;
91
	pcre_jit_stack *pcre1_jit_stack;
92
	const unsigned char *pcre1_tables;
93
	int pcre1_jit_on;
94 95 96 97 98 99
	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;
Fredrik K's avatar
Fredrik K committed
100
	kwset_t kws;
101
	unsigned fixed:1;
102
	unsigned ignore_case:1;
103
	unsigned word_regexp:1;
104 105 106 107 108 109
};

enum grep_expr_node {
	GREP_NODE_ATOM,
	GREP_NODE_NOT,
	GREP_NODE_AND,
110
	GREP_NODE_TRUE,
111
	GREP_NODE_OR
112 113
};

114 115 116 117 118 119 120 121
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
};

122 123
struct grep_expr {
	enum grep_expr_node node;
Junio C Hamano's avatar
Junio C Hamano committed
124
	unsigned hit;
125 126 127 128 129 130 131 132 133 134 135 136 137
	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;
138 139
	struct grep_pat *header_list;
	struct grep_pat **header_tail;
140
	struct grep_expr *pattern_expression;
141
	struct repository *repo;
142
	const char *prefix;
143 144
	int prefix_length;
	regex_t regexp;
René Scharfe's avatar
René Scharfe committed
145
	int linenum;
146
	int columnnum;
René Scharfe's avatar
René Scharfe committed
147
	int invert;
148
	int ignore_case;
René Scharfe's avatar
René Scharfe committed
149 150 151 152 153 154 155
	int status_only;
	int name_only;
	int unmatch_name_only;
	int count;
	int word_regexp;
	int fixed;
	int all_match;
156
	int debug;
157 158 159
#define GREP_BINARY_DEFAULT	0
#define GREP_BINARY_NOMATCH	1
#define GREP_BINARY_TEXT	2
René Scharfe's avatar
René Scharfe committed
160
	int binary;
161
	int allow_textconv;
René Scharfe's avatar
René Scharfe committed
162
	int extended;
163
	int use_reflog_filter;
164
	int pcre1;
165
	int pcre2;
René Scharfe's avatar
René Scharfe committed
166 167 168
	int relative;
	int pathname;
	int null_following_name;
169
	int only_matching;
170
	int color;
171
	int max_depth;
172
	int funcname;
173
	int funcbody;
174 175
	int extended_regexp_option;
	int pattern_type_option;
176
	char colors[NR_GREP_COLORS][COLOR_MAXLEN];
177 178
	unsigned pre_context;
	unsigned post_context;
179
	unsigned last_shown;
180
	int show_hunk_mark;
René Scharfe's avatar
René Scharfe committed
181
	int file_break;
René Scharfe's avatar
René Scharfe committed
182
	int heading;
183
	void *priv;
Fredrik K's avatar
Fredrik K committed
184 185 186

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

189
extern void init_grep_defaults(struct repository *);
190
extern int grep_config(const char *var, const char *value, void *);
191
extern void grep_init(struct grep_opt *, struct repository *repo, const char *prefix);
192
void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
193

194
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);
195
extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
196
extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
197
extern void compile_grep_patterns(struct grep_opt *opt);
198
extern void free_grep_patterns(struct grep_opt *opt);
199
extern int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
200

201 202 203 204
struct grep_source {
	char *name;

	enum grep_source_type {
205
		GREP_SOURCE_OID,
206 207 208 209 210 211 212
		GREP_SOURCE_FILE,
		GREP_SOURCE_BUF,
	} type;
	void *identifier;

	char *buf;
	unsigned long size;
213

214
	char *path; /* for attribute lookups */
215
	struct userdiff_driver *driver;
216 217 218
};

void grep_source_init(struct grep_source *gs, enum grep_source_type type,
219 220
		      const char *name, const char *path,
		      const void *identifier);
221 222
void grep_source_clear_data(struct grep_source *gs);
void grep_source_clear(struct grep_source *gs);
223 224
void grep_source_load_driver(struct grep_source *gs,
			     struct index_state *istate);
225

226 227 228

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

Fredrik K's avatar
Fredrik K committed
229 230 231
extern struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
extern int grep_threads_ok(const struct grep_opt *opt);

232 233 234 235
/*
 * Mutex used around access to the attributes machinery if
 * opt->use_threads.  Must be initialized/destroyed by callers!
 */
236
extern int grep_use_locks;
237
extern pthread_mutex_t grep_attr_mutex;
238 239 240 241 242 243 244 245 246 247 248 249 250 251
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);
}

252
#endif