util.c revision 1.7 1 1.7 joerg /* $NetBSD: util.c,v 1.7 2011/02/16 01:31:33 joerg Exp $ */
2 1.7 joerg /* $FreeBSD: head/usr.bin/grep/util.c 211496 2010-08-19 09:28:59Z des $ */
3 1.7 joerg /* $OpenBSD: util.c,v 1.39 2010/07/02 22:18:03 tedu Exp $ */
4 1.2 dsl
5 1.1 cjep /*-
6 1.7 joerg * Copyright (c) 1999 James Howard and Dag-Erling Codan Smrgrav
7 1.7 joerg * Copyright (C) 2008-2010 Gabor Kovesdan <gabor (at) FreeBSD.org>
8 1.1 cjep * All rights reserved.
9 1.1 cjep *
10 1.1 cjep * Redistribution and use in source and binary forms, with or without
11 1.1 cjep * modification, are permitted provided that the following conditions
12 1.1 cjep * are met:
13 1.1 cjep * 1. Redistributions of source code must retain the above copyright
14 1.1 cjep * notice, this list of conditions and the following disclaimer.
15 1.1 cjep * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 cjep * notice, this list of conditions and the following disclaimer in the
17 1.1 cjep * documentation and/or other materials provided with the distribution.
18 1.1 cjep *
19 1.1 cjep * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 1.1 cjep * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.1 cjep * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.1 cjep * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 1.1 cjep * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.1 cjep * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.1 cjep * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.1 cjep * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.1 cjep * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.1 cjep * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.1 cjep * SUCH DAMAGE.
30 1.1 cjep */
31 1.1 cjep
32 1.2 dsl #include <sys/cdefs.h>
33 1.7 joerg __RCSID("$NetBSD: util.c,v 1.7 2011/02/16 01:31:33 joerg Exp $");
34 1.2 dsl
35 1.7 joerg #include <sys/stat.h>
36 1.1 cjep #include <sys/types.h>
37 1.1 cjep
38 1.1 cjep #include <ctype.h>
39 1.1 cjep #include <err.h>
40 1.1 cjep #include <errno.h>
41 1.7 joerg #include <fnmatch.h>
42 1.1 cjep #include <fts.h>
43 1.7 joerg #include <libgen.h>
44 1.7 joerg #include <stdbool.h>
45 1.1 cjep #include <stdio.h>
46 1.1 cjep #include <stdlib.h>
47 1.1 cjep #include <string.h>
48 1.1 cjep #include <unistd.h>
49 1.7 joerg #include <wchar.h>
50 1.7 joerg #include <wctype.h>
51 1.1 cjep
52 1.1 cjep #include "grep.h"
53 1.1 cjep
54 1.7 joerg static int linesqueued;
55 1.7 joerg static int procline(struct str *l, int);
56 1.7 joerg
57 1.7 joerg bool
58 1.7 joerg file_matching(const char *fname)
59 1.7 joerg {
60 1.7 joerg bool ret;
61 1.7 joerg
62 1.7 joerg ret = finclude ? false : true;
63 1.7 joerg
64 1.7 joerg for (unsigned int i = 0; i < fpatterns; ++i) {
65 1.7 joerg if (fnmatch(fpattern[i].pat,
66 1.7 joerg fname, 0) == 0 || fnmatch(fpattern[i].pat,
67 1.7 joerg basename(fname), 0) == 0) {
68 1.7 joerg if (fpattern[i].mode == EXCL_PAT)
69 1.7 joerg return (false);
70 1.7 joerg else
71 1.7 joerg ret = true;
72 1.7 joerg }
73 1.7 joerg }
74 1.7 joerg return (ret);
75 1.7 joerg }
76 1.7 joerg
77 1.7 joerg static inline bool
78 1.7 joerg dir_matching(const char *dname)
79 1.7 joerg {
80 1.7 joerg bool ret;
81 1.7 joerg
82 1.7 joerg ret = dinclude ? false : true;
83 1.7 joerg
84 1.7 joerg for (unsigned int i = 0; i < dpatterns; ++i) {
85 1.7 joerg if (dname != NULL &&
86 1.7 joerg fnmatch(dname, dpattern[i].pat, 0) == 0) {
87 1.7 joerg if (dpattern[i].mode == EXCL_PAT)
88 1.7 joerg return (false);
89 1.7 joerg else
90 1.7 joerg ret = true;
91 1.7 joerg }
92 1.7 joerg }
93 1.7 joerg return (ret);
94 1.7 joerg }
95 1.7 joerg
96 1.1 cjep /*
97 1.7 joerg * Processes a directory when a recursive search is performed with
98 1.7 joerg * the -R option. Each appropriate file is passed to procfile().
99 1.1 cjep */
100 1.7 joerg int
101 1.1 cjep grep_tree(char **argv)
102 1.1 cjep {
103 1.2 dsl FTS *fts;
104 1.2 dsl FTSENT *p;
105 1.7 joerg char *d, *dir = NULL;
106 1.2 dsl int c, fts_flags;
107 1.7 joerg bool ok;
108 1.1 cjep
109 1.1 cjep c = fts_flags = 0;
110 1.1 cjep
111 1.7 joerg switch(linkbehave) {
112 1.7 joerg case LINK_EXPLICIT:
113 1.1 cjep fts_flags = FTS_COMFOLLOW;
114 1.7 joerg break;
115 1.7 joerg case LINK_SKIP:
116 1.1 cjep fts_flags = FTS_PHYSICAL;
117 1.7 joerg break;
118 1.7 joerg default:
119 1.7 joerg fts_flags = FTS_LOGICAL;
120 1.7 joerg
121 1.7 joerg }
122 1.1 cjep
123 1.7 joerg fts_flags |= FTS_NOSTAT | FTS_NOCHDIR;
124 1.1 cjep
125 1.7 joerg if (!(fts = fts_open(argv, fts_flags, NULL)))
126 1.7 joerg err(2, "fts_open");
127 1.1 cjep while ((p = fts_read(fts)) != NULL) {
128 1.1 cjep switch (p->fts_info) {
129 1.1 cjep case FTS_DNR:
130 1.7 joerg /* FALLTHROUGH */
131 1.1 cjep case FTS_ERR:
132 1.2 dsl errx(2, "%s: %s", p->fts_path, strerror(p->fts_errno));
133 1.1 cjep break;
134 1.7 joerg case FTS_D:
135 1.7 joerg /* FALLTHROUGH */
136 1.1 cjep case FTS_DP:
137 1.2 dsl break;
138 1.2 dsl case FTS_DC:
139 1.7 joerg /* Print a warning for recursive directory loop */
140 1.3 wiz warnx("warning: %s: recursive directory loop",
141 1.2 dsl p->fts_path);
142 1.1 cjep break;
143 1.1 cjep default:
144 1.7 joerg /* Check for file exclusion/inclusion */
145 1.7 joerg ok = true;
146 1.7 joerg if (dexclude || dinclude) {
147 1.7 joerg if ((d = strrchr(p->fts_path, '/')) != NULL) {
148 1.7 joerg dir = grep_malloc(sizeof(char) *
149 1.7 joerg (d - p->fts_path + 1));
150 1.7 joerg memcpy(dir, p->fts_path,
151 1.7 joerg d - p->fts_path);
152 1.7 joerg dir[d - p->fts_path] = '\0';
153 1.7 joerg }
154 1.7 joerg ok = dir_matching(dir);
155 1.7 joerg free(dir);
156 1.7 joerg dir = NULL;
157 1.7 joerg }
158 1.7 joerg if (fexclude || finclude)
159 1.7 joerg ok &= file_matching(p->fts_path);
160 1.7 joerg
161 1.7 joerg if (ok)
162 1.7 joerg c += procfile(p->fts_path);
163 1.1 cjep break;
164 1.1 cjep }
165 1.1 cjep }
166 1.1 cjep
167 1.7 joerg fts_close(fts);
168 1.7 joerg return (c);
169 1.1 cjep }
170 1.1 cjep
171 1.7 joerg /*
172 1.7 joerg * Opens a file and processes it. Each file is processed line-by-line
173 1.7 joerg * passing the lines to procline().
174 1.7 joerg */
175 1.1 cjep int
176 1.4 rillig procfile(const char *fn)
177 1.1 cjep {
178 1.7 joerg struct file *f;
179 1.2 dsl struct stat sb;
180 1.7 joerg struct str ln;
181 1.2 dsl mode_t s;
182 1.7 joerg int c, t;
183 1.7 joerg
184 1.7 joerg if (mflag && (mcount <= 0))
185 1.7 joerg return (0);
186 1.1 cjep
187 1.7 joerg if (strcmp(fn, "-") == 0) {
188 1.7 joerg fn = label != NULL ? label : getstr(1);
189 1.7 joerg f = grep_open(NULL);
190 1.1 cjep } else {
191 1.7 joerg if (!stat(fn, &sb)) {
192 1.7 joerg /* Check if we need to process the file */
193 1.7 joerg s = sb.st_mode & S_IFMT;
194 1.7 joerg if (s == S_IFDIR && dirbehave == DIR_SKIP)
195 1.7 joerg return (0);
196 1.7 joerg if ((s == S_IFIFO || s == S_IFCHR || s == S_IFBLK
197 1.7 joerg || s == S_IFSOCK) && devbehave == DEV_SKIP)
198 1.7 joerg return (0);
199 1.2 dsl }
200 1.7 joerg f = grep_open(fn);
201 1.1 cjep }
202 1.1 cjep if (f == NULL) {
203 1.1 cjep if (!sflag)
204 1.1 cjep warn("%s", fn);
205 1.7 joerg if (errno == ENOENT)
206 1.7 joerg notfound = true;
207 1.7 joerg return (0);
208 1.1 cjep }
209 1.1 cjep
210 1.7 joerg ln.file = grep_malloc(strlen(fn) + 1);
211 1.7 joerg strcpy(ln.file, fn);
212 1.1 cjep ln.line_no = 0;
213 1.7 joerg ln.len = 0;
214 1.1 cjep linesqueued = 0;
215 1.7 joerg tail = 0;
216 1.1 cjep ln.off = -1;
217 1.1 cjep
218 1.7 joerg for (c = 0; c == 0 || !(lflag || qflag); ) {
219 1.1 cjep ln.off += ln.len + 1;
220 1.7 joerg if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL || ln.len == 0) {
221 1.7 joerg if (ln.line_no == 0 && matchall)
222 1.7 joerg exit(0);
223 1.7 joerg else
224 1.7 joerg break;
225 1.7 joerg }
226 1.7 joerg if (ln.len > 0 && ln.dat[ln.len - 1] == '\n')
227 1.1 cjep --ln.len;
228 1.1 cjep ln.line_no++;
229 1.1 cjep
230 1.7 joerg /* Return if we need to skip a binary file */
231 1.7 joerg if (f->binary && binbehave == BINFILE_SKIP) {
232 1.7 joerg grep_close(f);
233 1.7 joerg free(ln.file);
234 1.7 joerg free(f);
235 1.7 joerg return (0);
236 1.7 joerg }
237 1.7 joerg /* Process the file line-by-line */
238 1.7 joerg if ((t = procline(&ln, f->binary)) == 0 && Bflag > 0) {
239 1.1 cjep enqueue(&ln);
240 1.1 cjep linesqueued++;
241 1.1 cjep }
242 1.1 cjep c += t;
243 1.7 joerg
244 1.7 joerg /* Count the matches if we have a match limit */
245 1.7 joerg if (mflag) {
246 1.7 joerg mcount -= t;
247 1.7 joerg if (mcount <= 0)
248 1.7 joerg break;
249 1.7 joerg }
250 1.1 cjep }
251 1.1 cjep if (Bflag > 0)
252 1.1 cjep clearqueue();
253 1.1 cjep grep_close(f);
254 1.1 cjep
255 1.1 cjep if (cflag) {
256 1.7 joerg if (!hflag)
257 1.7 joerg printf("%s:", ln.file);
258 1.1 cjep printf("%u\n", c);
259 1.7 joerg }
260 1.7 joerg if (lflag && !qflag && c != 0)
261 1.7 joerg printf("%s\n", fn);
262 1.7 joerg if (Lflag && !qflag && c == 0)
263 1.7 joerg printf("%s\n", fn);
264 1.7 joerg if (c && !cflag && !lflag && !Lflag &&
265 1.7 joerg binbehave == BINFILE_BIN && f->binary && !qflag)
266 1.7 joerg printf(getstr(8), fn);
267 1.7 joerg
268 1.7 joerg free(ln.file);
269 1.7 joerg free(f);
270 1.7 joerg return (c);
271 1.1 cjep }
272 1.1 cjep
273 1.7 joerg #define iswword(x) (iswalnum((x)) || (x) == L'_')
274 1.1 cjep
275 1.1 cjep /*
276 1.7 joerg * Processes a line comparing it with the specified patterns. Each pattern
277 1.7 joerg * is looped to be compared along with the full string, saving each and every
278 1.7 joerg * match, which is necessary to colorize the output and to count the
279 1.7 joerg * matches. The matching lines are passed to printline() to display the
280 1.7 joerg * appropriate output.
281 1.1 cjep */
282 1.7 joerg static inline int
283 1.7 joerg procline(struct str *l, int nottext)
284 1.1 cjep {
285 1.7 joerg regmatch_t matches[MAX_LINE_MATCHES];
286 1.2 dsl regmatch_t pmatch;
287 1.7 joerg size_t st = 0;
288 1.7 joerg unsigned int i;
289 1.7 joerg int c = 0, m = 0, r = 0;
290 1.7 joerg
291 1.7 joerg if (!matchall) {
292 1.7 joerg /* Loop to process the whole line */
293 1.7 joerg while (st <= l->len) {
294 1.7 joerg pmatch.rm_so = st;
295 1.7 joerg pmatch.rm_eo = l->len;
296 1.1 cjep
297 1.7 joerg /* Loop to compare with all the patterns */
298 1.7 joerg for (i = 0; i < patterns; i++) {
299 1.7 joerg /*
300 1.7 joerg * XXX: grep_search() is a workaround for speed up and should be
301 1.7 joerg * removed in the future. See fastgrep.c.
302 1.7 joerg */
303 1.7 joerg if (fg_pattern[i].pattern) {
304 1.7 joerg r = grep_search(&fg_pattern[i],
305 1.7 joerg (unsigned char *)l->dat,
306 1.7 joerg l->len, &pmatch);
307 1.7 joerg r = (r == 0) ? 0 : REG_NOMATCH;
308 1.7 joerg st = pmatch.rm_eo;
309 1.7 joerg } else {
310 1.7 joerg r = regexec(&r_pattern[i], l->dat, 1,
311 1.7 joerg &pmatch, eflags);
312 1.7 joerg r = (r == 0) ? 0 : REG_NOMATCH;
313 1.7 joerg st = pmatch.rm_eo;
314 1.7 joerg }
315 1.7 joerg if (r == REG_NOMATCH)
316 1.7 joerg continue;
317 1.7 joerg /* Check for full match */
318 1.7 joerg if (r == 0 && xflag)
319 1.7 joerg if (pmatch.rm_so != 0 ||
320 1.7 joerg (size_t)pmatch.rm_eo != l->len)
321 1.7 joerg r = REG_NOMATCH;
322 1.7 joerg /* Check for whole word match */
323 1.7 joerg if (r == 0 && wflag && pmatch.rm_so != 0) {
324 1.7 joerg wint_t wbegin, wend;
325 1.7 joerg
326 1.7 joerg wbegin = wend = L' ';
327 1.7 joerg if (pmatch.rm_so != 0 &&
328 1.7 joerg sscanf(&l->dat[pmatch.rm_so - 1],
329 1.7 joerg "%lc", &wbegin) != 1)
330 1.7 joerg r = REG_NOMATCH;
331 1.7 joerg else if ((size_t)pmatch.rm_eo != l->len &&
332 1.7 joerg sscanf(&l->dat[pmatch.rm_eo],
333 1.7 joerg "%lc", &wend) != 1)
334 1.7 joerg r = REG_NOMATCH;
335 1.7 joerg else if (iswword(wbegin) || iswword(wend))
336 1.2 dsl r = REG_NOMATCH;
337 1.2 dsl }
338 1.7 joerg if (r == 0) {
339 1.7 joerg if (m == 0)
340 1.7 joerg c++;
341 1.7 joerg if (m < MAX_LINE_MATCHES)
342 1.7 joerg matches[m++] = pmatch;
343 1.7 joerg /* matches - skip further patterns */
344 1.7 joerg if ((color != NULL && !oflag) || qflag || lflag)
345 1.7 joerg break;
346 1.2 dsl }
347 1.1 cjep }
348 1.7 joerg
349 1.7 joerg if (vflag) {
350 1.7 joerg c = !c;
351 1.2 dsl break;
352 1.1 cjep }
353 1.7 joerg /* One pass if we are not recording matches */
354 1.7 joerg if ((color != NULL && !oflag) || qflag || lflag)
355 1.7 joerg break;
356 1.7 joerg
357 1.7 joerg if (st == (size_t)pmatch.rm_so)
358 1.7 joerg break; /* No matches */
359 1.1 cjep }
360 1.7 joerg } else
361 1.7 joerg c = !vflag;
362 1.2 dsl
363 1.7 joerg if (c && binbehave == BINFILE_BIN && nottext)
364 1.7 joerg return (c); /* Binary file */
365 1.2 dsl
366 1.7 joerg /* Dealing with the context */
367 1.7 joerg if ((tail || c) && !cflag && !qflag && !lflag && !Lflag) {
368 1.1 cjep if (c) {
369 1.7 joerg if (!first && !prev && !tail && Aflag)
370 1.7 joerg printf("--\n");
371 1.1 cjep tail = Aflag;
372 1.7 joerg if (Bflag > 0) {
373 1.7 joerg if (!first && !prev)
374 1.7 joerg printf("--\n");
375 1.1 cjep printqueue();
376 1.7 joerg }
377 1.1 cjep linesqueued = 0;
378 1.7 joerg printline(l, ':', matches, m);
379 1.1 cjep } else {
380 1.7 joerg printline(l, '-', matches, m);
381 1.1 cjep tail--;
382 1.1 cjep }
383 1.7 joerg }
384 1.7 joerg
385 1.7 joerg if (c) {
386 1.7 joerg prev = true;
387 1.7 joerg first = false;
388 1.7 joerg } else
389 1.7 joerg prev = false;
390 1.2 dsl
391 1.7 joerg return (c);
392 1.1 cjep }
393 1.1 cjep
394 1.7 joerg /*
395 1.7 joerg * Safe malloc() for internal use.
396 1.7 joerg */
397 1.1 cjep void *
398 1.1 cjep grep_malloc(size_t size)
399 1.1 cjep {
400 1.2 dsl void *ptr;
401 1.1 cjep
402 1.1 cjep if ((ptr = malloc(size)) == NULL)
403 1.2 dsl err(2, "malloc");
404 1.7 joerg return (ptr);
405 1.1 cjep }
406 1.1 cjep
407 1.7 joerg /*
408 1.7 joerg * Safe calloc() for internal use.
409 1.7 joerg */
410 1.7 joerg void *
411 1.7 joerg grep_calloc(size_t nmemb, size_t size)
412 1.7 joerg {
413 1.7 joerg void *ptr;
414 1.7 joerg
415 1.7 joerg if ((ptr = calloc(nmemb, size)) == NULL)
416 1.7 joerg err(2, "calloc");
417 1.7 joerg return (ptr);
418 1.7 joerg }
419 1.7 joerg
420 1.7 joerg /*
421 1.7 joerg * Safe realloc() for internal use.
422 1.7 joerg */
423 1.1 cjep void *
424 1.1 cjep grep_realloc(void *ptr, size_t size)
425 1.1 cjep {
426 1.7 joerg
427 1.1 cjep if ((ptr = realloc(ptr, size)) == NULL)
428 1.2 dsl err(2, "realloc");
429 1.7 joerg return (ptr);
430 1.7 joerg }
431 1.7 joerg
432 1.7 joerg /*
433 1.7 joerg * Safe strdup() for internal use.
434 1.7 joerg */
435 1.7 joerg char *
436 1.7 joerg grep_strdup(const char *str)
437 1.7 joerg {
438 1.7 joerg char *ret;
439 1.7 joerg
440 1.7 joerg if ((ret = strdup(str)) == NULL)
441 1.7 joerg err(2, "strdup");
442 1.7 joerg return (ret);
443 1.1 cjep }
444 1.1 cjep
445 1.7 joerg /*
446 1.7 joerg * Prints a matching line according to the command line options.
447 1.7 joerg */
448 1.1 cjep void
449 1.7 joerg printline(struct str *line, int sep, regmatch_t *matches, int m)
450 1.1 cjep {
451 1.7 joerg size_t a = 0;
452 1.2 dsl int i, n = 0;
453 1.2 dsl
454 1.7 joerg if (!hflag) {
455 1.7 joerg if (nullflag == 0)
456 1.7 joerg fputs(line->file, stdout);
457 1.7 joerg else {
458 1.7 joerg printf("%s", line->file);
459 1.7 joerg putchar(0);
460 1.7 joerg }
461 1.1 cjep ++n;
462 1.1 cjep }
463 1.1 cjep if (nflag) {
464 1.7 joerg if (n > 0)
465 1.1 cjep putchar(sep);
466 1.1 cjep printf("%d", line->line_no);
467 1.1 cjep ++n;
468 1.1 cjep }
469 1.1 cjep if (bflag) {
470 1.7 joerg if (n > 0)
471 1.1 cjep putchar(sep);
472 1.7 joerg printf("%lld", (long long)line->off);
473 1.7 joerg ++n;
474 1.1 cjep }
475 1.1 cjep if (n)
476 1.1 cjep putchar(sep);
477 1.7 joerg /* --color and -o */
478 1.7 joerg if ((oflag || color) && m > 0) {
479 1.2 dsl for (i = 0; i < m; i++) {
480 1.2 dsl if (!oflag)
481 1.7 joerg fwrite(line->dat + a, matches[i].rm_so - a, 1,
482 1.7 joerg stdout);
483 1.7 joerg if (color)
484 1.7 joerg fprintf(stdout, "\33[%sm\33[K", color);
485 1.7 joerg
486 1.7 joerg fwrite(line->dat + matches[i].rm_so,
487 1.7 joerg matches[i].rm_eo - matches[i].rm_so, 1,
488 1.7 joerg stdout);
489 1.7 joerg if (color)
490 1.7 joerg fprintf(stdout, "\33[m\33[K");
491 1.2 dsl a = matches[i].rm_eo;
492 1.2 dsl if (oflag)
493 1.2 dsl putchar('\n');
494 1.2 dsl }
495 1.2 dsl if (!oflag) {
496 1.2 dsl if (line->len - a > 0)
497 1.2 dsl fwrite(line->dat + a, line->len - a, 1, stdout);
498 1.2 dsl putchar('\n');
499 1.2 dsl }
500 1.2 dsl } else {
501 1.2 dsl fwrite(line->dat, line->len, 1, stdout);
502 1.7 joerg putchar('\n');
503 1.2 dsl }
504 1.1 cjep }
505