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