Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 1 | #ifndef GREP_H |
| 2 | #define GREP_H |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 3 | #include "color.h" |
Michał Kiedrowicz | 63e7e9d | 2011-05-09 23:52:05 +0200 | [diff] [blame] | 4 | #ifdef USE_LIBPCRE |
| 5 | #include <pcre.h> |
| 6 | #else |
| 7 | typedef int pcre; |
| 8 | typedef int pcre_extra; |
| 9 | #endif |
Fredrik Kuivinen | 9ecedde | 2011-08-21 00:42:18 +0200 | [diff] [blame] | 10 | #include "kwset.h" |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 11 | #include "thread-utils.h" |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 12 | #include "userdiff.h" |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 13 | |
| 14 | enum grep_pat_token { |
| 15 | GREP_PATTERN, |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 16 | GREP_PATTERN_HEAD, |
| 17 | GREP_PATTERN_BODY, |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 18 | GREP_AND, |
| 19 | GREP_OPEN_PAREN, |
| 20 | GREP_CLOSE_PAREN, |
| 21 | GREP_NOT, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 22 | GREP_OR |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 23 | }; |
| 24 | |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 25 | enum grep_context { |
| 26 | GREP_CONTEXT_HEAD, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 27 | GREP_CONTEXT_BODY |
Junio C Hamano | 480c1ca | 2006-09-20 12:39:46 -0700 | [diff] [blame] | 28 | }; |
| 29 | |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 30 | enum grep_header_field { |
Antoine Pelisse | 3ce3ffb | 2013-02-03 14:37:09 +0000 | [diff] [blame] | 31 | GREP_HEADER_FIELD_MIN = 0, |
| 32 | GREP_HEADER_AUTHOR = GREP_HEADER_FIELD_MIN, |
Nguyễn Thái Ngọc Duy | ad4813b | 2012-09-29 11:41:27 +0700 | [diff] [blame] | 33 | GREP_HEADER_COMMITTER, |
Nguyễn Thái Ngọc Duy | 72fd13f | 2012-09-29 11:41:28 +0700 | [diff] [blame] | 34 | GREP_HEADER_REFLOG, |
Nguyễn Thái Ngọc Duy | ad4813b | 2012-09-29 11:41:27 +0700 | [diff] [blame] | 35 | |
| 36 | /* Must be at the end of the enum */ |
| 37 | GREP_HEADER_FIELD_MAX |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 40 | struct grep_pat { |
| 41 | struct grep_pat *next; |
| 42 | const char *origin; |
| 43 | int no; |
| 44 | enum grep_pat_token token; |
René Scharfe | 526a858 | 2012-05-20 16:33:07 +0200 | [diff] [blame] | 45 | char *pattern; |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 46 | size_t patternlen; |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 47 | enum grep_header_field field; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 48 | regex_t regexp; |
Michał Kiedrowicz | 63e7e9d | 2011-05-09 23:52:05 +0200 | [diff] [blame] | 49 | pcre *pcre_regexp; |
| 50 | pcre_extra *pcre_extra_info; |
Fredrik Kuivinen | 9ecedde | 2011-08-21 00:42:18 +0200 | [diff] [blame] | 51 | kwset_t kws; |
René Scharfe | c822255 | 2009-01-10 00:18:34 +0100 | [diff] [blame] | 52 | unsigned fixed:1; |
Brian Collins | 5183bf6 | 2009-11-06 01:22:35 -0800 | [diff] [blame] | 53 | unsigned ignore_case:1; |
René Scharfe | d7eb527 | 2009-03-07 13:28:40 +0100 | [diff] [blame] | 54 | unsigned word_regexp:1; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | enum grep_expr_node { |
| 58 | GREP_NODE_ATOM, |
| 59 | GREP_NODE_NOT, |
| 60 | GREP_NODE_AND, |
Junio C Hamano | 5aaeb73 | 2010-09-12 22:15:35 -0700 | [diff] [blame] | 61 | GREP_NODE_TRUE, |
Gary V. Vaughan | 4b05548 | 2010-05-14 09:31:35 +0000 | [diff] [blame] | 62 | GREP_NODE_OR |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
J Smith | 84befcd | 2012-08-03 10:53:50 -0400 | [diff] [blame] | 65 | enum 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 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 73 | struct grep_expr { |
| 74 | enum grep_expr_node node; |
Junio C Hamano | 0ab7bef | 2006-09-27 17:50:52 -0700 | [diff] [blame] | 75 | unsigned hit; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 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 | |
| 86 | struct grep_opt { |
| 87 | struct grep_pat *pattern_list; |
| 88 | struct grep_pat **pattern_tail; |
Junio C Hamano | 80235ba | 2010-01-17 20:09:06 -0800 | [diff] [blame] | 89 | struct grep_pat *header_list; |
| 90 | struct grep_pat **header_tail; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 91 | struct grep_expr *pattern_expression; |
Clemens Buchacher | 493b7a0 | 2009-09-05 14:31:17 +0200 | [diff] [blame] | 92 | const char *prefix; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 93 | int prefix_length; |
| 94 | regex_t regexp; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 95 | int linenum; |
| 96 | int invert; |
Brian Collins | 5183bf6 | 2009-11-06 01:22:35 -0800 | [diff] [blame] | 97 | int ignore_case; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 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; |
Junio C Hamano | 17bf35a | 2012-09-13 14:21:44 -0700 | [diff] [blame] | 105 | int debug; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 106 | #define GREP_BINARY_DEFAULT 0 |
| 107 | #define GREP_BINARY_NOMATCH 1 |
| 108 | #define GREP_BINARY_TEXT 2 |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 109 | int binary; |
Jeff King | 335ec3b | 2013-05-10 17:10:15 +0200 | [diff] [blame] | 110 | int allow_textconv; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 111 | int extended; |
Junio C Hamano | baa6378 | 2012-09-29 11:59:52 -0700 | [diff] [blame] | 112 | int use_reflog_filter; |
Michał Kiedrowicz | 63e7e9d | 2011-05-09 23:52:05 +0200 | [diff] [blame] | 113 | int pcre; |
René Scharfe | 3e230fa | 2009-05-07 21:46:48 +0200 | [diff] [blame] | 114 | int relative; |
| 115 | int pathname; |
| 116 | int null_following_name; |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 117 | int color; |
Michał Kiedrowicz | a91f453 | 2009-07-22 19:52:15 +0200 | [diff] [blame] | 118 | int max_depth; |
René Scharfe | 2944e4e | 2009-07-02 00:06:34 +0200 | [diff] [blame] | 119 | int funcname; |
René Scharfe | ba8ea74 | 2011-08-01 19:20:53 +0200 | [diff] [blame] | 120 | int funcbody; |
J Smith | 84befcd | 2012-08-03 10:53:50 -0400 | [diff] [blame] | 121 | int extended_regexp_option; |
| 122 | int pattern_type_option; |
Mark Lodato | 00588bb | 2010-03-07 11:52:47 -0500 | [diff] [blame] | 123 | char color_context[COLOR_MAXLEN]; |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 124 | char color_filename[COLOR_MAXLEN]; |
Mark Lodato | 00588bb | 2010-03-07 11:52:47 -0500 | [diff] [blame] | 125 | char color_function[COLOR_MAXLEN]; |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 126 | char color_lineno[COLOR_MAXLEN]; |
René Scharfe | 7e8f59d | 2009-03-07 13:32:32 +0100 | [diff] [blame] | 127 | char color_match[COLOR_MAXLEN]; |
Mark Lodato | 00588bb | 2010-03-07 11:52:47 -0500 | [diff] [blame] | 128 | char color_selected[COLOR_MAXLEN]; |
Mark Lodato | 55f638b | 2010-03-07 11:52:46 -0500 | [diff] [blame] | 129 | char color_sep[COLOR_MAXLEN]; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 130 | int regflags; |
| 131 | unsigned pre_context; |
| 132 | unsigned post_context; |
René Scharfe | 5dd06d3 | 2009-07-02 00:02:38 +0200 | [diff] [blame] | 133 | unsigned last_shown; |
René Scharfe | 046802d | 2009-07-02 00:03:44 +0200 | [diff] [blame] | 134 | int show_hunk_mark; |
René Scharfe | a8f0e76 | 2011-06-05 17:24:25 +0200 | [diff] [blame] | 135 | int file_break; |
René Scharfe | 1d84f72 | 2011-06-05 17:24:36 +0200 | [diff] [blame] | 136 | int heading; |
René Scharfe | 60ecac9 | 2009-07-02 00:07:24 +0200 | [diff] [blame] | 137 | void *priv; |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 138 | |
| 139 | void (*output)(struct grep_opt *opt, const void *data, size_t size); |
| 140 | void *output_priv; |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 143 | extern void init_grep_defaults(void); |
| 144 | extern int grep_config(const char *var, const char *value, void *); |
| 145 | extern void grep_init(struct grep_opt *, const char *prefix); |
Junio C Hamano | c5c31d3 | 2012-10-03 14:47:48 -0700 | [diff] [blame] | 146 | void grep_set_pattern_type_option(enum grep_pattern_type, struct grep_opt *opt); |
| 147 | void grep_commit_pattern_type(enum grep_pattern_type, struct grep_opt *opt); |
Junio C Hamano | 7687a05 | 2012-10-09 16:17:50 -0700 | [diff] [blame] | 148 | |
René Scharfe | ed40a09 | 2010-05-22 23:43:43 +0200 | [diff] [blame] | 149 | 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); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 150 | extern void append_grep_pattern(struct grep_opt *opt, const char *pat, const char *origin, int no, enum grep_pat_token t); |
Junio C Hamano | a4d7d2c | 2008-09-04 22:15:02 -0700 | [diff] [blame] | 151 | extern void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const char *); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 152 | extern void compile_grep_patterns(struct grep_opt *opt); |
Junio C Hamano | b48fb5b | 2006-09-27 16:27:10 -0700 | [diff] [blame] | 153 | extern void free_grep_patterns(struct grep_opt *opt); |
Jeff King | c876d6d | 2012-02-02 03:20:10 -0500 | [diff] [blame] | 154 | extern int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size); |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 155 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 156 | struct grep_source { |
| 157 | char *name; |
| 158 | |
| 159 | enum grep_source_type { |
| 160 | GREP_SOURCE_SHA1, |
| 161 | GREP_SOURCE_FILE, |
| 162 | GREP_SOURCE_BUF, |
| 163 | } type; |
| 164 | void *identifier; |
| 165 | |
| 166 | char *buf; |
| 167 | unsigned long size; |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 168 | |
Nguyễn Thái Ngọc Duy | 55c6168 | 2012-10-12 17:49:38 +0700 | [diff] [blame] | 169 | char *path; /* for attribute lookups */ |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 170 | struct userdiff_driver *driver; |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 171 | }; |
| 172 | |
| 173 | void grep_source_init(struct grep_source *gs, enum grep_source_type type, |
Nguyễn Thái Ngọc Duy | 55c6168 | 2012-10-12 17:49:38 +0700 | [diff] [blame] | 174 | const char *name, const char *path, |
| 175 | const void *identifier); |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 176 | void grep_source_clear_data(struct grep_source *gs); |
| 177 | void grep_source_clear(struct grep_source *gs); |
Jeff King | 94ad9d9 | 2012-02-02 03:20:43 -0500 | [diff] [blame] | 178 | void grep_source_load_driver(struct grep_source *gs); |
Junio C Hamano | 07a7d65 | 2012-09-15 14:04:36 -0700 | [diff] [blame] | 179 | |
Jeff King | e132702 | 2012-02-02 03:19:28 -0500 | [diff] [blame] | 180 | |
| 181 | int grep_source(struct grep_opt *opt, struct grep_source *gs); |
| 182 | |
Fredrik Kuivinen | 5b594f4 | 2010-01-25 23:51:39 +0100 | [diff] [blame] | 183 | extern struct grep_opt *grep_opt_dup(const struct grep_opt *opt); |
| 184 | extern int grep_threads_ok(const struct grep_opt *opt); |
| 185 | |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 186 | #ifndef NO_PTHREADS |
| 187 | /* |
| 188 | * Mutex used around access to the attributes machinery if |
| 189 | * opt->use_threads. Must be initialized/destroyed by callers! |
| 190 | */ |
Jeff King | 78db6ea | 2012-02-02 03:18:29 -0500 | [diff] [blame] | 191 | extern int grep_use_locks; |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 192 | extern pthread_mutex_t grep_attr_mutex; |
Jeff King | b3aeb28 | 2012-02-02 03:18:41 -0500 | [diff] [blame] | 193 | extern pthread_mutex_t grep_read_mutex; |
| 194 | |
| 195 | static inline void grep_read_lock(void) |
| 196 | { |
| 197 | if (grep_use_locks) |
| 198 | pthread_mutex_lock(&grep_read_mutex); |
| 199 | } |
| 200 | |
| 201 | static inline void grep_read_unlock(void) |
| 202 | { |
| 203 | if (grep_use_locks) |
| 204 | pthread_mutex_unlock(&grep_read_mutex); |
| 205 | } |
| 206 | |
| 207 | #else |
| 208 | #define grep_read_lock() |
| 209 | #define grep_read_unlock() |
Thomas Rast | 0579f91 | 2011-12-12 22:16:07 +0100 | [diff] [blame] | 210 | #endif |
| 211 | |
Junio C Hamano | 83b5d2f | 2006-09-17 16:02:52 -0700 | [diff] [blame] | 212 | #endif |