worktree: move subcommand
[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_FIELD_MIN = 0,
32         GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN,
33         GREP_HEADER_COMMITTER,
34         GREP_HEADER_REFLOG,
35
36         /* Must be at the end of the enum */
37         GREP_HEADER_FIELD_MAX
38 };
39
40 struct grep_pat {
41         struct grep_pat *next;
42         const char *origin;
43         int no;
44         enum grep_pat_token token;
45         char *pattern;
46         size_t patternlen;
47         enum grep_header_field field;
48         regex_t regexp;
49         pcre *pcre_regexp;
50         pcre_extra *pcre_extra_info;
51         const unsigned char *pcre_tables;
52         kwset_t kws;
53         unsigned fixed:1;
54         unsigned ignore_case:1;
55         unsigned word_regexp:1;
56 };
57
58 enum grep_expr_node {
59         GREP_NODE_ATOM,
60         GREP_NODE_NOT,
61         GREP_NODE_AND,
62         GREP_NODE_TRUE,
63         GREP_NODE_OR
64 };
65
66 enum grep_pattern_type {
67         GREP_PATTERN_TYPE_UNSPECIFIED = 0,
68         GREP_PATTERN_TYPE_BRE,
69         GREP_PATTERN_TYPE_ERE,
70         GREP_PATTERN_TYPE_FIXED,
71         GREP_PATTERN_TYPE_PCRE
72 };
73
74 struct grep_expr {
75         enum grep_expr_node node;
76         unsigned hit;
77         union {
78                 struct grep_pat *atom;
79                 struct grep_expr *unary;
80                 struct {
81                         struct grep_expr *left;
82                         struct grep_expr *right;
83                 } binary;
84         } u;
85 };
86
87 struct grep_opt {
88         struct grep_pat *pattern_list;
89         struct grep_pat **pattern_tail;
90         struct grep_pat *header_list;
91         struct grep_pat **header_tail;
92         struct grep_expr *pattern_expression;
93         const char *prefix;
94         int prefix_length;
95         regex_t regexp;
96         int linenum;
97         int invert;
98         int ignore_case;
99         int status_only;
100         int name_only;
101         int unmatch_name_only;
102         int count;
103         int word_regexp;
104         int fixed;
105         int all_match;
106         int debug;
107 #define GREP_BINARY_DEFAULT     0
108 #define GREP_BINARY_NOMATCH     1
109 #define GREP_BINARY_TEXT        2
110         int binary;
111         int allow_textconv;
112         int extended;
113         int use_reflog_filter;
114         int pcre;
115         int relative;
116         int pathname;
117         int null_following_name;
118         int color;
119         int max_depth;
120         int funcname;
121         int funcbody;
122         int extended_regexp_option;
123         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         char color_match_context[COLOR_MAXLEN];
129         char color_match_selected[COLOR_MAXLEN];
130         char color_selected[COLOR_MAXLEN];
131         char color_sep[COLOR_MAXLEN];
132         int regflags;
133         unsigned pre_context;
134         unsigned post_context;
135         unsigned last_shown;
136         int show_hunk_mark;
137         int file_break;
138         int heading;
139         void *priv;
140
141         void (*output)(struct grep_opt *opt, const void *data, size_t size);
142         void *output_priv;
143 };
144
145 extern void init_grep_defaults(void);
146 extern int grep_config(const char *var, const char *value, void *);
147 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 struct grep_source {
158         char *name;
159
160         enum grep_source_type {
161                 GREP_SOURCE_SHA1,
162                 GREP_SOURCE_FILE,
163                 GREP_SOURCE_BUF,
164                 GREP_SOURCE_SUBMODULE,
165         } type;
166         void *identifier;
167
168         char *buf;
169         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                       const char *name, const char *path,
177                       const void *identifier);
178 void grep_source_clear_data(struct grep_source *gs);
179 void grep_source_clear(struct grep_source *gs);
180 void grep_source_load_driver(struct grep_source *gs);
181
182
183 int grep_source(struct grep_opt *opt, struct grep_source *gs);
184
185 extern struct grep_opt *grep_opt_dup(const struct grep_opt *opt);
186 extern int grep_threads_ok(const struct grep_opt *opt);
187
188 #ifndef NO_PTHREADS
189 /*
190  * Mutex used around access to the attributes machinery if
191  * opt->use_threads.  Must be initialized/destroyed by callers!
192  */
193 extern int grep_use_locks;
194 extern pthread_mutex_t grep_attr_mutex;
195 extern pthread_mutex_t grep_read_mutex;
196
197 static inline void grep_read_lock(void)
198 {
199         if (grep_use_locks)
200                 pthread_mutex_lock(&grep_read_mutex);
201 }
202
203 static inline void grep_read_unlock(void)
204 {
205         if (grep_use_locks)
206                 pthread_mutex_unlock(&grep_read_mutex);
207 }
208
209 #else
210 #define grep_read_lock()
211 #define grep_read_unlock()
212 #endif
213
214 #endif