Annotation of src/usr.bin/grep/util.c, Revision 1.45
1.45 ! millert 1: /* $OpenBSD: util.c,v 1.44 2012/12/12 11:12:24 millert Exp $ */
1.3 deraadt 2:
1.1 deraadt 3: /*-
4: * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
5: * All rights reserved.
6: *
7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. Redistributions in binary form must reproduce the above copyright
13: * notice, this list of conditions and the following disclaimer in the
14: * documentation and/or other materials provided with the distribution.
15: *
16: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26: * SUCH DAMAGE.
27: */
28:
29: #include <sys/types.h>
30: #include <sys/stat.h>
31:
32: #include <ctype.h>
33: #include <err.h>
34: #include <errno.h>
35: #include <fts.h>
36: #include <regex.h>
37: #include <stdio.h>
38: #include <stdlib.h>
39: #include <string.h>
40: #include <unistd.h>
41: #include <zlib.h>
42:
43: #include "grep.h"
44:
45: /*
46: * Process a file line by line...
47: */
48:
49: static int linesqueued;
1.4 tedu 50: static int procline(str_t *l, int);
1.40 tedu 51: static int grep_search(fastgrep_t *, char *, size_t, regmatch_t *pmatch);
1.39 tedu 52: #ifndef SMALL
1.40 tedu 53: static int grep_cmp(const char *, const char *, size_t);
1.6 tedu 54: static void grep_revstr(unsigned char *, int);
1.39 tedu 55: #endif
1.1 deraadt 56:
1.2 deraadt 57: int
1.1 deraadt 58: grep_tree(char **argv)
59: {
1.10 deraadt 60: FTS *fts;
61: FTSENT *p;
62: int c, fts_flags;
1.1 deraadt 63:
1.37 tedu 64: c = 0;
1.1 deraadt 65:
1.37 tedu 66: fts_flags = FTS_PHYSICAL | FTS_NOSTAT | FTS_NOCHDIR;
1.1 deraadt 67:
1.11 millert 68: if (!(fts = fts_open(argv, fts_flags, NULL)))
1.14 millert 69: err(2, NULL);
1.1 deraadt 70: while ((p = fts_read(fts)) != NULL) {
71: switch (p->fts_info) {
72: case FTS_DNR:
73: break;
74: case FTS_ERR:
1.45 ! millert 75: file_err = 1;
! 76: if(!sflag)
! 77: warnx("%s: %s", p->fts_path, strerror(p->fts_errno));
1.1 deraadt 78: break;
79: case FTS_DP:
80: break;
81: default:
82: c += procfile(p->fts_path);
83: break;
84: }
85: }
1.34 otto 86: if (errno)
87: err(2, "fts_read");
1.1 deraadt 88:
89: return c;
90: }
91:
92: int
93: procfile(char *fn)
94: {
95: str_t ln;
96: file_t *f;
1.4 tedu 97: int c, t, z, nottext;
1.1 deraadt 98:
99: if (fn == NULL) {
100: fn = "(standard input)";
101: f = grep_fdopen(STDIN_FILENO, "r");
102: } else {
103: f = grep_open(fn, "r");
104: }
105: if (f == NULL) {
1.45 ! millert 106: file_err = 1;
1.1 deraadt 107: if (!sflag)
108: warn("%s", fn);
109: return 0;
110: }
1.4 tedu 111:
112: nottext = grep_bin_file(f);
113: if (nottext && binbehave == BIN_FILE_SKIP) {
1.1 deraadt 114: grep_close(f);
115: return 0;
116: }
117:
118: ln.file = fn;
119: ln.line_no = 0;
1.20 espie 120: ln.len = 0;
1.1 deraadt 121: linesqueued = 0;
1.33 jaredy 122: tail = 0;
1.1 deraadt 123: ln.off = -1;
124:
125: if (Bflag > 0)
126: initqueue();
1.27 otto 127: for (c = 0; c == 0 || !(lflag || qflag); ) {
1.1 deraadt 128: ln.off += ln.len + 1;
129: if ((ln.dat = grep_fgetln(f, &ln.len)) == NULL)
130: break;
131: if (ln.len > 0 && ln.dat[ln.len - 1] == '\n')
132: --ln.len;
133: ln.line_no++;
134:
135: z = tail;
1.2 deraadt 136:
1.4 tedu 137: if ((t = procline(&ln, nottext)) == 0 && Bflag > 0 && z == 0) {
1.1 deraadt 138: enqueue(&ln);
139: linesqueued++;
140: }
141: c += t;
142: }
143: if (Bflag > 0)
144: clearqueue();
145: grep_close(f);
146:
147: if (cflag) {
148: if (!hflag)
149: printf("%s:", ln.file);
150: printf("%u\n", c);
151: }
152: if (lflag && c != 0)
153: printf("%s\n", fn);
154: if (Lflag && c == 0)
155: printf("%s\n", fn);
1.4 tedu 156: if (c && !cflag && !lflag && !Lflag &&
1.7 tedu 157: binbehave == BIN_FILE_BIN && nottext && !qflag)
1.4 tedu 158: printf("Binary file %s matches\n", fn);
159:
1.1 deraadt 160: return c;
161: }
162:
163:
164: /*
165: * Process an individual line in a file. Return non-zero if it matches.
166: */
167:
168: #define isword(x) (isalnum(x) || (x) == '_')
169:
170: static int
1.4 tedu 171: procline(str_t *l, int nottext)
1.1 deraadt 172: {
173: regmatch_t pmatch;
1.15 dhartmei 174: int c, i, r;
1.42 aschrijv 175: regoff_t offset;
176:
177: /* size_t will be converted to regoff_t. ssize_t is guaranteed to fit
178: * into regoff_t */
179: if (l->len > SSIZE_MAX) {
180: errx(2, "Line is too big to process");
181: }
1.1 deraadt 182:
1.40 tedu 183: c = 0;
184: i = 0;
1.1 deraadt 185: if (matchall) {
1.41 tedu 186: c = 1;
1.1 deraadt 187: goto print;
188: }
1.2 deraadt 189:
1.40 tedu 190: for (i = 0; i < patterns; i++) {
191: offset = 0;
192: redo:
1.22 millert 193: if (fg_pattern[i].pattern) {
1.40 tedu 194: r = grep_search(&fg_pattern[i], l->dat + offset,
195: l->len - offset, &pmatch);
196: pmatch.rm_so += offset;
197: pmatch.rm_eo += offset;
1.22 millert 198: } else {
1.40 tedu 199: pmatch.rm_so = offset;
1.43 otto 200: pmatch.rm_eo = l->len;
1.9 millert 201: r = regexec(&r_pattern[i], l->dat, 1, &pmatch, eflags);
1.22 millert 202: }
203: if (r == 0 && xflag) {
204: if (pmatch.rm_so != 0 || pmatch.rm_eo != l->len)
205: r = REG_NOMATCH;
1.1 deraadt 206: }
1.15 dhartmei 207: if (r == 0) {
1.40 tedu 208: c = 1;
1.44 millert 209: if (oflag && pmatch.rm_so != pmatch.rm_eo)
1.40 tedu 210: goto print;
1.1 deraadt 211: break;
212: }
213: }
1.40 tedu 214: if (oflag)
215: return c;
216: print:
1.15 dhartmei 217: if (vflag)
218: c = !c;
1.2 deraadt 219:
1.4 tedu 220: if (c && binbehave == BIN_FILE_BIN && nottext)
221: return c; /* Binary file */
222:
1.1 deraadt 223: if ((tail > 0 || c) && !cflag && !qflag) {
224: if (c) {
1.5 deraadt 225: if (first > 0 && tail == 0 && (Bflag < linesqueued) &&
226: (Aflag || Bflag))
1.1 deraadt 227: printf("--\n");
228: first = 1;
229: tail = Aflag;
230: if (Bflag > 0)
231: printqueue();
232: linesqueued = 0;
1.40 tedu 233: printline(l, ':', oflag ? &pmatch : NULL);
1.1 deraadt 234: } else {
1.40 tedu 235: printline(l, '-', oflag ? &pmatch : NULL);
1.1 deraadt 236: tail--;
237: }
238: }
1.40 tedu 239: if (oflag && !matchall) {
240: offset = pmatch.rm_eo;
241: goto redo;
242: }
1.1 deraadt 243: return c;
244: }
245:
1.39 tedu 246: #ifndef SMALL
1.31 otto 247: void
1.25 millert 248: fgrepcomp(fastgrep_t *fg, const char *pattern)
249: {
250: int i;
251:
252: /* Initialize. */
253: fg->patternLen = strlen(pattern);
254: fg->bol = 0;
255: fg->eol = 0;
256: fg->wmatch = wflag;
257: fg->reversedSearch = 0;
258:
259: /*
260: * Make a copy and upper case it for later if in -i mode,
261: * else just copy the pointer.
262: */
263: if (iflag) {
264: fg->pattern = grep_malloc(fg->patternLen + 1);
265: for (i = 0; i < fg->patternLen; i++)
266: fg->pattern[i] = toupper(pattern[i]);
267: fg->pattern[fg->patternLen] = '\0';
268: } else
1.28 deraadt 269: fg->pattern = (unsigned char *)pattern; /* really const */
1.25 millert 270:
271: /* Preprocess pattern. */
272: for (i = 0; i <= UCHAR_MAX; i++)
273: fg->qsBc[i] = fg->patternLen;
274: for (i = 1; i < fg->patternLen; i++) {
275: fg->qsBc[fg->pattern[i]] = fg->patternLen - i;
276: /*
277: * If case is ignored, make the jump apply to both upper and
278: * lower cased characters. As the pattern is stored in upper
279: * case, apply the same to the lower case equivalents.
280: */
281: if (iflag)
282: fg->qsBc[tolower(fg->pattern[i])] = fg->patternLen - i;
283: }
284: }
1.39 tedu 285: #endif
1.25 millert 286:
287: /*
288: * Returns: -1 on failure, 0 on success
289: */
290: int
1.6 tedu 291: fastcomp(fastgrep_t *fg, const char *pattern)
292: {
1.39 tedu 293: #ifdef SMALL
294: return -1;
295: #else
1.6 tedu 296: int i;
297: int bol = 0;
298: int eol = 0;
299: int shiftPatternLen;
300: int hasDot = 0;
301: int firstHalfDot = -1;
302: int firstLastHalfDot = -1;
303: int lastHalfDot = 0;
304:
305: /* Initialize. */
1.28 deraadt 306: fg->patternLen = strlen(pattern);
1.6 tedu 307: fg->bol = 0;
308: fg->eol = 0;
1.22 millert 309: fg->wmatch = 0;
1.6 tedu 310: fg->reversedSearch = 0;
311:
312: /* Remove end-of-line character ('$'). */
1.38 eric 313: if (fg->patternLen > 0 && pattern[fg->patternLen - 1] == '$') {
1.6 tedu 314: eol++;
315: fg->eol = 1;
316: fg->patternLen--;
317: }
318:
319: /* Remove beginning-of-line character ('^'). */
320: if (pattern[0] == '^') {
321: bol++;
322: fg->bol = 1;
323: fg->patternLen--;
324: }
325:
1.22 millert 326: /* Remove enclosing [[:<:]] and [[:>:]] (word match). */
1.30 otto 327: if (wflag) {
328: /* basic re's use \( \), extended re's ( ) */
329: int extra = Eflag ? 1 : 2;
330: fg->patternLen -= 14 + 2 * extra;
331: fg->wmatch = 7 + extra;
332: } else if (fg->patternLen >= 14 &&
1.22 millert 333: strncmp(pattern + fg->bol, "[[:<:]]", 7) == 0 &&
1.24 millert 334: strncmp(pattern + fg->bol + fg->patternLen - 7, "[[:>:]]", 7) == 0) {
1.22 millert 335: fg->patternLen -= 14;
336: fg->wmatch = 7;
337: }
338:
1.6 tedu 339: /*
1.22 millert 340: * Copy pattern minus '^' and '$' characters as well as word
341: * match character classes at the beginning and ending of the
342: * string respectively.
1.6 tedu 343: */
1.22 millert 344: fg->pattern = grep_malloc(fg->patternLen + 1);
345: memcpy(fg->pattern, pattern + bol + fg->wmatch, fg->patternLen);
346: fg->pattern[fg->patternLen] = '\0';
1.6 tedu 347:
348: /* Look for ways to cheat...er...avoid the full regex engine. */
349: for (i = 0; i < fg->patternLen; i++)
350: {
351: /* Can still cheat? */
352: if ((isalnum(fg->pattern[i])) || isspace(fg->pattern[i]) ||
353: (fg->pattern[i] == '_') || (fg->pattern[i] == ',') ||
354: (fg->pattern[i] == '=') || (fg->pattern[i] == '-') ||
355: (fg->pattern[i] == ':') || (fg->pattern[i] == '/')) {
356: /* As long as it is good, upper case it for later. */
357: if (iflag)
358: fg->pattern[i] = toupper(fg->pattern[i]);
359: } else if (fg->pattern[i] == '.') {
360: hasDot = i;
361: if (i < fg->patternLen / 2) {
1.19 otto 362: if (firstHalfDot < 0)
1.6 tedu 363: /* Closest dot to the beginning */
364: firstHalfDot = i;
365: } else {
366: /* Closest dot to the end of the pattern. */
367: lastHalfDot = i;
368: if (firstLastHalfDot < 0)
369: firstLastHalfDot = i;
370: }
371: } else {
372: /* Free memory and let others know this is empty. */
373: free(fg->pattern);
374: fg->pattern = NULL;
375: return (-1);
376: }
377: }
378:
379: /*
380: * Determine if a reverse search would be faster based on the placement
381: * of the dots.
382: */
383: if ((!(lflag || cflag)) && ((!(bol || eol)) &&
384: ((lastHalfDot) && ((firstHalfDot < 0) ||
385: ((fg->patternLen - (lastHalfDot + 1)) < firstHalfDot))))) {
386: fg->reversedSearch = 1;
387: hasDot = fg->patternLen - (firstHalfDot < 0 ?
388: firstLastHalfDot : firstHalfDot) - 1;
389: grep_revstr(fg->pattern, fg->patternLen);
390: }
391:
392: /*
393: * Normal Quick Search would require a shift based on the position the
394: * next character after the comparison is within the pattern. With
395: * wildcards, the position of the last dot effects the maximum shift
396: * distance.
397: * The closer to the end the wild card is the slower the search. A
1.10 deraadt 398: * reverse version of this algorithm would be useful for wildcards near
1.6 tedu 399: * the end of the string.
400: *
401: * Examples:
402: * Pattern Max shift
403: * ------- ---------
404: * this 5
405: * .his 4
406: * t.is 3
407: * th.s 2
408: * thi. 1
409: */
410:
411: /* Adjust the shift based on location of the last dot ('.'). */
412: shiftPatternLen = fg->patternLen - hasDot;
413:
414: /* Preprocess pattern. */
415: for (i = 0; i <= UCHAR_MAX; i++)
416: fg->qsBc[i] = shiftPatternLen;
417: for (i = hasDot + 1; i < fg->patternLen; i++) {
418: fg->qsBc[fg->pattern[i]] = fg->patternLen - i;
419: /*
420: * If case is ignored, make the jump apply to both upper and
421: * lower cased characters. As the pattern is stored in upper
422: * case, apply the same to the lower case equivalents.
423: */
424: if (iflag)
425: fg->qsBc[tolower(fg->pattern[i])] = fg->patternLen - i;
426: }
427:
428: /*
429: * Put pattern back to normal after pre-processing to allow for easy
430: * comparisons later.
431: */
432: if (fg->reversedSearch)
433: grep_revstr(fg->pattern, fg->patternLen);
434:
435: return (0);
1.39 tedu 436: #endif
1.6 tedu 437: }
438:
1.26 otto 439: /*
440: * Word boundaries using regular expressions are defined as the point
441: * of transition from a non-word char to a word char, or vice versa.
442: * This means that grep -w +a and grep -w a+ never match anything,
443: * because they lack a starting or ending transition, but grep -w a+b
444: * does match a line containing a+b.
445: */
1.22 millert 446: #define wmatch(d, l, s, e) \
1.26 otto 447: ((s == 0 || !isword(d[s-1])) && (e == l || !isword(d[e])) && \
448: e > s && isword(d[s]) && isword(d[e-1]))
1.22 millert 449:
1.9 millert 450: static int
1.40 tedu 451: grep_search(fastgrep_t *fg, char *data, size_t dataLen, regmatch_t *pmatch)
1.6 tedu 452: {
1.39 tedu 453: #ifdef SMALL
454: return 0;
455: #else
1.42 aschrijv 456: regoff_t j;
1.6 tedu 457: int rtrnVal = REG_NOMATCH;
458:
1.9 millert 459: pmatch->rm_so = -1;
460: pmatch->rm_eo = -1;
461:
1.6 tedu 462: /* No point in going farther if we do not have enough data. */
463: if (dataLen < fg->patternLen)
464: return (rtrnVal);
465:
466: /* Only try once at the beginning or ending of the line. */
467: if (fg->bol || fg->eol) {
468: /* Simple text comparison. */
469: /* Verify data is >= pattern length before searching on it. */
470: if (dataLen >= fg->patternLen) {
471: /* Determine where in data to start search at. */
472: if (fg->eol)
473: j = dataLen - fg->patternLen;
474: else
475: j = 0;
476: if (!((fg->bol && fg->eol) && (dataLen != fg->patternLen)))
1.22 millert 477: if (grep_cmp(fg->pattern, data + j,
478: fg->patternLen) == -1) {
1.9 millert 479: pmatch->rm_so = j;
480: pmatch->rm_eo = j + fg->patternLen;
1.22 millert 481: if (!fg->wmatch || wmatch(data, dataLen,
482: pmatch->rm_so, pmatch->rm_eo))
483: rtrnVal = 0;
1.9 millert 484: }
1.6 tedu 485: }
486: } else if (fg->reversedSearch) {
487: /* Quick Search algorithm. */
1.17 millert 488: j = dataLen;
489: do {
1.6 tedu 490: if (grep_cmp(fg->pattern, data + j - fg->patternLen,
491: fg->patternLen) == -1) {
1.9 millert 492: pmatch->rm_so = j - fg->patternLen;
493: pmatch->rm_eo = j;
1.22 millert 494: if (!fg->wmatch || wmatch(data, dataLen,
495: pmatch->rm_so, pmatch->rm_eo)) {
496: rtrnVal = 0;
497: break;
498: }
1.6 tedu 499: }
1.17 millert 500: /* Shift if within bounds, otherwise, we are done. */
501: if (j == fg->patternLen)
502: break;
1.40 tedu 503: j -= fg->qsBc[(unsigned char)data[j - fg->patternLen - 1]];
1.17 millert 504: } while (j >= fg->patternLen);
1.6 tedu 505: } else {
506: /* Quick Search algorithm. */
507: j = 0;
508: do {
509: if (grep_cmp(fg->pattern, data + j, fg->patternLen) == -1) {
1.9 millert 510: pmatch->rm_so = j;
511: pmatch->rm_eo = j + fg->patternLen;
1.32 jaredy 512: if (fg->patternLen == 0 || !fg->wmatch ||
513: wmatch(data, dataLen, pmatch->rm_so,
514: pmatch->rm_eo)) {
1.22 millert 515: rtrnVal = 0;
516: break;
517: }
1.6 tedu 518: }
519:
520: /* Shift if within bounds, otherwise, we are done. */
521: if (j + fg->patternLen == dataLen)
522: break;
523: else
1.40 tedu 524: j += fg->qsBc[(unsigned char)data[j + fg->patternLen]];
1.6 tedu 525: } while (j <= (dataLen - fg->patternLen));
526: }
527:
528: return (rtrnVal);
1.39 tedu 529: #endif
1.6 tedu 530: }
531:
532:
1.1 deraadt 533: void *
534: grep_malloc(size_t size)
535: {
1.10 deraadt 536: void *ptr;
1.1 deraadt 537:
538: if ((ptr = malloc(size)) == NULL)
1.14 millert 539: err(2, "malloc");
1.35 deraadt 540: return ptr;
541: }
542:
543: void *
544: grep_calloc(size_t nmemb, size_t size)
545: {
546: void *ptr;
547:
548: if ((ptr = calloc(nmemb, size)) == NULL)
549: err(2, "calloc");
1.1 deraadt 550: return ptr;
551: }
552:
553: void *
554: grep_realloc(void *ptr, size_t size)
555: {
556: if ((ptr = realloc(ptr, size)) == NULL)
1.14 millert 557: err(2, "realloc");
1.6 tedu 558: return ptr;
559: }
560:
1.39 tedu 561: #ifndef SMALL
1.6 tedu 562: /*
563: * Returns: i >= 0 on failure (position that it failed)
564: * -1 on success
565: */
1.18 avsm 566: static int
1.40 tedu 567: grep_cmp(const char *pattern, const char *data, size_t len)
1.6 tedu 568: {
569: int i;
570:
571: for (i = 0; i < len; i++) {
1.25 millert 572: if (((pattern[i] == data[i]) || (!Fflag && pattern[i] == '.'))
573: || (iflag && pattern[i] == toupper(data[i])))
1.6 tedu 574: continue;
575: return (i);
576: }
577:
578: return (-1);
579: }
580:
581: static void
582: grep_revstr(unsigned char *str, int len)
583: {
584: int i;
585: char c;
586:
587: for (i = 0; i < len / 2; i++) {
588: c = str[i];
589: str[i] = str[len - i - 1];
590: str[len - i - 1] = c;
591: }
1.1 deraadt 592: }
1.39 tedu 593: #endif
1.1 deraadt 594:
595: void
1.40 tedu 596: printline(str_t *line, int sep, regmatch_t *pmatch)
1.1 deraadt 597: {
598: int n;
1.2 deraadt 599:
1.1 deraadt 600: n = 0;
601: if (!hflag) {
602: fputs(line->file, stdout);
603: ++n;
604: }
605: if (nflag) {
606: if (n)
607: putchar(sep);
608: printf("%d", line->line_no);
609: ++n;
610: }
611: if (bflag) {
612: if (n)
613: putchar(sep);
1.21 otto 614: printf("%lld", (long long)line->off);
615: ++n;
1.1 deraadt 616: }
617: if (n)
618: putchar(sep);
1.40 tedu 619: if (pmatch)
620: fwrite(line->dat + pmatch->rm_so,
621: pmatch->rm_eo - pmatch->rm_so, 1, stdout);
622: else
623: fwrite(line->dat, line->len, 1, stdout);
1.1 deraadt 624: putchar('\n');
625: }