| 6 |
its pattern matching. On a Unix or Win32 system it can recurse into |
its pattern matching. On a Unix or Win32 system it can recurse into |
| 7 |
directories. |
directories. |
| 8 |
|
|
| 9 |
Copyright (c) 1997-2007 University of Cambridge |
Copyright (c) 1997-2009 University of Cambridge |
| 10 |
|
|
| 11 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
| 12 |
Redistribution and use in source and binary forms, with or without |
Redistribution and use in source and binary forms, with or without |
| 139 |
|
|
| 140 |
static char *include_pattern = NULL; |
static char *include_pattern = NULL; |
| 141 |
static char *exclude_pattern = NULL; |
static char *exclude_pattern = NULL; |
| 142 |
|
static char *include_dir_pattern = NULL; |
| 143 |
|
static char *exclude_dir_pattern = NULL; |
| 144 |
|
|
| 145 |
static pcre *include_compiled = NULL; |
static pcre *include_compiled = NULL; |
| 146 |
static pcre *exclude_compiled = NULL; |
static pcre *exclude_compiled = NULL; |
| 147 |
|
static pcre *include_dir_compiled = NULL; |
| 148 |
|
static pcre *exclude_dir_compiled = NULL; |
| 149 |
|
|
| 150 |
static int after_context = 0; |
static int after_context = 0; |
| 151 |
static int before_context = 0; |
static int before_context = 0; |
| 185 |
/* Options without a single-letter equivalent get a negative value. This can be |
/* Options without a single-letter equivalent get a negative value. This can be |
| 186 |
used to identify them. */ |
used to identify them. */ |
| 187 |
|
|
| 188 |
#define N_COLOUR (-1) |
#define N_COLOUR (-1) |
| 189 |
#define N_EXCLUDE (-2) |
#define N_EXCLUDE (-2) |
| 190 |
#define N_HELP (-3) |
#define N_EXCLUDE_DIR (-3) |
| 191 |
#define N_INCLUDE (-4) |
#define N_HELP (-4) |
| 192 |
#define N_LABEL (-5) |
#define N_INCLUDE (-5) |
| 193 |
#define N_LOCALE (-6) |
#define N_INCLUDE_DIR (-6) |
| 194 |
#define N_NULL (-7) |
#define N_LABEL (-7) |
| 195 |
#define N_LOFFSETS (-8) |
#define N_LOCALE (-8) |
| 196 |
#define N_FOFFSETS (-9) |
#define N_NULL (-9) |
| 197 |
|
#define N_LOFFSETS (-10) |
| 198 |
|
#define N_FOFFSETS (-11) |
| 199 |
|
|
| 200 |
static option_item optionlist[] = { |
static option_item optionlist[] = { |
| 201 |
{ OP_NODATA, N_NULL, NULL, "", " terminate options" }, |
{ OP_NODATA, N_NULL, NULL, "", " terminate options" }, |
| 228 |
{ OP_NODATA, 'r', NULL, "recursive", "recursively scan sub-directories" }, |
{ OP_NODATA, 'r', NULL, "recursive", "recursively scan sub-directories" }, |
| 229 |
{ OP_STRING, N_EXCLUDE,&exclude_pattern, "exclude=pattern","exclude matching files when recursing" }, |
{ OP_STRING, N_EXCLUDE,&exclude_pattern, "exclude=pattern","exclude matching files when recursing" }, |
| 230 |
{ OP_STRING, N_INCLUDE,&include_pattern, "include=pattern","include matching files when recursing" }, |
{ OP_STRING, N_INCLUDE,&include_pattern, "include=pattern","include matching files when recursing" }, |
| 231 |
|
{ OP_STRING, N_EXCLUDE_DIR,&exclude_dir_pattern, "exclude_dir=pattern","exclude matching directories when recursing" }, |
| 232 |
|
{ OP_STRING, N_INCLUDE_DIR,&include_dir_pattern, "include_dir=pattern","include matching directories when recursing" }, |
| 233 |
#ifdef JFRIEDL_DEBUG |
#ifdef JFRIEDL_DEBUG |
| 234 |
{ OP_OP_NUMBER, 'S', &S_arg, "jeffS", "replace matched (sub)string with X" }, |
{ OP_OP_NUMBER, 'S', &S_arg, "jeffS", "replace matched (sub)string with X" }, |
| 235 |
#endif |
#endif |
| 1054 |
if (printname != NULL) fprintf(stdout, "%s:", printname); |
if (printname != NULL) fprintf(stdout, "%s:", printname); |
| 1055 |
if (number) fprintf(stdout, "%d:", linenumber); |
if (number) fprintf(stdout, "%d:", linenumber); |
| 1056 |
if (line_offsets) |
if (line_offsets) |
| 1057 |
fprintf(stdout, "%d,%d", matchptr + offsets[0] - ptr, |
fprintf(stdout, "%d,%d", (int)(matchptr + offsets[0] - ptr), |
| 1058 |
offsets[1] - offsets[0]); |
offsets[1] - offsets[0]); |
| 1059 |
else if (file_offsets) |
else if (file_offsets) |
| 1060 |
fprintf(stdout, "%d,%d", filepos + matchptr + offsets[0] - ptr, |
fprintf(stdout, "%d,%d", (int)(filepos + matchptr + offsets[0] - ptr), |
| 1061 |
offsets[1] - offsets[0]); |
offsets[1] - offsets[0]); |
| 1062 |
else |
else |
| 1063 |
|
{ |
| 1064 |
|
if (do_colour) fprintf(stdout, "%c[%sm", 0x1b, colour_string); |
| 1065 |
fwrite(matchptr + offsets[0], 1, offsets[1] - offsets[0], stdout); |
fwrite(matchptr + offsets[0], 1, offsets[1] - offsets[0], stdout); |
| 1066 |
|
if (do_colour) fprintf(stdout, "%c[00m", 0x1b); |
| 1067 |
|
} |
| 1068 |
fprintf(stdout, "\n"); |
fprintf(stdout, "\n"); |
| 1069 |
matchptr += offsets[1]; |
matchptr += offsets[1]; |
| 1070 |
length -= offsets[1]; |
length -= offsets[1]; |
| 1373 |
} |
} |
| 1374 |
|
|
| 1375 |
/* If the file is a directory, skip if skipping or if we are recursing, scan |
/* If the file is a directory, skip if skipping or if we are recursing, scan |
| 1376 |
each file within it, subject to any include or exclude patterns that were set. |
each file and directory within it, subject to any include or exclude patterns |
| 1377 |
The scanning code is localized so it can be made system-specific. */ |
that were set. The scanning code is localized so it can be made |
| 1378 |
|
system-specific. */ |
| 1379 |
|
|
| 1380 |
if ((sep = isdirectory(pathname)) != 0) |
if ((sep = isdirectory(pathname)) != 0) |
| 1381 |
{ |
{ |
| 1396 |
|
|
| 1397 |
while ((nextfile = readdirectory(dir)) != NULL) |
while ((nextfile = readdirectory(dir)) != NULL) |
| 1398 |
{ |
{ |
| 1399 |
int frc, blen; |
int frc, nflen; |
| 1400 |
sprintf(buffer, "%.512s%c%.128s", pathname, sep, nextfile); |
sprintf(buffer, "%.512s%c%.128s", pathname, sep, nextfile); |
| 1401 |
blen = strlen(buffer); |
nflen = strlen(nextfile); |
| 1402 |
|
|
| 1403 |
|
if (isdirectory(buffer)) |
| 1404 |
|
{ |
| 1405 |
|
if (exclude_dir_compiled != NULL && |
| 1406 |
|
pcre_exec(exclude_dir_compiled, NULL, nextfile, nflen, 0, 0, NULL, 0) >= 0) |
| 1407 |
|
continue; |
| 1408 |
|
|
| 1409 |
|
if (include_dir_compiled != NULL && |
| 1410 |
|
pcre_exec(include_dir_compiled, NULL, nextfile, nflen, 0, 0, NULL, 0) < 0) |
| 1411 |
|
continue; |
| 1412 |
|
} |
| 1413 |
|
else |
| 1414 |
|
{ |
| 1415 |
|
if (exclude_compiled != NULL && |
| 1416 |
|
pcre_exec(exclude_compiled, NULL, nextfile, nflen, 0, 0, NULL, 0) >= 0) |
| 1417 |
|
continue; |
| 1418 |
|
|
| 1419 |
if (exclude_compiled != NULL && |
if (include_compiled != NULL && |
| 1420 |
pcre_exec(exclude_compiled, NULL, buffer, blen, 0, 0, NULL, 0) >= 0) |
pcre_exec(include_compiled, NULL, nextfile, nflen, 0, 0, NULL, 0) < 0) |
| 1421 |
continue; |
continue; |
| 1422 |
|
} |
|
if (include_compiled != NULL && |
|
|
pcre_exec(include_compiled, NULL, buffer, blen, 0, 0, NULL, 0) < 0) |
|
|
continue; |
|
| 1423 |
|
|
| 1424 |
frc = grep_or_recurse(buffer, dir_recurse, FALSE); |
frc = grep_or_recurse(buffer, dir_recurse, FALSE); |
| 1425 |
if (frc > 1) rc = frc; |
if (frc > 1) rc = frc; |
| 1605 |
int n; |
int n; |
| 1606 |
char s[4]; |
char s[4]; |
| 1607 |
if (op->one_char > 0) sprintf(s, "-%c,", op->one_char); else strcpy(s, " "); |
if (op->one_char > 0) sprintf(s, "-%c,", op->one_char); else strcpy(s, " "); |
| 1608 |
printf(" %s --%s%n", s, op->long_name, &n); |
n = 30 - printf(" %s --%s", s, op->long_name); |
|
n = 30 - n; |
|
| 1609 |
if (n < 1) n = 1; |
if (n < 1) n = 1; |
| 1610 |
printf("%.*s%s\n", n, " ", op->help_text); |
printf("%.*s%s\n", n, " ", op->help_text); |
| 1611 |
} |
} |
| 2315 |
} |
} |
| 2316 |
} |
} |
| 2317 |
|
|
| 2318 |
|
if (exclude_dir_pattern != NULL) |
| 2319 |
|
{ |
| 2320 |
|
exclude_dir_compiled = pcre_compile(exclude_dir_pattern, 0, &error, &errptr, |
| 2321 |
|
pcretables); |
| 2322 |
|
if (exclude_dir_compiled == NULL) |
| 2323 |
|
{ |
| 2324 |
|
fprintf(stderr, "pcregrep: Error in 'exclude_dir' regex at offset %d: %s\n", |
| 2325 |
|
errptr, error); |
| 2326 |
|
goto EXIT2; |
| 2327 |
|
} |
| 2328 |
|
} |
| 2329 |
|
|
| 2330 |
|
if (include_dir_pattern != NULL) |
| 2331 |
|
{ |
| 2332 |
|
include_dir_compiled = pcre_compile(include_dir_pattern, 0, &error, &errptr, |
| 2333 |
|
pcretables); |
| 2334 |
|
if (include_dir_compiled == NULL) |
| 2335 |
|
{ |
| 2336 |
|
fprintf(stderr, "pcregrep: Error in 'include_dir' regex at offset %d: %s\n", |
| 2337 |
|
errptr, error); |
| 2338 |
|
goto EXIT2; |
| 2339 |
|
} |
| 2340 |
|
} |
| 2341 |
|
|
| 2342 |
/* If there are no further arguments, do the business on stdin and exit. */ |
/* If there are no further arguments, do the business on stdin and exit. */ |
| 2343 |
|
|
| 2344 |
if (i >= argc) |
if (i >= argc) |