grep.h 4.8 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 6 7 8 9
#include <pcre.h>
#else
typedef int pcre;
typedef int pcre_extra;
#endif
F
Fredrik Kuivinen 已提交
10
#include "kwset.h"
11
#include "thread-utils.h"
12
#include "userdiff.h"
13 14 15

enum grep_pat_token {
	GREP_PATTERN,
16 17
	GREP_PATTERN_HEAD,
	GREP_PATTERN_BODY,
18 19 20 21
	GREP_AND,
	GREP_OPEN_PAREN,
	GREP_CLOSE_PAREN,
	GREP_NOT,
22
	GREP_OR
23 24
};

25 26
enum grep_context {
	GREP_CONTEXT_HEAD,
27
	GREP_CONTEXT_BODY
28 29
};

30
enum grep_header_field {
31 32
	GREP_HEADER_FIELD_MIN = 0,
	GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN,
33
	GREP_HEADER_COMMITTER,
34
	GREP_HEADER_REFLOG,
35 36 37

	/* Must be at the end of the enum */
	GREP_HEADER_FIELD_MAX
38 39
};

40 41 42 43 44
struct grep_pat {
	struct grep_pat *next;
	const char *origin;
	int no;
	enum grep_pat_token token;
45
	char *pattern;
46
	size_t patternlen;
47
	enum grep_header_field field;
48
	regex_t regexp;
M
Michał Kiedrowicz 已提交
49 50
	pcre *pcre_regexp;
	pcre_extra *pcre_extra_info;
51
	const unsigned char *pcre_tables;
F
Fredrik Kuivinen 已提交
52
	kwset_t kws;
53
	unsigned fixed:1;
54
	unsigned ignore_case:1;
55
	unsigned word_regexp:1;
56 57 58 59 60 61
};

enum grep_expr_node {
	GREP_NODE_ATOM,
	GREP_NODE_NOT,
	GREP_NODE_AND,
62
	GREP_NODE_TRUE,
63
	GREP_NODE_OR
64 65
};

66 67 68 69 70 71 72 73
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
};

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

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

145 146 147
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);
148
void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt);
149

150
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);
151
extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
152
extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
153
extern void compile_grep_patterns(struct grep_opt *opt);
154
extern void free_grep_patterns(struct grep_opt *opt);
155
extern int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
156

157 158 159 160 161 162 163
struct grep_source {
	char *name;

	enum grep_source_type {
		GREP_SOURCE_SHA1,
		GREP_SOURCE_FILE,
		GREP_SOURCE_BUF,
164
		GREP_SOURCE_SUBMODULE,
165 166 167 168 169
	} type;
	void *identifier;

	char *buf;
	unsigned long size;
170

171
	char *path; /* for attribute lookups */
172
	struct userdiff_driver *driver;
173 174 175
};

void grep_source_init(struct grep_source *gs, enum grep_source_type type,
176 177
		      const char *name, const char *path,
		      const void *identifier);
178 179
void grep_source_clear_data(struct grep_source *gs);
void grep_source_clear(struct grep_source *gs);
180
void grep_source_load_driver(struct grep_source *gs);
181

182 183 184

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

F
Fredrik Kuivinen 已提交
185 186 187
extern struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
extern int grep_threads_ok(const struct grep_opt *opt);

188 189 190 191 192
#ifndef NO_PTHREADS
/*
 * Mutex used around access to the attributes machinery if
 * opt->use_threads.  Must be initialized/destroyed by callers!
 */
J
Jeff King 已提交
193
extern int grep_use_locks;
194
extern pthread_mutex_t grep_attr_mutex;
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
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()
212 213
#endif

214
#endif