]>
Commit | Line | Data |
---|---|---|
83b5d2f5 JH |
1 | #include "cache.h" |
2 | #include <regex.h> | |
3 | #include "grep.h" | |
4 | ||
5 | void append_grep_pattern(struct grep_opt *opt, const char *pat, | |
6 | const char *origin, int no, enum grep_pat_token t) | |
7 | { | |
8 | struct grep_pat *p = xcalloc(1, sizeof(*p)); | |
9 | p->pattern = pat; | |
10 | p->origin = origin; | |
11 | p->no = no; | |
12 | p->token = t; | |
13 | *opt->pattern_tail = p; | |
14 | opt->pattern_tail = &p->next; | |
15 | p->next = NULL; | |
16 | } | |
17 | ||
18 | static void compile_regexp(struct grep_pat *p, struct grep_opt *opt) | |
19 | { | |
20 | int err = regcomp(&p->regexp, p->pattern, opt->regflags); | |
21 | if (err) { | |
22 | char errbuf[1024]; | |
23 | char where[1024]; | |
24 | if (p->no) | |
25 | sprintf(where, "In '%s' at %d, ", | |
26 | p->origin, p->no); | |
27 | else if (p->origin) | |
28 | sprintf(where, "%s, ", p->origin); | |
29 | else | |
30 | where[0] = 0; | |
31 | regerror(err, &p->regexp, errbuf, 1024); | |
32 | regfree(&p->regexp); | |
33 | die("%s'%s': %s", where, p->pattern, errbuf); | |
34 | } | |
35 | } | |
36 | ||
37 | static struct grep_expr *compile_pattern_expr(struct grep_pat **); | |
38 | static struct grep_expr *compile_pattern_atom(struct grep_pat **list) | |
39 | { | |
40 | struct grep_pat *p; | |
41 | struct grep_expr *x; | |
42 | ||
43 | p = *list; | |
44 | switch (p->token) { | |
45 | case GREP_PATTERN: /* atom */ | |
480c1ca6 JH |
46 | case GREP_PATTERN_HEAD: |
47 | case GREP_PATTERN_BODY: | |
83b5d2f5 JH |
48 | x = xcalloc(1, sizeof (struct grep_expr)); |
49 | x->node = GREP_NODE_ATOM; | |
50 | x->u.atom = p; | |
51 | *list = p->next; | |
52 | return x; | |
53 | case GREP_OPEN_PAREN: | |
54 | *list = p->next; | |
55 | x = compile_pattern_expr(list); | |
56 | if (!x) | |
57 | return NULL; | |
58 | if (!*list || (*list)->token != GREP_CLOSE_PAREN) | |
59 | die("unmatched parenthesis"); | |
60 | *list = (*list)->next; | |
61 | return x; | |
62 | default: | |
63 | return NULL; | |
64 | } | |
65 | } | |
66 | ||
67 | static struct grep_expr *compile_pattern_not(struct grep_pat **list) | |
68 | { | |
69 | struct grep_pat *p; | |
70 | struct grep_expr *x; | |
71 | ||
72 | p = *list; | |
73 | switch (p->token) { | |
74 | case GREP_NOT: | |
75 | if (!p->next) | |
76 | die("--not not followed by pattern expression"); | |
77 | *list = p->next; | |
78 | x = xcalloc(1, sizeof (struct grep_expr)); | |
79 | x->node = GREP_NODE_NOT; | |
80 | x->u.unary = compile_pattern_not(list); | |
81 | if (!x->u.unary) | |
82 | die("--not followed by non pattern expression"); | |
83 | return x; | |
84 | default: | |
85 | return compile_pattern_atom(list); | |
86 | } | |
87 | } | |
88 | ||
89 | static struct grep_expr *compile_pattern_and(struct grep_pat **list) | |
90 | { | |
91 | struct grep_pat *p; | |
92 | struct grep_expr *x, *y, *z; | |
93 | ||
94 | x = compile_pattern_not(list); | |
95 | p = *list; | |
96 | if (p && p->token == GREP_AND) { | |
97 | if (!p->next) | |
98 | die("--and not followed by pattern expression"); | |
99 | *list = p->next; | |
100 | y = compile_pattern_and(list); | |
101 | if (!y) | |
102 | die("--and not followed by pattern expression"); | |
103 | z = xcalloc(1, sizeof (struct grep_expr)); | |
104 | z->node = GREP_NODE_AND; | |
105 | z->u.binary.left = x; | |
106 | z->u.binary.right = y; | |
107 | return z; | |
108 | } | |
109 | return x; | |
110 | } | |
111 | ||
112 | static struct grep_expr *compile_pattern_or(struct grep_pat **list) | |
113 | { | |
114 | struct grep_pat *p; | |
115 | struct grep_expr *x, *y, *z; | |
116 | ||
117 | x = compile_pattern_and(list); | |
118 | p = *list; | |
119 | if (x && p && p->token != GREP_CLOSE_PAREN) { | |
120 | y = compile_pattern_or(list); | |
121 | if (!y) | |
122 | die("not a pattern expression %s", p->pattern); | |
123 | z = xcalloc(1, sizeof (struct grep_expr)); | |
124 | z->node = GREP_NODE_OR; | |
125 | z->u.binary.left = x; | |
126 | z->u.binary.right = y; | |
127 | return z; | |
128 | } | |
129 | return x; | |
130 | } | |
131 | ||
132 | static struct grep_expr *compile_pattern_expr(struct grep_pat **list) | |
133 | { | |
134 | return compile_pattern_or(list); | |
135 | } | |
136 | ||
137 | void compile_grep_patterns(struct grep_opt *opt) | |
138 | { | |
139 | struct grep_pat *p; | |
140 | ||
141 | if (opt->fixed) | |
142 | return; | |
143 | ||
144 | /* First compile regexps */ | |
145 | for (p = opt->pattern_list; p; p = p->next) { | |
480c1ca6 JH |
146 | switch (p->token) { |
147 | case GREP_PATTERN: /* atom */ | |
148 | case GREP_PATTERN_HEAD: | |
149 | case GREP_PATTERN_BODY: | |
83b5d2f5 | 150 | compile_regexp(p, opt); |
480c1ca6 JH |
151 | break; |
152 | default: | |
83b5d2f5 | 153 | opt->extended = 1; |
480c1ca6 JH |
154 | break; |
155 | } | |
83b5d2f5 JH |
156 | } |
157 | ||
158 | if (!opt->extended) | |
159 | return; | |
160 | ||
161 | /* Then bundle them up in an expression. | |
162 | * A classic recursive descent parser would do. | |
163 | */ | |
164 | p = opt->pattern_list; | |
165 | opt->pattern_expression = compile_pattern_expr(&p); | |
166 | if (p) | |
167 | die("incomplete pattern expression: %s", p->pattern); | |
168 | } | |
169 | ||
170 | static char *end_of_line(char *cp, unsigned long *left) | |
171 | { | |
172 | unsigned long l = *left; | |
173 | while (l && *cp != '\n') { | |
174 | l--; | |
175 | cp++; | |
176 | } | |
177 | *left = l; | |
178 | return cp; | |
179 | } | |
180 | ||
181 | static int word_char(char ch) | |
182 | { | |
183 | return isalnum(ch) || ch == '_'; | |
184 | } | |
185 | ||
186 | static void show_line(struct grep_opt *opt, const char *bol, const char *eol, | |
187 | const char *name, unsigned lno, char sign) | |
188 | { | |
189 | if (opt->pathname) | |
190 | printf("%s%c", name, sign); | |
191 | if (opt->linenum) | |
192 | printf("%d%c", lno, sign); | |
193 | printf("%.*s\n", (int)(eol-bol), bol); | |
194 | } | |
195 | ||
196 | /* | |
197 | * NEEDSWORK: share code with diff.c | |
198 | */ | |
199 | #define FIRST_FEW_BYTES 8000 | |
200 | static int buffer_is_binary(const char *ptr, unsigned long size) | |
201 | { | |
202 | if (FIRST_FEW_BYTES < size) | |
203 | size = FIRST_FEW_BYTES; | |
204 | return !!memchr(ptr, 0, size); | |
205 | } | |
206 | ||
207 | static int fixmatch(const char *pattern, char *line, regmatch_t *match) | |
208 | { | |
209 | char *hit = strstr(line, pattern); | |
210 | if (!hit) { | |
211 | match->rm_so = match->rm_eo = -1; | |
212 | return REG_NOMATCH; | |
213 | } | |
214 | else { | |
215 | match->rm_so = hit - line; | |
216 | match->rm_eo = match->rm_so + strlen(pattern); | |
217 | return 0; | |
218 | } | |
219 | } | |
220 | ||
480c1ca6 | 221 | static int match_one_pattern(struct grep_opt *opt, struct grep_pat *p, char *bol, char *eol, enum grep_context ctx) |
83b5d2f5 JH |
222 | { |
223 | int hit = 0; | |
224 | int at_true_bol = 1; | |
225 | regmatch_t pmatch[10]; | |
226 | ||
480c1ca6 JH |
227 | if ((p->token != GREP_PATTERN) && |
228 | ((p->token == GREP_PATTERN_HEAD) != (ctx == GREP_CONTEXT_HEAD))) | |
229 | return 0; | |
230 | ||
83b5d2f5 JH |
231 | again: |
232 | if (!opt->fixed) { | |
233 | regex_t *exp = &p->regexp; | |
234 | hit = !regexec(exp, bol, ARRAY_SIZE(pmatch), | |
235 | pmatch, 0); | |
236 | } | |
237 | else { | |
238 | hit = !fixmatch(p->pattern, bol, pmatch); | |
239 | } | |
240 | ||
241 | if (hit && opt->word_regexp) { | |
242 | if ((pmatch[0].rm_so < 0) || | |
243 | (eol - bol) <= pmatch[0].rm_so || | |
244 | (pmatch[0].rm_eo < 0) || | |
245 | (eol - bol) < pmatch[0].rm_eo) | |
246 | die("regexp returned nonsense"); | |
247 | ||
248 | /* Match beginning must be either beginning of the | |
249 | * line, or at word boundary (i.e. the last char must | |
250 | * not be a word char). Similarly, match end must be | |
251 | * either end of the line, or at word boundary | |
252 | * (i.e. the next char must not be a word char). | |
253 | */ | |
254 | if ( ((pmatch[0].rm_so == 0 && at_true_bol) || | |
255 | !word_char(bol[pmatch[0].rm_so-1])) && | |
256 | ((pmatch[0].rm_eo == (eol-bol)) || | |
257 | !word_char(bol[pmatch[0].rm_eo])) ) | |
258 | ; | |
259 | else | |
260 | hit = 0; | |
261 | ||
262 | if (!hit && pmatch[0].rm_so + bol + 1 < eol) { | |
263 | /* There could be more than one match on the | |
264 | * line, and the first match might not be | |
265 | * strict word match. But later ones could be! | |
266 | */ | |
267 | bol = pmatch[0].rm_so + bol + 1; | |
268 | at_true_bol = 0; | |
269 | goto again; | |
270 | } | |
271 | } | |
272 | return hit; | |
273 | } | |
274 | ||
275 | static int match_expr_eval(struct grep_opt *opt, | |
276 | struct grep_expr *x, | |
480c1ca6 JH |
277 | char *bol, char *eol, |
278 | enum grep_context ctx) | |
83b5d2f5 JH |
279 | { |
280 | switch (x->node) { | |
281 | case GREP_NODE_ATOM: | |
480c1ca6 | 282 | return match_one_pattern(opt, x->u.atom, bol, eol, ctx); |
83b5d2f5 JH |
283 | break; |
284 | case GREP_NODE_NOT: | |
480c1ca6 | 285 | return !match_expr_eval(opt, x->u.unary, bol, eol, ctx); |
83b5d2f5 | 286 | case GREP_NODE_AND: |
480c1ca6 JH |
287 | return (match_expr_eval(opt, x->u.binary.left, bol, eol, ctx) && |
288 | match_expr_eval(opt, x->u.binary.right, bol, eol, ctx)); | |
83b5d2f5 | 289 | case GREP_NODE_OR: |
480c1ca6 JH |
290 | return (match_expr_eval(opt, x->u.binary.left, bol, eol, ctx) || |
291 | match_expr_eval(opt, x->u.binary.right, bol, eol, ctx)); | |
83b5d2f5 JH |
292 | } |
293 | die("Unexpected node type (internal error) %d\n", x->node); | |
294 | } | |
295 | ||
480c1ca6 JH |
296 | static int match_expr(struct grep_opt *opt, char *bol, char *eol, |
297 | enum grep_context ctx) | |
83b5d2f5 JH |
298 | { |
299 | struct grep_expr *x = opt->pattern_expression; | |
480c1ca6 | 300 | return match_expr_eval(opt, x, bol, eol, ctx); |
83b5d2f5 JH |
301 | } |
302 | ||
480c1ca6 JH |
303 | static int match_line(struct grep_opt *opt, char *bol, char *eol, |
304 | enum grep_context ctx) | |
83b5d2f5 JH |
305 | { |
306 | struct grep_pat *p; | |
307 | if (opt->extended) | |
480c1ca6 | 308 | return match_expr(opt, bol, eol, ctx); |
83b5d2f5 | 309 | for (p = opt->pattern_list; p; p = p->next) { |
480c1ca6 | 310 | if (match_one_pattern(opt, p, bol, eol, ctx)) |
83b5d2f5 JH |
311 | return 1; |
312 | } | |
313 | return 0; | |
314 | } | |
315 | ||
316 | int grep_buffer(struct grep_opt *opt, const char *name, char *buf, unsigned long size) | |
317 | { | |
318 | char *bol = buf; | |
319 | unsigned long left = size; | |
320 | unsigned lno = 1; | |
321 | struct pre_context_line { | |
322 | char *bol; | |
323 | char *eol; | |
324 | } *prev = NULL, *pcl; | |
325 | unsigned last_hit = 0; | |
326 | unsigned last_shown = 0; | |
327 | int binary_match_only = 0; | |
328 | const char *hunk_mark = ""; | |
329 | unsigned count = 0; | |
480c1ca6 | 330 | enum grep_context ctx = GREP_CONTEXT_HEAD; |
83b5d2f5 JH |
331 | |
332 | if (buffer_is_binary(buf, size)) { | |
333 | switch (opt->binary) { | |
334 | case GREP_BINARY_DEFAULT: | |
335 | binary_match_only = 1; | |
336 | break; | |
337 | case GREP_BINARY_NOMATCH: | |
338 | return 0; /* Assume unmatch */ | |
339 | break; | |
340 | default: | |
341 | break; | |
342 | } | |
343 | } | |
344 | ||
345 | if (opt->pre_context) | |
346 | prev = xcalloc(opt->pre_context, sizeof(*prev)); | |
347 | if (opt->pre_context || opt->post_context) | |
348 | hunk_mark = "--\n"; | |
349 | ||
350 | while (left) { | |
351 | char *eol, ch; | |
352 | int hit = 0; | |
353 | ||
354 | eol = end_of_line(bol, &left); | |
355 | ch = *eol; | |
356 | *eol = 0; | |
357 | ||
480c1ca6 JH |
358 | if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol)) |
359 | ctx = GREP_CONTEXT_BODY; | |
360 | ||
361 | hit = match_line(opt, bol, eol, ctx); | |
83b5d2f5 JH |
362 | *eol = ch; |
363 | ||
364 | /* "grep -v -e foo -e bla" should list lines | |
365 | * that do not have either, so inversion should | |
366 | * be done outside. | |
367 | */ | |
368 | if (opt->invert) | |
369 | hit = !hit; | |
370 | if (opt->unmatch_name_only) { | |
371 | if (hit) | |
372 | return 0; | |
373 | goto next_line; | |
374 | } | |
375 | if (hit) { | |
376 | count++; | |
377 | if (opt->status_only) | |
378 | return 1; | |
379 | if (binary_match_only) { | |
380 | printf("Binary file %s matches\n", name); | |
381 | return 1; | |
382 | } | |
383 | if (opt->name_only) { | |
384 | printf("%s\n", name); | |
385 | return 1; | |
386 | } | |
387 | /* Hit at this line. If we haven't shown the | |
388 | * pre-context lines, we would need to show them. | |
389 | * When asked to do "count", this still show | |
390 | * the context which is nonsense, but the user | |
391 | * deserves to get that ;-). | |
392 | */ | |
393 | if (opt->pre_context) { | |
394 | unsigned from; | |
395 | if (opt->pre_context < lno) | |
396 | from = lno - opt->pre_context; | |
397 | else | |
398 | from = 1; | |
399 | if (from <= last_shown) | |
400 | from = last_shown + 1; | |
401 | if (last_shown && from != last_shown + 1) | |
402 | printf(hunk_mark); | |
403 | while (from < lno) { | |
404 | pcl = &prev[lno-from-1]; | |
405 | show_line(opt, pcl->bol, pcl->eol, | |
406 | name, from, '-'); | |
407 | from++; | |
408 | } | |
409 | last_shown = lno-1; | |
410 | } | |
411 | if (last_shown && lno != last_shown + 1) | |
412 | printf(hunk_mark); | |
413 | if (!opt->count) | |
414 | show_line(opt, bol, eol, name, lno, ':'); | |
415 | last_shown = last_hit = lno; | |
416 | } | |
417 | else if (last_hit && | |
418 | lno <= last_hit + opt->post_context) { | |
419 | /* If the last hit is within the post context, | |
420 | * we need to show this line. | |
421 | */ | |
422 | if (last_shown && lno != last_shown + 1) | |
423 | printf(hunk_mark); | |
424 | show_line(opt, bol, eol, name, lno, '-'); | |
425 | last_shown = lno; | |
426 | } | |
427 | if (opt->pre_context) { | |
428 | memmove(prev+1, prev, | |
429 | (opt->pre_context-1) * sizeof(*prev)); | |
430 | prev->bol = bol; | |
431 | prev->eol = eol; | |
432 | } | |
433 | ||
434 | next_line: | |
435 | bol = eol + 1; | |
436 | if (!left) | |
437 | break; | |
438 | left--; | |
439 | lno++; | |
440 | } | |
441 | ||
442 | if (opt->status_only) | |
443 | return 0; | |
444 | if (opt->unmatch_name_only) { | |
445 | /* We did not see any hit, so we want to show this */ | |
446 | printf("%s\n", name); | |
447 | return 1; | |
448 | } | |
449 | ||
450 | /* NEEDSWORK: | |
451 | * The real "grep -c foo *.c" gives many "bar.c:0" lines, | |
452 | * which feels mostly useless but sometimes useful. Maybe | |
453 | * make it another option? For now suppress them. | |
454 | */ | |
455 | if (opt->count && count) | |
456 | printf("%s:%u\n", name, count); | |
457 | return !!last_hit; | |
458 | } | |
459 |