version 1.39, 2010/07/02 22:18:03 |
version 1.40, 2011/07/08 01:20:24 |
|
|
|
|
static int linesqueued; |
static int linesqueued; |
static int procline(str_t *l, int); |
static int procline(str_t *l, int); |
static int grep_search(fastgrep_t *, unsigned char *, size_t, regmatch_t *pmatch); |
static int grep_search(fastgrep_t *, char *, size_t, regmatch_t *pmatch); |
#ifndef SMALL |
#ifndef SMALL |
static int grep_cmp(const unsigned char *, const unsigned char *, size_t); |
static int grep_cmp(const char *, const char *, size_t); |
static void grep_revstr(unsigned char *, int); |
static void grep_revstr(unsigned char *, int); |
#endif |
#endif |
|
|
|
|
{ |
{ |
regmatch_t pmatch; |
regmatch_t pmatch; |
int c, i, r; |
int c, i, r; |
|
int offset; |
|
|
|
c = 0; |
|
i = 0; |
if (matchall) { |
if (matchall) { |
c = !vflag; |
|
goto print; |
goto print; |
} |
} |
|
|
for (c = i = 0; i < patterns; i++) { |
for (i = 0; i < patterns; i++) { |
|
offset = 0; |
|
redo: |
if (fg_pattern[i].pattern) { |
if (fg_pattern[i].pattern) { |
r = grep_search(&fg_pattern[i], (unsigned char *)l->dat, |
r = grep_search(&fg_pattern[i], l->dat + offset, |
l->len, &pmatch); |
l->len - offset, &pmatch); |
|
pmatch.rm_so += offset; |
|
pmatch.rm_eo += offset; |
} else { |
} else { |
pmatch.rm_so = 0; |
pmatch.rm_so = offset; |
pmatch.rm_eo = l->len; |
pmatch.rm_eo = l->len - offset; |
r = regexec(&r_pattern[i], l->dat, 1, &pmatch, eflags); |
r = regexec(&r_pattern[i], l->dat, 1, &pmatch, eflags); |
} |
} |
if (r == 0 && xflag) { |
if (r == 0 && xflag) { |
|
|
r = REG_NOMATCH; |
r = REG_NOMATCH; |
} |
} |
if (r == 0) { |
if (r == 0) { |
c++; |
c = 1; |
|
if (oflag) |
|
goto print; |
break; |
break; |
} |
} |
} |
} |
|
if (oflag) |
|
return c; |
|
print: |
if (vflag) |
if (vflag) |
c = !c; |
c = !c; |
|
|
print: |
|
if (c && binbehave == BIN_FILE_BIN && nottext) |
if (c && binbehave == BIN_FILE_BIN && nottext) |
return c; /* Binary file */ |
return c; /* Binary file */ |
|
|
|
|
if (Bflag > 0) |
if (Bflag > 0) |
printqueue(); |
printqueue(); |
linesqueued = 0; |
linesqueued = 0; |
printline(l, ':'); |
printline(l, ':', oflag ? &pmatch : NULL); |
} else { |
} else { |
printline(l, '-'); |
printline(l, '-', oflag ? &pmatch : NULL); |
tail--; |
tail--; |
} |
} |
} |
} |
|
if (oflag && !matchall) { |
|
offset = pmatch.rm_eo; |
|
goto redo; |
|
} |
return c; |
return c; |
} |
} |
|
|
|
|
e > s && isword(d[s]) && isword(d[e-1])) |
e > s && isword(d[s]) && isword(d[e-1])) |
|
|
static int |
static int |
grep_search(fastgrep_t *fg, unsigned char *data, size_t dataLen, regmatch_t *pmatch) |
grep_search(fastgrep_t *fg, char *data, size_t dataLen, regmatch_t *pmatch) |
{ |
{ |
#ifdef SMALL |
#ifdef SMALL |
return 0; |
return 0; |
|
|
/* Shift if within bounds, otherwise, we are done. */ |
/* Shift if within bounds, otherwise, we are done. */ |
if (j == fg->patternLen) |
if (j == fg->patternLen) |
break; |
break; |
j -= fg->qsBc[data[j - fg->patternLen - 1]]; |
j -= fg->qsBc[(unsigned char)data[j - fg->patternLen - 1]]; |
} while (j >= fg->patternLen); |
} while (j >= fg->patternLen); |
} else { |
} else { |
/* Quick Search algorithm. */ |
/* Quick Search algorithm. */ |
|
|
if (j + fg->patternLen == dataLen) |
if (j + fg->patternLen == dataLen) |
break; |
break; |
else |
else |
j += fg->qsBc[data[j + fg->patternLen]]; |
j += fg->qsBc[(unsigned char)data[j + fg->patternLen]]; |
} while (j <= (dataLen - fg->patternLen)); |
} while (j <= (dataLen - fg->patternLen)); |
} |
} |
|
|
|
|
* -1 on success |
* -1 on success |
*/ |
*/ |
static int |
static int |
grep_cmp(const unsigned char *pattern, const unsigned char *data, size_t len) |
grep_cmp(const char *pattern, const char *data, size_t len) |
{ |
{ |
int i; |
int i; |
|
|
|
|
#endif |
#endif |
|
|
void |
void |
printline(str_t *line, int sep) |
printline(str_t *line, int sep, regmatch_t *pmatch) |
{ |
{ |
int n; |
int n; |
|
|
|
|
} |
} |
if (n) |
if (n) |
putchar(sep); |
putchar(sep); |
fwrite(line->dat, line->len, 1, stdout); |
if (pmatch) |
|
fwrite(line->dat + pmatch->rm_so, |
|
pmatch->rm_eo - pmatch->rm_so, 1, stdout); |
|
else |
|
fwrite(line->dat, line->len, 1, stdout); |
putchar('\n'); |
putchar('\n'); |
} |
} |