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