| 50 |
|
|
| 51 |
#include <sys/types.h> |
#include <sys/types.h> |
| 52 |
#include <sys/stat.h> |
#include <sys/stat.h> |
| 53 |
#include <unistd.h> |
#ifdef HAVE_UNISTD_H |
| 54 |
|
# include <unistd.h> |
| 55 |
|
#endif |
| 56 |
|
|
| 57 |
#include "pcre.h" |
#include <pcre.h> |
| 58 |
|
|
| 59 |
#define FALSE 0 |
#define FALSE 0 |
| 60 |
#define TRUE 1 |
#define TRUE 1 |
| 121 |
static const unsigned char *pcretables = NULL; |
static const unsigned char *pcretables = NULL; |
| 122 |
|
|
| 123 |
static int pattern_count = 0; |
static int pattern_count = 0; |
| 124 |
static pcre **pattern_list; |
static pcre **pattern_list = NULL; |
| 125 |
static pcre_extra **hints_list; |
static pcre_extra **hints_list = NULL; |
| 126 |
|
|
| 127 |
static char *include_pattern = NULL; |
static char *include_pattern = NULL; |
| 128 |
static char *exclude_pattern = NULL; |
static char *exclude_pattern = NULL; |
| 465 |
|
|
| 466 |
|
|
| 467 |
|
|
| 468 |
#if ! HAVE_STRERROR |
#ifndef HAVE_STRERROR |
| 469 |
/************************************************* |
/************************************************* |
| 470 |
* Provide strerror() for non-ANSI libraries * |
* Provide strerror() for non-ANSI libraries * |
| 471 |
*************************************************/ |
*************************************************/ |
| 1408 |
suffix[process_options]); |
suffix[process_options]); |
| 1409 |
pattern_list[pattern_count] = |
pattern_list[pattern_count] = |
| 1410 |
pcre_compile(buffer, options, &error, &errptr, pcretables); |
pcre_compile(buffer, options, &error, &errptr, pcretables); |
| 1411 |
if (pattern_list[pattern_count++] != NULL) return TRUE; |
if (pattern_list[pattern_count] != NULL) |
| 1412 |
|
{ |
| 1413 |
|
pattern_count++; |
| 1414 |
|
return TRUE; |
| 1415 |
|
} |
| 1416 |
|
|
| 1417 |
/* Handle compile errors */ |
/* Handle compile errors */ |
| 1418 |
|
|
| 1494 |
int rc = 1; |
int rc = 1; |
| 1495 |
int pcre_options = 0; |
int pcre_options = 0; |
| 1496 |
int cmd_pattern_count = 0; |
int cmd_pattern_count = 0; |
| 1497 |
|
int hint_count = 0; |
| 1498 |
int errptr; |
int errptr; |
| 1499 |
BOOL only_one_at_top; |
BOOL only_one_at_top; |
| 1500 |
char *patterns[MAX_PATTERN_COUNT]; |
char *patterns[MAX_PATTERN_COUNT]; |
| 1873 |
if (pattern_list == NULL || hints_list == NULL) |
if (pattern_list == NULL || hints_list == NULL) |
| 1874 |
{ |
{ |
| 1875 |
fprintf(stderr, "pcregrep: malloc failed\n"); |
fprintf(stderr, "pcregrep: malloc failed\n"); |
| 1876 |
return 2; |
goto EXIT2; |
| 1877 |
} |
} |
| 1878 |
|
|
| 1879 |
/* If no patterns were provided by -e, and there is no file provided by -f, |
/* If no patterns were provided by -e, and there is no file provided by -f, |
| 1892 |
{ |
{ |
| 1893 |
if (!compile_pattern(patterns[j], pcre_options, NULL, |
if (!compile_pattern(patterns[j], pcre_options, NULL, |
| 1894 |
(j == 0 && cmd_pattern_count == 1)? 0 : j + 1)) |
(j == 0 && cmd_pattern_count == 1)? 0 : j + 1)) |
| 1895 |
return 2; |
goto EXIT2; |
| 1896 |
} |
} |
| 1897 |
|
|
| 1898 |
/* Compile the regular expressions that are provided in a file. */ |
/* Compile the regular expressions that are provided in a file. */ |
| 1916 |
{ |
{ |
| 1917 |
fprintf(stderr, "pcregrep: Failed to open %s: %s\n", pattern_filename, |
fprintf(stderr, "pcregrep: Failed to open %s: %s\n", pattern_filename, |
| 1918 |
strerror(errno)); |
strerror(errno)); |
| 1919 |
return 2; |
goto EXIT2; |
| 1920 |
} |
} |
| 1921 |
filename = pattern_filename; |
filename = pattern_filename; |
| 1922 |
} |
} |
| 1929 |
linenumber++; |
linenumber++; |
| 1930 |
if (buffer[0] == 0) continue; /* Skip blank lines */ |
if (buffer[0] == 0) continue; /* Skip blank lines */ |
| 1931 |
if (!compile_pattern(buffer, pcre_options, filename, linenumber)) |
if (!compile_pattern(buffer, pcre_options, filename, linenumber)) |
| 1932 |
return 2; |
goto EXIT2; |
| 1933 |
} |
} |
| 1934 |
|
|
| 1935 |
if (f != stdin) fclose(f); |
if (f != stdin) fclose(f); |
| 1945 |
char s[16]; |
char s[16]; |
| 1946 |
if (pattern_count == 1) s[0] = 0; else sprintf(s, " number %d", j); |
if (pattern_count == 1) s[0] = 0; else sprintf(s, " number %d", j); |
| 1947 |
fprintf(stderr, "pcregrep: Error while studying regex%s: %s\n", s, error); |
fprintf(stderr, "pcregrep: Error while studying regex%s: %s\n", s, error); |
| 1948 |
return 2; |
goto EXIT2; |
| 1949 |
} |
} |
| 1950 |
|
hint_count++; |
| 1951 |
} |
} |
| 1952 |
|
|
| 1953 |
/* If there are include or exclude patterns, compile them. */ |
/* If there are include or exclude patterns, compile them. */ |
| 1960 |
{ |
{ |
| 1961 |
fprintf(stderr, "pcregrep: Error in 'exclude' regex at offset %d: %s\n", |
fprintf(stderr, "pcregrep: Error in 'exclude' regex at offset %d: %s\n", |
| 1962 |
errptr, error); |
errptr, error); |
| 1963 |
return 2; |
goto EXIT2; |
| 1964 |
} |
} |
| 1965 |
} |
} |
| 1966 |
|
|
| 1972 |
{ |
{ |
| 1973 |
fprintf(stderr, "pcregrep: Error in 'include' regex at offset %d: %s\n", |
fprintf(stderr, "pcregrep: Error in 'include' regex at offset %d: %s\n", |
| 1974 |
errptr, error); |
errptr, error); |
| 1975 |
return 2; |
goto EXIT2; |
| 1976 |
} |
} |
| 1977 |
} |
} |
| 1978 |
|
|
| 1979 |
/* 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. */ |
| 1980 |
|
|
| 1981 |
if (i >= argc) |
if (i >= argc) |
| 1982 |
return pcregrep(stdin, (filenames > FN_DEFAULT)? stdin_name : NULL); |
{ |
| 1983 |
|
rc = pcregrep(stdin, (filenames > FN_DEFAULT)? stdin_name : NULL); |
| 1984 |
|
goto EXIT; |
| 1985 |
|
} |
| 1986 |
|
|
| 1987 |
/* Otherwise, work through the remaining arguments as files or directories. |
/* Otherwise, work through the remaining arguments as files or directories. |
| 1988 |
Pass in the fact that there is only one argument at top level - this suppresses |
Pass in the fact that there is only one argument at top level - this suppresses |
| 1999 |
else if (frc == 0 && rc == 1) rc = 0; |
else if (frc == 0 && rc == 1) rc = 0; |
| 2000 |
} |
} |
| 2001 |
|
|
| 2002 |
|
EXIT: |
| 2003 |
|
if (pattern_list != NULL) |
| 2004 |
|
{ |
| 2005 |
|
for (i = 0; i < pattern_count; i++) free(pattern_list[i]); |
| 2006 |
|
free(pattern_list); |
| 2007 |
|
} |
| 2008 |
|
if (hints_list != NULL) |
| 2009 |
|
{ |
| 2010 |
|
for (i = 0; i < hint_count; i++) free(hints_list[i]); |
| 2011 |
|
free(hints_list); |
| 2012 |
|
} |
| 2013 |
return rc; |
return rc; |
| 2014 |
|
|
| 2015 |
|
EXIT2: |
| 2016 |
|
rc = 2; |
| 2017 |
|
goto EXIT; |
| 2018 |
} |
} |
| 2019 |
|
|
| 2020 |
/* End of pcregrep */ |
/* End of pcregrep */ |