util.c revision 1.1.1.2 1 1.1.1.2 cjep /* $NetBSD: util.c,v 1.1.1.2 2004/01/02 15:00:34 cjep Exp $ */
2 1.1.1.2 cjep
3 1.1 cjep /*-
4 1.1 cjep * Copyright (c) 1999 James Howard and Dag-Erling Codan Smrgrav
5 1.1 cjep * All rights reserved.
6 1.1 cjep *
7 1.1 cjep * Redistribution and use in source and binary forms, with or without
8 1.1 cjep * modification, are permitted provided that the following conditions
9 1.1 cjep * are met:
10 1.1 cjep * 1. Redistributions of source code must retain the above copyright
11 1.1 cjep * notice, this list of conditions and the following disclaimer.
12 1.1 cjep * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cjep * notice, this list of conditions and the following disclaimer in the
14 1.1 cjep * documentation and/or other materials provided with the distribution.
15 1.1 cjep *
16 1.1 cjep * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 cjep * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 cjep * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 cjep * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 cjep * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 cjep * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 cjep * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 cjep * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 cjep * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 cjep * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 cjep * SUCH DAMAGE.
27 1.1 cjep *
28 1.1 cjep */
29 1.1 cjep
30 1.1.1.2 cjep #include <sys/cdefs.h>
31 1.1.1.2 cjep #ifndef lint
32 1.1.1.2 cjep __RCSID("$NetBSD: util.c,v 1.1.1.2 2004/01/02 15:00:34 cjep Exp $");
33 1.1.1.2 cjep #endif /* not lint */
34 1.1.1.2 cjep
35 1.1 cjep #include <sys/types.h>
36 1.1 cjep #include <sys/stat.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.1 cjep #include <fts.h>
42 1.1 cjep #include <regex.h>
43 1.1 cjep #include <stdio.h>
44 1.1 cjep #include <stdlib.h>
45 1.1 cjep #include <string.h>
46 1.1 cjep #include <unistd.h>
47 1.1 cjep #include <zlib.h>
48 1.1 cjep
49 1.1 cjep #include "grep.h"
50 1.1 cjep
51 1.1 cjep /*
52 1.1 cjep * Process a file line by line...
53 1.1 cjep */
54 1.1 cjep
55 1.1.1.2 cjep static int linesqueued, newfile;
56 1.1.1.2 cjep static int procline(str_t *l, int nottext);
57 1.1 cjep
58 1.1 cjep int
59 1.1 cjep grep_tree(char **argv)
60 1.1 cjep {
61 1.1.1.2 cjep FTS *fts;
62 1.1.1.2 cjep FTSENT *p;
63 1.1.1.2 cjep int c, fts_flags;
64 1.1 cjep
65 1.1 cjep c = fts_flags = 0;
66 1.1 cjep
67 1.1.1.2 cjep /* if (linkbehave == LINK_EXPLICIT)
68 1.1 cjep fts_flags = FTS_COMFOLLOW;
69 1.1.1.2 cjep if (linkbehave == LINK_SKIP)
70 1.1 cjep fts_flags = FTS_PHYSICAL;
71 1.1.1.2 cjep if (linkbehave == LINK_FOLLOW)
72 1.1.1.2 cjep fts_flags = FTS_LOGICAL;*/
73 1.1 cjep
74 1.1.1.2 cjep fts_flags |= FTS_NOSTAT | FTS_NOCHDIR | FTS_LOGICAL;
75 1.1 cjep
76 1.1.1.2 cjep if ((fts = fts_open(argv, fts_flags, NULL)) == NULL)
77 1.1.1.2 cjep err(2, NULL);
78 1.1 cjep while ((p = fts_read(fts)) != NULL) {
79 1.1 cjep switch (p->fts_info) {
80 1.1 cjep case FTS_DNR:
81 1.1 cjep break;
82 1.1 cjep case FTS_ERR:
83 1.1.1.2 cjep errx(2, "%s: %s", p->fts_path, strerror(p->fts_errno));
84 1.1 cjep break;
85 1.1 cjep case FTS_DP:
86 1.1.1.2 cjep case FTS_D:
87 1.1.1.2 cjep break;
88 1.1.1.2 cjep case FTS_DC:
89 1.1.1.2 cjep warnx("warning: %s: recursive directory loop\n",
90 1.1.1.2 cjep p->fts_path);
91 1.1 cjep break;
92 1.1 cjep default:
93 1.1 cjep c += procfile(p->fts_path);
94 1.1 cjep break;
95 1.1 cjep }
96 1.1 cjep }
97 1.1 cjep
98 1.1 cjep return c;
99 1.1 cjep }
100 1.1 cjep
101 1.1 cjep int
102 1.1 cjep procfile(char *fn)
103 1.1 cjep {
104 1.1 cjep str_t ln;
105 1.1 cjep file_t *f;
106 1.1.1.2 cjep struct stat sb;
107 1.1.1.2 cjep mode_t s;
108 1.1.1.2 cjep int c, t, z, nottext, skip;
109 1.1.1.2 cjep
110 1.1.1.2 cjep tail = 0;
111 1.1.1.2 cjep newfile = 1;
112 1.1 cjep
113 1.1 cjep if (fn == NULL) {
114 1.1.1.2 cjep fn = stdin_label;
115 1.1 cjep f = grep_fdopen(STDIN_FILENO, "r");
116 1.1 cjep } else {
117 1.1.1.2 cjep skip = 1;
118 1.1.1.2 cjep if (dirbehave == GREP_SKIP || devbehave == GREP_SKIP) {
119 1.1.1.2 cjep if (stat(fn, &sb)) {
120 1.1.1.2 cjep fprintf(stderr, "Cannot stat %s %d\n",
121 1.1.1.2 cjep fn, errno);
122 1.1.1.2 cjep /* XXX record error variable */
123 1.1.1.2 cjep } else {
124 1.1.1.2 cjep s = sb.st_mode & S_IFMT;
125 1.1.1.2 cjep if (s == S_IFDIR && dirbehave == GREP_SKIP)
126 1.1.1.2 cjep skip = 0;
127 1.1.1.2 cjep if ( (s == S_IFIFO || s == S_IFCHR ||
128 1.1.1.2 cjep s == S_IFBLK || s == S_IFSOCK)
129 1.1.1.2 cjep && devbehave == GREP_SKIP)
130 1.1.1.2 cjep skip = 0;
131 1.1.1.2 cjep }
132 1.1.1.2 cjep }
133 1.1.1.2 cjep if (skip == 0)
134 1.1.1.2 cjep return 0;
135 1.1.1.2 cjep
136 1.1 cjep f = grep_open(fn, "r");
137 1.1 cjep }
138 1.1 cjep if (f == NULL) {
139 1.1 cjep if (!sflag)
140 1.1 cjep warn("%s", fn);
141 1.1 cjep return 0;
142 1.1 cjep }
143 1.1.1.2 cjep
144 1.1.1.2 cjep nottext = grep_bin_file(f);
145 1.1.1.2 cjep
146 1.1.1.2 cjep if (nottext && binbehave == BIN_FILE_SKIP) {
147 1.1.1.2 cjep /* Skip this file as it is binary */
148 1.1 cjep grep_close(f);
149 1.1 cjep return 0;
150 1.1 cjep }
151 1.1 cjep
152 1.1 cjep ln.file = fn;
153 1.1 cjep ln.line_no = 0;
154 1.1 cjep linesqueued = 0;
155 1.1 cjep ln.off = -1;
156 1.1 cjep
157 1.1 cjep if (Bflag > 0)
158 1.1 cjep initqueue();
159 1.1 cjep for (c = 0; !(lflag && c);) {
160 1.1 cjep ln.off += ln.len + 1;
161 1.1 cjep if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL)
162 1.1 cjep break;
163 1.1.1.2 cjep if (ln.len > 0 && ln.dat[ln.len - 1] == line_endchar)
164 1.1 cjep --ln.len;
165 1.1 cjep ln.line_no++;
166 1.1 cjep
167 1.1 cjep z = tail;
168 1.1 cjep
169 1.1.1.2 cjep if ((t = procline(&ln, nottext)) == 0 && Bflag > 0 && z == 0) {
170 1.1 cjep enqueue(&ln);
171 1.1 cjep linesqueued++;
172 1.1 cjep }
173 1.1 cjep c += t;
174 1.1.1.2 cjep
175 1.1.1.2 cjep /* If we have a maximum number of matches, stop processing */
176 1.1.1.2 cjep if (mflag && c >= maxcount)
177 1.1.1.2 cjep break;
178 1.1 cjep }
179 1.1 cjep if (Bflag > 0)
180 1.1 cjep clearqueue();
181 1.1 cjep grep_close(f);
182 1.1 cjep
183 1.1 cjep if (cflag) {
184 1.1.1.2 cjep if (output_filenames)
185 1.1.1.2 cjep printf("%s%c", ln.file, fn_colonchar);
186 1.1 cjep printf("%u\n", c);
187 1.1.1.2 cjep }
188 1.1.1.2 cjep
189 1.1 cjep if (lflag && c != 0)
190 1.1.1.2 cjep printf("%s%c", fn, fn_endchar);
191 1.1 cjep if (Lflag && c == 0)
192 1.1.1.2 cjep printf("%s%c", fn, fn_endchar);
193 1.1.1.2 cjep if (c && !cflag && !lflag && !Lflag &&
194 1.1.1.2 cjep binbehave == BIN_FILE_BIN && nottext && !qflag)
195 1.1.1.2 cjep printf("Binary file %s matches\n", fn);
196 1.1.1.2 cjep
197 1.1 cjep return c;
198 1.1 cjep }
199 1.1 cjep
200 1.1 cjep
201 1.1 cjep /*
202 1.1 cjep * Process an individual line in a file. Return non-zero if it matches.
203 1.1 cjep */
204 1.1 cjep
205 1.1 cjep #define isword(x) (isalnum(x) || (x) == '_')
206 1.1 cjep
207 1.1 cjep static int
208 1.1.1.2 cjep procline(str_t *l, int nottext)
209 1.1 cjep {
210 1.1.1.2 cjep regmatch_t pmatch;
211 1.1.1.2 cjep regmatch_t matches[MAX_LINE_MATCHES];
212 1.1.1.2 cjep int c = 0, i, r, t, m = 0;
213 1.1.1.2 cjep regoff_t st = 0;
214 1.1 cjep
215 1.1 cjep if (matchall) {
216 1.1 cjep c = !vflag;
217 1.1 cjep goto print;
218 1.1 cjep }
219 1.1 cjep
220 1.1 cjep t = vflag ? REG_NOMATCH : 0;
221 1.1.1.2 cjep
222 1.1.1.2 cjep while (st <= l->len) {
223 1.1.1.2 cjep pmatch.rm_so = st;
224 1.1.1.2 cjep pmatch.rm_eo = l->len;
225 1.1.1.2 cjep for (i = 0; i < patterns; i++) {
226 1.1.1.2 cjep r = regexec(&r_pattern[i], l->dat, 1, &pmatch, eflags);
227 1.1.1.2 cjep if (r == REG_NOMATCH && t == 0)
228 1.1.1.2 cjep continue;
229 1.1.1.2 cjep if (r == 0) {
230 1.1.1.2 cjep if (wflag) {
231 1.1.1.2 cjep if ((pmatch.rm_so != 0 && isword(l->dat[pmatch.rm_so - 1]))
232 1.1.1.2 cjep || (pmatch.rm_eo != l->len && isword(l->dat[pmatch.rm_eo])))
233 1.1.1.2 cjep r = REG_NOMATCH;
234 1.1.1.2 cjep }
235 1.1.1.2 cjep if (xflag) {
236 1.1.1.2 cjep if (pmatch.rm_so != 0 || pmatch.rm_eo != l->len)
237 1.1.1.2 cjep r = REG_NOMATCH;
238 1.1.1.2 cjep }
239 1.1 cjep }
240 1.1.1.2 cjep if (r == t) {
241 1.1.1.2 cjep if (m == 0)
242 1.1.1.2 cjep c++;
243 1.1.1.2 cjep if (m < MAX_LINE_MATCHES) {
244 1.1.1.2 cjep matches[m] = pmatch;
245 1.1.1.2 cjep m++;
246 1.1.1.2 cjep }
247 1.1.1.2 cjep st = pmatch.rm_eo;
248 1.1.1.2 cjep break;
249 1.1 cjep }
250 1.1 cjep }
251 1.1.1.2 cjep
252 1.1.1.2 cjep /* One pass if we are not recording matches */
253 1.1.1.2 cjep if (!oflag && !colours)
254 1.1 cjep break;
255 1.1.1.2 cjep
256 1.1.1.2 cjep if (st == pmatch.rm_so)
257 1.1.1.2 cjep break; /* No matches */
258 1.1.1.2 cjep
259 1.1 cjep }
260 1.1 cjep
261 1.1 cjep print:
262 1.1.1.2 cjep
263 1.1.1.2 cjep if (c && binbehave == BIN_FILE_BIN && nottext)
264 1.1.1.2 cjep return c; /* Binary file */
265 1.1.1.2 cjep
266 1.1 cjep if ((tail > 0 || c) && !cflag && !qflag) {
267 1.1 cjep if (c) {
268 1.1.1.2 cjep
269 1.1.1.2 cjep if ( (Aflag || Bflag) && first > 0 &&
270 1.1.1.2 cjep ( (Bflag <= linesqueued && tail == 0) || newfile) )
271 1.1.1.2 cjep printf("--\n");
272 1.1.1.2 cjep
273 1.1 cjep first = 1;
274 1.1.1.2 cjep newfile = 0;
275 1.1 cjep tail = Aflag;
276 1.1 cjep if (Bflag > 0)
277 1.1 cjep printqueue();
278 1.1 cjep linesqueued = 0;
279 1.1.1.2 cjep printline(l, fn_colonchar, matches, m);
280 1.1 cjep } else {
281 1.1.1.2 cjep printline(l, fn_dashchar, matches, m);
282 1.1 cjep tail--;
283 1.1 cjep }
284 1.1.1.2 cjep
285 1.1 cjep }
286 1.1 cjep return c;
287 1.1 cjep }
288 1.1 cjep
289 1.1 cjep void *
290 1.1 cjep grep_malloc(size_t size)
291 1.1 cjep {
292 1.1.1.2 cjep void *ptr;
293 1.1 cjep
294 1.1 cjep if ((ptr = malloc(size)) == NULL)
295 1.1.1.2 cjep err(2, "malloc");
296 1.1 cjep return ptr;
297 1.1 cjep }
298 1.1 cjep
299 1.1 cjep void *
300 1.1 cjep grep_realloc(void *ptr, size_t size)
301 1.1 cjep {
302 1.1 cjep if ((ptr = realloc(ptr, size)) == NULL)
303 1.1.1.2 cjep err(2, "realloc");
304 1.1 cjep return ptr;
305 1.1 cjep }
306 1.1 cjep
307 1.1 cjep void
308 1.1.1.2 cjep printline(str_t *line, int sep, regmatch_t *matches, int m)
309 1.1 cjep {
310 1.1.1.2 cjep int i, n = 0;
311 1.1.1.2 cjep size_t a = 0;
312 1.1.1.2 cjep
313 1.1.1.2 cjep if (output_filenames) {
314 1.1 cjep fputs(line->file, stdout);
315 1.1 cjep ++n;
316 1.1 cjep }
317 1.1 cjep if (nflag) {
318 1.1 cjep if (n)
319 1.1 cjep putchar(sep);
320 1.1 cjep printf("%d", line->line_no);
321 1.1 cjep ++n;
322 1.1 cjep }
323 1.1 cjep if (bflag) {
324 1.1 cjep if (n)
325 1.1 cjep putchar(sep);
326 1.1 cjep printf("%lu", (unsigned long)line->off);
327 1.1 cjep }
328 1.1 cjep if (n)
329 1.1 cjep putchar(sep);
330 1.1.1.2 cjep
331 1.1.1.2 cjep if ((oflag || colours) && m > 0) {
332 1.1.1.2 cjep
333 1.1.1.2 cjep for (i = 0; i < m; i++) {
334 1.1.1.2 cjep
335 1.1.1.2 cjep if (!oflag)
336 1.1.1.2 cjep fwrite(line->dat + a, matches[i].rm_so - a, 1, stdout);
337 1.1.1.2 cjep
338 1.1.1.2 cjep if (colours)
339 1.1.1.2 cjep fprintf(stdout, "\33[%sm", grep_colour);
340 1.1.1.2 cjep fwrite(line->dat + matches[i].rm_so,
341 1.1.1.2 cjep matches[i].rm_eo - matches[i].rm_so, 1, stdout);
342 1.1.1.2 cjep
343 1.1.1.2 cjep if (colours)
344 1.1.1.2 cjep fprintf(stdout, "\33[00m");
345 1.1.1.2 cjep a = matches[i].rm_eo;
346 1.1.1.2 cjep if (oflag)
347 1.1.1.2 cjep putchar('\n');
348 1.1.1.2 cjep }
349 1.1.1.2 cjep if (!oflag) {
350 1.1.1.2 cjep if (line->len - a > 0)
351 1.1.1.2 cjep fwrite(line->dat + a, line->len - a, 1, stdout);
352 1.1.1.2 cjep putchar('\n');
353 1.1.1.2 cjep }
354 1.1.1.2 cjep
355 1.1.1.2 cjep
356 1.1.1.2 cjep } else {
357 1.1.1.2 cjep fwrite(line->dat, line->len, 1, stdout);
358 1.1.1.2 cjep putchar(line_endchar);
359 1.1.1.2 cjep }
360 1.1.1.2 cjep
361 1.1 cjep }
362