grep: prepare for new header field filter
[git.git] / grep.h
1 #ifndef GREP_H
2 #define GREP_H
3 #include "color.h"
4 #ifdef USE_LIBPCRE
5 #include <pcre.h>
6 #else
7 typedef int pcre;
8 typedef int pcre_extra;
9 #endif
10 #include "kwset.h"
11 #include "thread-utils.h"
12 #include "userdiff.h"
13
14 enum grep_pat_token {
15         GREP_PATTERN,
16         GREP_PATTERN_HEAD,
17         GREP_PATTERN_BODY,
18         GREP_AND,
19         GREP_OPEN_PAREN,
20         GREP_CLOSE_PAREN,
21         GREP_NOT,
22         GREP_OR
23 };
24
25 enum grep_context {
26         GREP_CONTEXT_HEAD,
27         GREP_CONTEXT_BODY
28 };
29
30 enum grep_header_field {
31         GREP_HEADER_AUTHOR = 0,
32         GREP_HEADER_COMMITTER,
33
34         /* Must be at the end of the enum */
35         GREP_HEADER_FIELD_MAX
36 };
37
38 struct grep_pat {
39         struct grep_pat *next;
40         const char *origin;
41         int no;
42         enum grep_pat_token token;
43         char *pattern;
44         size_t patternlen;
45         enum grep_header_field field;
46         regex_t regexp;
47         pcre *pcre_regexp;
48         pcre_extra *pcre_extra_info;
49         kwset_t kws;
50         unsigned fixed:1;
51         unsigned ignore_case:1;
52         unsigned word_regexp:1;
53 };
54
55 enum grep_expr_node {
56         GREP_NODE_ATOM,
57         GREP_NODE_NOT,
58         GREP_NODE_AND,
59         GREP_NODE_TRUE,
60         GREP_NODE_OR
61 };
62
63 enum grep_pattern_type {
64         GREP_PATTERN_TYPE_UNSPECIFIED = 0,
65         GREP_PATTERN_TYPE_BRE,
66         GREP_PATTERN_TYPE_ERE,
67         GREP_PATTERN_TYPE_FIXED,
68         GREP_PATTERN_TYPE_PCRE
69 };
70
71 struct grep_expr {
72         enum grep_expr_node node;
73         unsigned hit;
74         union {
75                 struct grep_pat *atom;
76                 struct grep_expr *unary;
77                 struct {
78                         struct grep_expr *left;
79                         struct grep_expr *right;
80                 } binary;
81         } u;
82 };
83
84 struct grep_opt {
85         struct grep_pat *pattern_list;
86         struct grep_pat **pattern_tail;
87         struct grep_pat *header_list;
88         struct grep_pat **header_tail;
89         struct grep_expr *pattern_expression;
90         const char *prefix;
91         int prefix_length;
92         regex_t regexp;
93         int linenum;
94         int invert;
95         int ignore_case;
96         int status_only;
97         int name_only;
98         int unmatch_name_only;
99         int count;
100         int word_regexp;
101         int fixed;
102         int all_match;
103         int debug;
104 #define GREP_BINARY_DEFAULT     0
105 #define GREP_BINARY_NOMATCH     1
106 #define GREP_BINARY_TEXT        2
107         int binary;
108         int extended;
109         int pcre;
110         int relative;
111         int pathname;
112         int null_following_name;
113         int color;
114         int max_depth;
115         int funcname;
116         int funcbody;
117         int extended_regexp_option;
118         int pattern_type_option;
119         char color_context[COLOR_MAXLEN];
120         char color_filename[COLOR_MAXLEN];
121         char color_function[COLOR_MAXLEN];
122         char color_lineno[COLOR_MAXLEN];
123         char color_match[COLOR_MAXLEN];
124         char color_selected[COLOR_MAXLEN];
125         char color_sep[COLOR_MAXLEN];
126         int regflags;
127         unsigned pre_context;
128         unsigned post_context;
129         unsigned last_shown;
130         int show_hunk_mark;
131         int file_break;
132         int heading;
133         void *priv;
134
135         void (*output)(struct grep_opt *opt, const void *data, size_t size);
136         void *output_priv;
137 };
138
139 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);
140 extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t);
141 extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *);
142 extern void compile_grep_patterns(struct grep_opt *opt);
143 extern void free_grep_patterns(struct grep_opt *opt);
144 extern int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
145
146 struct grep_source {
147         char *name;
148
149         enum grep_source_type {
150                 GREP_SOURCE_SHA1,
151                 GREP_SOURCE_FILE,
152                 GREP_SOURCE_BUF,
153         } type;
154         void *identifier;
155
156         char *buf;
157         unsigned long size;
158
159         struct userdiff_driver *driver;
160 };
161
162 void grep_source_init(struct grep_source *gs, enum grep_source_type type,
163                       const char *name, const void *identifier);
164 void grep_source_clear_data(struct grep_source *gs);
165 void grep_source_clear(struct grep_source *gs);
166 void grep_source_load_driver(struct grep_source *gs);
167
168
169 int grep_source(struct grep_opt *opt, struct grep_source *gs);
170
171 extern struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
172 extern int grep_threads_ok(const struct grep_opt *opt);
173
174 #ifndef NO_PTHREADS
175 /*
176  * Mutex used around access to the attributes machinery if
177  * opt->use_threads.  Must be initialized/destroyed by callers!
178  */
179 extern int grep_use_locks;
180 extern pthread_mutex_t grep_attr_mutex;
181 extern pthread_mutex_t grep_read_mutex;
182
183 static inline void grep_read_lock(void)
184 {
185         if (grep_use_locks)
186                 pthread_mutex_lock(&grep_read_mutex);
187 }
188
189 static inline void grep_read_unlock(void)
190 {
191         if (grep_use_locks)
192                 pthread_mutex_unlock(&grep_read_mutex);
193 }
194
195 #else
196 #define grep_read_lock()
197 #define grep_read_unlock()
198 #endif
199
200 #endif