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