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