grep.h 4.1 KB
Newer Older
1 2
#ifndef GREP_H
#define GREP_H
R
René Scharfe 已提交
3
#include "color.h"
M
Michał Kiedrowicz 已提交
4 5 6 7 8 9
#ifdef USE_LIBPCRE
#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 31
enum grep_header_field {
	GREP_HEADER_AUTHOR = 0,
32
	GREP_HEADER_COMMITTER
33
};
34
#define GREP_HEADER_FIELD_MAX (GREP_HEADER_COMMITTER + 1)
35

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

enum grep_expr_node {
	GREP_NODE_ATOM,
	GREP_NODE_NOT,
	GREP_NODE_AND,
57
	GREP_NODE_TRUE,
58
	GREP_NODE_OR
59 60 61 62
};

struct grep_expr {
	enum grep_expr_node node;
J
Junio C Hamano 已提交
63
	unsigned hit;
64 65 66 67 68 69 70 71 72 73 74 75 76
	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;
77 78
	struct grep_pat *header_list;
	struct grep_pat **header_tail;
79
	struct grep_expr *pattern_expression;
80
	const char *prefix;
81 82
	int prefix_length;
	regex_t regexp;
R
René Scharfe 已提交
83 84
	int linenum;
	int invert;
85
	int ignore_case;
R
René Scharfe 已提交
86 87 88 89 90 91 92
	int status_only;
	int name_only;
	int unmatch_name_only;
	int count;
	int word_regexp;
	int fixed;
	int all_match;
93 94 95
#define GREP_BINARY_DEFAULT	0
#define GREP_BINARY_NOMATCH	1
#define GREP_BINARY_TEXT	2
R
René Scharfe 已提交
96 97
	int binary;
	int extended;
M
Michał Kiedrowicz 已提交
98
	int pcre;
R
René Scharfe 已提交
99 100 101
	int relative;
	int pathname;
	int null_following_name;
R
René Scharfe 已提交
102
	int color;
103
	int max_depth;
104
	int funcname;
105
	int funcbody;
106
	char color_context[COLOR_MAXLEN];
107
	char color_filename[COLOR_MAXLEN];
108
	char color_function[COLOR_MAXLEN];
109
	char color_lineno[COLOR_MAXLEN];
R
René Scharfe 已提交
110
	char color_match[COLOR_MAXLEN];
111
	char color_selected[COLOR_MAXLEN];
112
	char color_sep[COLOR_MAXLEN];
113 114 115
	int regflags;
	unsigned pre_context;
	unsigned post_context;
116
	unsigned last_shown;
117
	int show_hunk_mark;
R
René Scharfe 已提交
118
	int file_break;
R
René Scharfe 已提交
119
	int heading;
120
	void *priv;
F
Fredrik Kuivinen 已提交
121 122 123

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

126
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);
127
extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
128
extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
129
extern void compile_grep_patterns(struct grep_opt *opt);
130
extern void free_grep_patterns(struct grep_opt *opt);
131
extern int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
132

133 134 135 136 137 138 139 140 141 142 143 144
struct grep_source {
	char *name;

	enum grep_source_type {
		GREP_SOURCE_SHA1,
		GREP_SOURCE_FILE,
		GREP_SOURCE_BUF,
	} type;
	void *identifier;

	char *buf;
	unsigned long size;
145 146

	struct userdiff_driver *driver;
147 148 149 150 151 152 153
};

void grep_source_init(struct grep_source *gs, enum grep_source_type type,
		      const char *name, const void *identifier);
int grep_source_load(struct grep_source *gs);
void grep_source_clear_data(struct grep_source *gs);
void grep_source_clear(struct grep_source *gs);
154
void grep_source_load_driver(struct grep_source *gs);
155
int grep_source_is_binary(struct grep_source *gs);
156 157 158

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

F
Fredrik Kuivinen 已提交
159 160 161
extern struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
extern int grep_threads_ok(const struct grep_opt *opt);

162 163 164 165 166
#ifndef NO_PTHREADS
/*
 * Mutex used around access to the attributes machinery if
 * opt->use_threads.  Must be initialized/destroyed by callers!
 */
J
Jeff King 已提交
167
extern int grep_use_locks;
168
extern pthread_mutex_t grep_attr_mutex;
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
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()
186 187
#endif

188
#endif