1 /* $OpenBSD: util.c,v 1.35 2007/09/02 15:19:32 deraadt Exp $ */
4 * Copyright (c) 1999 James Howard and Dag-Erling Coïdan Smørgrav
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/types.h>
45 * Process a file line by line...
48 static int linesqueued
;
49 static int procline(str_t
*l
, int);
50 static int grep_search(fastgrep_t
*, unsigned char *, size_t, regmatch_t
*pmatch
);
51 static int grep_cmp(const unsigned char *, const unsigned char *, size_t);
52 static void grep_revstr(unsigned char *, int);
55 grep_tree(char **argv
)
64 fts_flags
= FTS_COMFOLLOW
;
66 fts_flags
= FTS_PHYSICAL
;
68 fts_flags
= FTS_LOGICAL
;
70 fts_flags
|= FTS_NOSTAT
| FTS_NOCHDIR
;
72 if (!(fts
= fts_open(argv
, fts_flags
, NULL
)))
74 while ((p
= fts_read(fts
)) != NULL
) {
75 switch (p
->fts_info
) {
79 errx(2, "%s: %s", p
->fts_path
, strerror(p
->fts_errno
));
85 c
+= procfile(p
->fts_path
);
100 int c
, t
, z
, nottext
;
103 fn
= "(standard input)";
104 f
= grep_fdopen(STDIN_FILENO
, "r");
106 f
= grep_open(fn
, "r");
114 nottext
= grep_bin_file(f
);
115 if (nottext
&& binbehave
== BIN_FILE_SKIP
) {
129 for (c
= 0; c
== 0 || !(lflag
|| qflag
); ) {
130 ln
.off
+= ln
.len
+ 1;
131 if ((ln
.dat
= grep_fgetln(f
, &ln
.len
)) == NULL
)
133 if (ln
.len
> 0 && ln
.dat
[ln
.len
- 1] == '\n')
139 if ((t
= procline(&ln
, nottext
)) == 0 && Bflag
> 0 && z
== 0) {
151 printf("%s:", ln
.file
);
158 if (c
&& !cflag
&& !lflag
&& !Lflag
&&
159 binbehave
== BIN_FILE_BIN
&& nottext
&& !qflag
)
160 printf("Binary file %s matches\n", fn
);
167 * Process an individual line in a file. Return non-zero if it matches.
170 #define isword(x) (isalnum(x) || (x) == '_')
173 procline(str_t
*l
, int nottext
)
183 for (c
= i
= 0; i
< patterns
; i
++) {
184 if (fg_pattern
[i
].pattern
) {
185 r
= grep_search(&fg_pattern
[i
], (unsigned char *)l
->dat
,
189 pmatch
.rm_eo
= l
->len
;
190 r
= regexec(&r_pattern
[i
], l
->dat
, 1, &pmatch
, eflags
);
192 if (r
== 0 && xflag
) {
193 if (pmatch
.rm_so
!= 0 || pmatch
.rm_eo
!= l
->len
)
205 if (c
&& binbehave
== BIN_FILE_BIN
&& nottext
)
206 return c
; /* Binary file */
208 if ((tail
> 0 || c
) && !cflag
&& !qflag
) {
210 if (first
> 0 && tail
== 0 && (Bflag
< linesqueued
) &&
228 fgrepcomp(fastgrep_t
*fg
, const char *pattern
)
233 fg
->patternLen
= strlen(pattern
);
237 fg
->reversedSearch
= 0;
240 * Make a copy and upper case it for later if in -i mode,
241 * else just copy the pointer.
244 fg
->pattern
= grep_malloc(fg
->patternLen
+ 1);
245 for (i
= 0; i
< fg
->patternLen
; i
++)
246 fg
->pattern
[i
] = toupper(pattern
[i
]);
247 fg
->pattern
[fg
->patternLen
] = '\0';
249 fg
->pattern
= (unsigned char *)pattern
; /* really const */
251 /* Preprocess pattern. */
252 for (i
= 0; i
<= UCHAR_MAX
; i
++)
253 fg
->qsBc
[i
] = fg
->patternLen
;
254 for (i
= 1; i
< fg
->patternLen
; i
++) {
255 fg
->qsBc
[fg
->pattern
[i
]] = fg
->patternLen
- i
;
257 * If case is ignored, make the jump apply to both upper and
258 * lower cased characters. As the pattern is stored in upper
259 * case, apply the same to the lower case equivalents.
262 fg
->qsBc
[tolower(fg
->pattern
[i
])] = fg
->patternLen
- i
;
267 * Returns: -1 on failure, 0 on success
270 fastcomp(fastgrep_t
*fg
, const char *pattern
)
277 int firstHalfDot
= -1;
278 int firstLastHalfDot
= -1;
282 fg
->patternLen
= strlen(pattern
);
286 fg
->reversedSearch
= 0;
288 /* Remove end-of-line character ('$'). */
289 if (pattern
[fg
->patternLen
- 1] == '$') {
295 /* Remove beginning-of-line character ('^'). */
296 if (pattern
[0] == '^') {
302 /* Remove enclosing [[:<:]] and [[:>:]] (word match). */
304 /* basic re's use \( \), extended re's ( ) */
305 int extra
= Eflag
? 1 : 2;
306 fg
->patternLen
-= 14 + 2 * extra
;
307 fg
->wmatch
= 7 + extra
;
308 } else if (fg
->patternLen
>= 14 &&
309 strncmp(pattern
+ fg
->bol
, "[[:<:]]", 7) == 0 &&
310 strncmp(pattern
+ fg
->bol
+ fg
->patternLen
- 7, "[[:>:]]", 7) == 0) {
311 fg
->patternLen
-= 14;
316 * Copy pattern minus '^' and '$' characters as well as word
317 * match character classes at the beginning and ending of the
318 * string respectively.
320 fg
->pattern
= grep_malloc(fg
->patternLen
+ 1);
321 memcpy(fg
->pattern
, pattern
+ bol
+ fg
->wmatch
, fg
->patternLen
);
322 fg
->pattern
[fg
->patternLen
] = '\0';
324 /* Look for ways to cheat...er...avoid the full regex engine. */
325 for (i
= 0; i
< fg
->patternLen
; i
++)
327 /* Can still cheat? */
328 if ((isalnum(fg
->pattern
[i
])) || isspace(fg
->pattern
[i
]) ||
329 (fg
->pattern
[i
] == '_') || (fg
->pattern
[i
] == ',') ||
330 (fg
->pattern
[i
] == '=') || (fg
->pattern
[i
] == '-') ||
331 (fg
->pattern
[i
] == ':') || (fg
->pattern
[i
] == '/')) {
332 /* As long as it is good, upper case it for later. */
334 fg
->pattern
[i
] = toupper(fg
->pattern
[i
]);
335 } else if (fg
->pattern
[i
] == '.') {
337 if (i
< fg
->patternLen
/ 2) {
338 if (firstHalfDot
< 0)
339 /* Closest dot to the beginning */
342 /* Closest dot to the end of the pattern. */
344 if (firstLastHalfDot
< 0)
345 firstLastHalfDot
= i
;
348 /* Free memory and let others know this is empty. */
356 * Determine if a reverse search would be faster based on the placement
359 if ((!(lflag
|| cflag
)) && ((!(bol
|| eol
)) &&
360 ((lastHalfDot
) && ((firstHalfDot
< 0) ||
361 ((fg
->patternLen
- (lastHalfDot
+ 1)) < firstHalfDot
))))) {
362 fg
->reversedSearch
= 1;
363 hasDot
= fg
->patternLen
- (firstHalfDot
< 0 ?
364 firstLastHalfDot
: firstHalfDot
) - 1;
365 grep_revstr(fg
->pattern
, fg
->patternLen
);
369 * Normal Quick Search would require a shift based on the position the
370 * next character after the comparison is within the pattern. With
371 * wildcards, the position of the last dot effects the maximum shift
373 * The closer to the end the wild card is the slower the search. A
374 * reverse version of this algorithm would be useful for wildcards near
375 * the end of the string.
387 /* Adjust the shift based on location of the last dot ('.'). */
388 shiftPatternLen
= fg
->patternLen
- hasDot
;
390 /* Preprocess pattern. */
391 for (i
= 0; i
<= UCHAR_MAX
; i
++)
392 fg
->qsBc
[i
] = shiftPatternLen
;
393 for (i
= hasDot
+ 1; i
< fg
->patternLen
; i
++) {
394 fg
->qsBc
[fg
->pattern
[i
]] = fg
->patternLen
- i
;
396 * If case is ignored, make the jump apply to both upper and
397 * lower cased characters. As the pattern is stored in upper
398 * case, apply the same to the lower case equivalents.
401 fg
->qsBc
[tolower(fg
->pattern
[i
])] = fg
->patternLen
- i
;
405 * Put pattern back to normal after pre-processing to allow for easy
408 if (fg
->reversedSearch
)
409 grep_revstr(fg
->pattern
, fg
->patternLen
);
415 * Word boundaries using regular expressions are defined as the point
416 * of transition from a non-word char to a word char, or vice versa.
417 * This means that grep -w +a and grep -w a+ never match anything,
418 * because they lack a starting or ending transition, but grep -w a+b
419 * does match a line containing a+b.
421 #define wmatch(d, l, s, e) \
422 ((s == 0 || !isword(d[s-1])) && (e == l || !isword(d[e])) && \
423 e > s && isword(d[s]) && isword(d[e-1]))
426 grep_search(fastgrep_t
*fg
, unsigned char *data
, size_t dataLen
, regmatch_t
*pmatch
)
429 int rtrnVal
= REG_NOMATCH
;
434 /* No point in going farther if we do not have enough data. */
435 if (dataLen
< fg
->patternLen
)
438 /* Only try once at the beginning or ending of the line. */
439 if (fg
->bol
|| fg
->eol
) {
440 /* Simple text comparison. */
441 /* Verify data is >= pattern length before searching on it. */
442 if (dataLen
>= fg
->patternLen
) {
443 /* Determine where in data to start search at. */
445 j
= dataLen
- fg
->patternLen
;
448 if (!((fg
->bol
&& fg
->eol
) && (dataLen
!= fg
->patternLen
)))
449 if (grep_cmp(fg
->pattern
, data
+ j
,
450 fg
->patternLen
) == -1) {
452 pmatch
->rm_eo
= j
+ fg
->patternLen
;
453 if (!fg
->wmatch
|| wmatch(data
, dataLen
,
454 pmatch
->rm_so
, pmatch
->rm_eo
))
458 } else if (fg
->reversedSearch
) {
459 /* Quick Search algorithm. */
462 if (grep_cmp(fg
->pattern
, data
+ j
- fg
->patternLen
,
463 fg
->patternLen
) == -1) {
464 pmatch
->rm_so
= j
- fg
->patternLen
;
466 if (!fg
->wmatch
|| wmatch(data
, dataLen
,
467 pmatch
->rm_so
, pmatch
->rm_eo
)) {
472 /* Shift if within bounds, otherwise, we are done. */
473 if (j
== fg
->patternLen
)
475 j
-= fg
->qsBc
[data
[j
- fg
->patternLen
- 1]];
476 } while (j
>= fg
->patternLen
);
478 /* Quick Search algorithm. */
481 if (grep_cmp(fg
->pattern
, data
+ j
, fg
->patternLen
) == -1) {
483 pmatch
->rm_eo
= j
+ fg
->patternLen
;
484 if (fg
->patternLen
== 0 || !fg
->wmatch
||
485 wmatch(data
, dataLen
, pmatch
->rm_so
,
492 /* Shift if within bounds, otherwise, we are done. */
493 if (j
+ fg
->patternLen
== dataLen
)
496 j
+= fg
->qsBc
[data
[j
+ fg
->patternLen
]];
497 } while (j
<= (dataLen
- fg
->patternLen
));
505 grep_malloc(size_t size
)
509 if ((ptr
= malloc(size
)) == NULL
)
515 grep_calloc(size_t nmemb
, size_t size
)
519 if ((ptr
= calloc(nmemb
, size
)) == NULL
&& (size
> 0) && (nmemb
> 0))
525 grep_realloc(void *ptr
, size_t size
)
527 if ((ptr
= realloc(ptr
, size
)) == NULL
)
533 * Returns: i >= 0 on failure (position that it failed)
537 grep_cmp(const unsigned char *pattern
, const unsigned char *data
, size_t len
)
541 for (i
= 0; i
< len
; i
++) {
542 if (((pattern
[i
] == data
[i
]) || (!Fflag
&& pattern
[i
] == '.'))
543 || (iflag
&& pattern
[i
] == toupper(data
[i
])))
552 grep_revstr(unsigned char *str
, int len
)
557 for (i
= 0; i
< len
/ 2; i
++) {
559 str
[i
] = str
[len
- i
- 1];
560 str
[len
- i
- 1] = c
;
565 printline(str_t
*line
, int sep
)
571 fputs(line
->file
, stdout
);
577 printf("%d", line
->line_no
);
584 printf("%lld", (long long)line
->off
);
586 printf("%ld", (long)line
->off
);
592 fwrite(line
->dat
, line
->len
, 1, stdout
);