57 #include <sys/types.h>
79 #define OFFSET_SIZE 99
82 #define MAXPATLEN BUFSIZ
84 #define MAXPATLEN 8192
87 #define PATBUFSIZE (MAXPATLEN + 10)
106 #define PO_WORD_MATCH 0x0001
107 #define PO_LINE_MATCH 0x0002
108 #define PO_FIXED_STRINGS 0x0004
124 #define FWRITE(a,b,c,d) if (fwrite(a,b,c,d)) {}
136 static int S_arg = -1;
137 static unsigned int jfriedl_XR = 0;
138 static unsigned int jfriedl_XT = 0;
139 static const char *jfriedl_prefix =
"";
140 static const char *jfriedl_postfix =
"";
164 #if defined HAVE_WINDOWS_H && HAVE_WINDOWS_H
176 #ifdef SUPPORT_PCREGREP_JIT
287 static const char *
incexname[4] = {
"--include",
"--exclude",
288 "--include-dir",
"--exclude-dir" };
306 #define N_COLOUR (-1)
307 #define N_EXCLUDE (-2)
308 #define N_EXCLUDE_DIR (-3)
310 #define N_INCLUDE (-5)
311 #define N_INCLUDE_DIR (-6)
313 #define N_LOCALE (-8)
315 #define N_LOFFSETS (-10)
316 #define N_FOFFSETS (-11)
317 #define N_LBUFFER (-12)
318 #define N_M_LIMIT (-13)
319 #define N_M_LIMIT_REC (-14)
320 #define N_BUFSIZE (-15)
321 #define N_NOJIT (-16)
322 #define N_FILE_LIST (-17)
323 #define N_BINARY_FILES (-18)
324 #define N_EXCLUDE_FROM (-19)
325 #define N_INCLUDE_FROM (-20)
326 #define N_OM_SEPARATOR (-21)
332 {
OP_NODATA,
'a',
NULL,
"text",
"treat binary files as text" },
339 {
OP_NODATA,
'c',
NULL,
"count",
"print only a count of matching lines per FILE" },
340 {
OP_STRING,
'D', &
DEE_option,
"devices=action",
"how to handle devices, FIFOs, and sockets" },
343 {
OP_NODATA,
'F',
NULL,
"fixed-strings",
"patterns are sets of newline-separated strings" },
347 {
OP_NODATA,
'H',
NULL,
"with-filename",
"force the prefixing filename on output" },
348 {
OP_NODATA,
'h',
NULL,
"no-filename",
"suppress the prefixing filename on output" },
349 {
OP_NODATA,
'I',
NULL,
"",
"treat binary files as not matching (ignore)" },
350 {
OP_NODATA,
'i',
NULL,
"ignore-case",
"ignore case distinctions" },
351 #ifdef SUPPORT_PCREGREP_JIT
356 {
OP_NODATA,
'l',
NULL,
"files-with-matches",
"print only FILE names containing matches" },
357 {
OP_NODATA,
'L',
NULL,
"files-without-match",
"print only FILE names not containing matches" },
364 {
OP_NODATA,
'M',
NULL,
"multiline",
"run in multiline mode" },
365 {
OP_STRING,
'N', &
newline,
"newline=type",
"set newline type (CR, LF, CRLF, ANYCRLF or ANY)" },
366 {
OP_NODATA,
'n',
NULL,
"line-number",
"print line number with output lines" },
369 {
OP_NODATA,
'q',
NULL,
"quiet",
"suppress output, just set return code" },
370 {
OP_NODATA,
'r',
NULL,
"recursive",
"recursively scan sub-directories" },
387 {
OP_OP_NUMBER,
'S', &S_arg,
"jeffS",
"replace matched (sub)string with X" },
389 {
OP_NODATA,
's',
NULL,
"no-messages",
"suppress error messages" },
391 {
OP_NODATA,
'V',
NULL,
"version",
"print version information and exit" },
392 {
OP_NODATA,
'v',
NULL,
"invert-match",
"select non-matching lines" },
393 {
OP_NODATA,
'w',
NULL,
"word-regex(p)",
"force patterns to match only as words" },
394 {
OP_NODATA,
'x',
NULL,
"line-regex(p)",
"force patterns to match only whole lines" },
406 "",
"\\b",
"^(?:",
"^(?:",
"\\Q",
"\\b\\Q",
"^(?:\\Q",
"^(?:\\Q" };
409 "",
"\\b",
")$",
")$",
"\\E",
"\\E\\b",
"\\E)$",
"\\E)$" };
416 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
417 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
418 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
419 3,3,3,3,3,3,3,3,4,4,4,4,5,5,5,5 };
438 fprintf(stderr,
"pcregrep: Error %d, %d or %d means that a resource limit "
441 fprintf(stderr,
"pcregrep: Check your regex for nested unlimited loops.\n");
467 fprintf(stderr,
"pcregrep: malloc failed\n");
472 fprintf(stderr,
"pcregrep: pattern is too long (limit is %d bytes)\n",
547 #if (defined HAVE_SYS_STAT_H && defined HAVE_DIRENT_H && defined HAVE_SYS_TYPES_H) || defined NATIVE_ZOS
548 #include <sys/types.h>
549 #include <sys/stat.h>
552 #if defined NATIVE_ZOS
556 #include "pcrzosfs.h"
570 if (stat(filename, &statbuf) < 0)
572 return (statbuf.st_mode & S_IFMT) == S_IFDIR;
578 return opendir(filename);
586 struct dirent *dent = readdir(dir);
588 if (
strcmp(dent->d_name,
".") != 0 &&
strcmp(dent->d_name,
"..") != 0)
607 if (stat(filename, &statbuf) < 0)
609 return (statbuf.st_mode & S_IFMT) == S_IFREG;
613 #if defined NATIVE_ZOS
636 return isatty(fileno(stdout));
642 return isatty(fileno(
f));
658 #elif defined HAVE_WINDOWS_H && HAVE_WINDOWS_H
663 #ifndef WIN32_LEAN_AND_MEAN
664 # define WIN32_LEAN_AND_MEAN
669 #ifndef INVALID_FILE_ATTRIBUTES
670 #define INVALID_FILE_ATTRIBUTES 0xFFFFFFFF
677 WIN32_FIND_DATA data;
685 DWORD attr = GetFileAttributes(filename);
686 if (attr == INVALID_FILE_ATTRIBUTES)
688 return (attr & FILE_ATTRIBUTE_DIRECTORY) != 0;
698 len = strlen(filename);
701 if ((pattern ==
NULL) || (dir ==
NULL))
703 fprintf(stderr,
"pcregrep: malloc failed\n");
706 memcpy(pattern, filename,
len);
707 memcpy(&(pattern[
len]),
"\\*", 3);
708 dir->handle = FindFirstFile(pattern, &(dir->data));
715 err = GetLastError();
718 errno = (err == ERROR_ACCESS_DENIED) ? EACCES : ENOENT;
729 if (!FindNextFile(dir->handle, &(dir->data)))
736 if (
strcmp(dir->data.cFileName,
".") != 0 &&
strcmp(dir->data.cFileName,
"..") != 0)
737 return dir->data.cFileName;
747 FindClose(dir->handle);
822 #ifndef HAVE_STRERROR
837 if (n < 0 || n >=
sys_nerr)
return "unknown error number";
852 fprintf(stderr,
"Usage: pcregrep [-");
857 fprintf(stderr,
"] [long options] [pattern] [files]\n");
858 fprintf(stderr,
"Type `pcregrep --help' for more information and the long "
874 printf(
"Usage: pcregrep [OPTION]... [PATTERN] [FILE1 FILE2 ...]\n");
875 printf(
"Search for PATTERN in each FILE or standard input.\n");
876 printf(
"PATTERN must be present if neither -e nor -f is used.\n");
877 printf(
"\"-\" can be used as a file name to mean STDIN.\n");
880 printf(
"Files whose names end in .gz are read using zlib.\n");
883 #ifdef SUPPORT_LIBBZ2
884 printf(
"Files whose names end in .bz2 are read using bzlib2.\n");
887 #if defined SUPPORT_LIBZ || defined SUPPORT_LIBBZ2
888 printf(
"Other files and the standard input are read as plain files.\n\n");
890 printf(
"All files are read as plain files, without any interpretation.\n\n");
893 printf(
"Example: pcregrep -i 'hello.*world' menu.h main.c\n\n");
894 printf(
"Options:\n");
917 n = 31 - printf(
" %s --%s", s, op->
long_name);
924 printf(
"\nNumbers may be followed by K or M, e.g. --buffer-size=100K.\n");
926 printf(
"When reading patterns or file names from a file, trailing white\n");
927 printf(
"space is removed and blank lines are ignored.\n");
928 printf(
"The maximum size of any pattern is %d bytes.\n",
MAXPATLEN);
930 printf(
"\nWith no FILEs, read standard input. If fewer than two FILEs given, assume -h.\n");
931 printf(
"Exit status is 0 if any matches, 1 if no matches, and 2 if trouble.\n");
954 int plen = strlen(path);
994 unsigned long int n = 0;
995 char *endptr = option_data;
996 while (*endptr != 0 &&
isspace((
unsigned char)(*endptr))) endptr++;
997 while (
isdigit((
unsigned char)(*endptr)))
998 n =
n * 10 + (
int)(*endptr++ -
'0');
1004 else if (
toupper(*endptr) ==
'M')
1014 char *equals = strchr(op->
long_name,
'=');
1017 fprintf(stderr,
"pcregrep: Malformed number \"%s\" after --%.*s\n",
1021 fprintf(stderr,
"pcregrep: Malformed number \"%s\" after -%c\n",
1052 fprintf(stderr,
"pcregrep: malloc failed\n");
1092 while ((c = fgetc(
f)) != EOF)
1095 if (c ==
'\n' || yield >= length)
break;
1125 while (p < endptr && *p !=
'\n') p++;
1135 while (p < endptr && *p !=
'\r') p++;
1147 while (p < endptr && *p !=
'\r') p++;
1165 register int c = *((
unsigned char *)p);
1167 if (
utf8 && c >= 0xc0)
1173 for (gcii = 1; gcii <= extra; gcii++)
1176 c |= (p[gcii] & 0x3f) << gcss;
1189 if (p < endptr && *p ==
'\n')
1209 register int c = *((
unsigned char *)p);
1211 if (
utf8 && c >= 0xc0)
1217 for (gcii = 1; gcii <= extra; gcii++)
1220 c |= (p[gcii] & 0x3f) << gcss;
1235 if (p < endptr && *p ==
'\n')
1245 *lenptr =
utf8? 2 : 1;
1287 while (p > startptr && p[-1] !=
'\n') p--;
1292 while (p > startptr && p[-1] !=
'\n') p--;
1299 while (p > startptr && p[-1] !=
'\n') p--;
1300 if (p <= startptr + 1 || p[-2] ==
'\r')
return p;
1306 if (*(--p) ==
'\n' && p > startptr && p[-1] ==
'\r') p--;
1307 if (
utf8)
while ((*p & 0xc0) == 0x80) p--;
1309 while (p > startptr)
1311 register unsigned int c;
1317 while ((*pp & 0xc0) == 0x80) pp--;
1318 c = *((
unsigned char *)pp);
1325 for (gcii = 1; gcii <= extra; gcii++)
1328 c |= (pp[gcii] & 0x3f) << gcss;
1332 else c = *((
unsigned char *)pp);
1391 char *endptr,
char *printname)
1396 while (lastmatchrestart < endptr && count++ <
after_context)
1399 char *pp = lastmatchrestart;
1400 if (printname !=
NULL) fprintf(stdout,
"%s-", printname);
1401 if (
number) fprintf(stdout,
"%lu-", lastmatchnumber++);
1403 FWRITE(lastmatchrestart, 1, pp - lastmatchrestart, stdout);
1404 lastmatchrestart = pp;
1435 int startoffset,
int *offsets,
int *mrc)
1438 size_t slen = length;
1440 const char *msg =
"this text:\n\n";
1445 msg =
"text that starts:\n\n";
1451 if (*mrc >= 0)
return TRUE;
1453 fprintf(stderr,
"pcregrep: pcre_exec() gave error %d while matching ", *mrc);
1455 fprintf(stderr,
"%s", msg);
1456 FWRITE(matchptr, 1, slen, stderr);
1457 fprintf(stderr,
"\n\n");
1463 fprintf(stderr,
"pcregrep: Too many errors - abandoned.\n");
1502 pcregrep(
void *handle,
int frtype,
char *filename,
char *printname)
1507 unsigned long int linenumber = 1;
1508 unsigned long int lastmatchnumber = 0;
1509 unsigned long int count = 0;
1510 char *lastmatchrestart =
NULL;
1523 #ifdef SUPPORT_LIBBZ2
1544 #ifdef SUPPORT_LIBBZ2
1547 inbz2 = (
BZFILE *)handle;
1549 if ((
int)bufflength < 0)
return 2;
1555 in = (FILE *)handle;
1557 bufflength = input_line_buffered?
1571 memchr(
main_buffer, 0, (bufflength > 1024)? 1024 : bufflength) !=
NULL;
1580 while (ptr < endptr)
1584 int startoffset = 0;
1586 unsigned int options = 0;
1588 char *matchptr = ptr;
1590 size_t length, linelength;
1592 prevoffsets[0] = prevoffsets[1] = -1;
1603 linelength =
t - ptr - endlinelength;
1604 length =
multiline? (size_t)(endptr - ptr) : linelength;
1612 fprintf(stderr,
"pcregrep: line %lu%s%s is too long for the internal buffer\n"
1613 "pcregrep: check the --buffer-size option\n",
1615 (filename ==
NULL)?
"" :
" of file ",
1616 (filename ==
NULL)?
"" : filename);
1622 #ifdef JFRIEDL_DEBUG
1623 if (jfriedl_XT || jfriedl_XR)
1625 # include <sys/time.h>
1628 struct timezone
dummy;
1633 unsigned long newlen = length * jfriedl_XT + strlen(jfriedl_prefix) + strlen(jfriedl_postfix);
1634 const char *
orig = ptr;
1635 ptr =
malloc(newlen + 1);
1637 printf(
"out of memory");
1641 strcpy(endptr, jfriedl_prefix); endptr += strlen(jfriedl_prefix);
1642 for (
i = 0;
i < jfriedl_XT;
i++) {
1643 strncpy(endptr,
orig, length);
1646 strcpy(endptr, jfriedl_postfix); endptr += strlen(jfriedl_postfix);
1651 perror(
"bad gettimeofday");
1654 for (
i = 0;
i < jfriedl_XR;
i++)
1658 if (gettimeofday(&end_time, &
dummy) != 0)
1659 perror(
"bad gettimeofday");
1661 double delta = ((end_time.tv_sec + (end_time.tv_usec / 1000000.0))
1665 printf(
"%s TIMER[%.4f]\n",
match ?
"MATCH" :
"FAIL",
delta);
1674 ONLY_MATCHING_RESTART:
1697 if (
quiet)
return 0;
1709 fprintf(stdout,
"Binary file %s matches\n", filename);
1718 fprintf(stdout,
"%s\n", printname);
1735 int oldstartoffset = startoffset;
1744 if (prevoffsets[0] != offsets[0] || prevoffsets[1] != offsets[1])
1746 prevoffsets[0] = offsets[0];
1747 prevoffsets[1] = offsets[1];
1749 if (printname !=
NULL) fprintf(stdout,
"%s:", printname);
1750 if (
number) fprintf(stdout,
"%lu:", linenumber);
1755 fprintf(stdout,
"%d,%d\n", (
int)(matchptr + offsets[0] - ptr),
1756 offsets[1] - offsets[0]);
1761 fprintf(stdout,
"%d,%d\n",
1762 (
int)(filepos + matchptr + offsets[0] - ptr),
1763 offsets[1] - offsets[0]);
1774 int n =
om->groupnum;
1777 int plen = offsets[2*
n + 1] - offsets[2*
n];
1782 FWRITE(matchptr + offsets[
n*2], 1, plen, stdout);
1783 if (
do_colour) fprintf(stdout,
"%c[00m", 0x1b);
1789 if (printed || printname !=
NULL ||
number) fprintf(stdout,
"\n");
1807 startoffset = offsets[1];
1808 if (startoffset <= oldstartoffset)
1810 if ((
size_t)startoffset >= length)
1812 startoffset = oldstartoffset + 1;
1814 while ((matchptr[startoffset] & 0xc0) == 0x80) startoffset++;
1821 while (startoffset > (
int)linelength)
1823 matchptr = ptr += linelength + endlinelength;
1824 filepos += (
int)(linelength + endlinelength);
1826 startoffset -= (
int)(linelength + endlinelength);
1828 linelength =
t - ptr - endlinelength;
1829 length = (size_t)(endptr - ptr);
1832 goto ONLY_MATCHING_RESTART;
1849 char *p = lastmatchrestart;
1861 while (lastmatchrestart < p)
1863 char *pp = lastmatchrestart;
1864 if (printname !=
NULL) fprintf(stdout,
"%s-", printname);
1865 if (
number) fprintf(stdout,
"%lu-", lastmatchnumber++);
1867 FWRITE(lastmatchrestart, 1, pp - lastmatchrestart, stdout);
1868 lastmatchrestart = pp;
1877 fprintf(stdout,
"--\n");
1879 hyphenprinted =
TRUE;
1890 while (p >
main_buffer && (lastmatchnumber == 0 || p > lastmatchrestart) &&
1897 if (lastmatchnumber > 0 && p > lastmatchrestart && !hyphenprinted)
1898 fprintf(stdout,
"--\n");
1904 if (printname !=
NULL) fprintf(stdout,
"%s-", printname);
1905 if (
number) fprintf(stdout,
"%lu-", linenumber - linecount--);
1907 FWRITE(p, 1, pp - p, stdout);
1916 endhyphenpending =
TRUE;
1918 if (printname !=
NULL) fprintf(stdout,
"%s:", printname);
1919 if (
number) fprintf(stdout,
"%lu:", linenumber);
1929 char *endmatch = ptr + offsets[1];
1931 while (
t <= endmatch)
1934 if (
t < endmatch) linenumber++;
else break;
1936 linelength =
t - ptr - endlinelength;
1946 #ifdef JFRIEDL_DEBUG
1947 if (S_arg >= 0 && S_arg < mrc)
1949 int first = S_arg * 2;
1952 fprintf(stdout,
"X");
1953 FWRITE(ptr + offsets[
last], 1, linelength - offsets[
last], stdout);
1964 FWRITE(ptr, 1, offsets[0], stdout);
1966 FWRITE(ptr + offsets[0], 1, offsets[1] - offsets[0], stdout);
1967 fprintf(stdout,
"%c[00m", 0x1b);
1970 startoffset = offsets[1];
1971 if (startoffset >= (
int)linelength + endlinelength ||
1975 FWRITE(matchptr + startoffset, 1, offsets[0] - startoffset, stdout);
1977 FWRITE(matchptr + offsets[0], 1, offsets[1] - offsets[0], stdout);
1978 fprintf(stdout,
"%c[00m", 0x1b);
1985 plength = (
int)((linelength + endlinelength) - startoffset);
1986 if (plength > 0)
FWRITE(ptr + startoffset, 1, plength, stdout);
1991 else FWRITE(ptr, 1, linelength + endlinelength, stdout);
2003 lastmatchrestart = ptr + linelength + endlinelength;
2004 lastmatchnumber = linenumber + 1;
2014 char *endmatch = ptr + offsets[1];
2016 while (
t < endmatch)
2019 if (
t <= endmatch) linenumber++;
else break;
2021 endmatch =
end_of_line(endmatch, endptr, &ellength);
2022 linelength = endmatch - ptr - ellength;
2029 ptr += linelength + endlinelength;
2030 filepos += (
int)(linelength + endlinelength);
2036 if (input_line_buffered && bufflength < (
size_t)
bufsize)
2051 lastmatchnumber > 0 &&
2054 do_after_lines(lastmatchnumber, lastmatchrestart, endptr, printname);
2055 lastmatchnumber = 0;
2070 #ifdef SUPPORT_LIBBZ2
2078 (input_line_buffered?
2085 if (lastmatchnumber > 0) lastmatchrestart -=
bufthird;
2094 do_after_lines(lastmatchnumber, lastmatchrestart, endptr, printname);
2103 fprintf(stdout,
"%s\n", printname);
2114 fprintf(stdout,
"%s:", printname);
2115 fprintf(stdout,
"%lu\n", count);
2157 #ifdef SUPPORT_LIBBZ2
2161 #if defined SUPPORT_LIBZ || defined SUPPORT_LIBBZ2
2165 #if defined NATIVE_ZOS
2167 FILE *zos_test_file;
2172 if (
strcmp(pathname,
"-") == 0)
2183 lastcomp = strrchr(pathname,
FILESEP);
2184 lastcomp = (lastcomp ==
NULL)? pathname : lastcomp + 1;
2193 #if defined NATIVE_ZOS
2194 zos_test_file = fopen(pathname,
"rb");
2196 if (zos_test_file ==
NULL)
2198 if (!
silent) fprintf(stderr,
"pcregrep: failed to test next file %s\n",
2202 zos_type = identifyzosfiletype (zos_test_file);
2203 fclose (zos_test_file);
2207 if (zos_type == __ZOS_PDS || zos_type == __ZOS_PDSE)
2209 return travelonpdsdir (pathname, only_one_at_top);
2222 else if (zos_type == __ZOS_HFS)
2244 fprintf(stderr,
"pcregrep: Failed to open directory %s: %s\n", pathname,
2252 int fnlength = strlen(pathname) + strlen(nextfile) + 2;
2253 if (fnlength > 2048)
2255 fprintf(stderr,
"pcre2grep: recursive filename is too long\n");
2261 if (frc > 1) rc = frc;
2262 else if (frc == 0 && rc == 1) rc = 0;
2270 #if defined NATIVE_ZOS
2279 #
if defined NATIVE_ZOS
2293 #if defined SUPPORT_LIBZ || defined SUPPORT_LIBBZ2
2294 pathlen = (
int)(strlen(pathname));
2300 if (pathlen > 3 &&
strcmp(pathname + pathlen - 3,
".gz") == 0)
2302 ingz =
gzopen(pathname,
"rb");
2306 fprintf(stderr,
"pcregrep: Failed to open %s: %s\n", pathname,
2310 handle = (
void *)ingz;
2318 #ifdef SUPPORT_LIBBZ2
2319 if (pathlen > 4 &&
strcmp(pathname + pathlen - 4,
".bz2") == 0)
2322 handle = (
void *)inbz2;
2331 #ifdef SUPPORT_LIBBZ2
2335 in = fopen(pathname,
"rb");
2336 handle = (
void *)
in;
2345 fprintf(stderr,
"pcregrep: Failed to open %s: %s\n", pathname,
2367 #ifdef SUPPORT_LIBBZ2
2380 fprintf(stderr,
"pcregrep: Failed to read %s using bzlib: %s\n",
2440 fprintf(stdout,
"pcregrep version %s\n",
pcre_version());
2445 fprintf(stderr,
"pcregrep: Unknown option -%c\n",
letter);
2466 sprintf(p,
"%d",
n);
2467 while (*p != 0) p++;
2470 case 1: strcpy(p,
"st");
break;
2471 case 2: strcpy(p,
"nd");
break;
2472 case 3: strcpy(p,
"rd");
break;
2473 default: strcpy(p,
"th");
break;
2507 const char *fromtext,
int count)
2512 int patlen = strlen(ps);
2520 char *eop = ps + patlen;
2526 patlen = (
int)(pe - ps - ellength);
2533 fprintf(stderr,
"pcregrep: Buffer overflow while compiling \"%s\"\n",
2544 if (errptr > patlen) errptr = patlen;
2548 fprintf(stderr,
"pcregrep: Error in regex in line %d of %s "
2549 "at offset %d: %s\n", count, fromtext, errptr,
error);
2554 fprintf(stderr,
"pcregrep: Error in %s regex at offset %d: %s\n",
2555 fromtext, errptr,
error);
2557 fprintf(stderr,
"pcregrep: Error in %s %s regex at offset %d: %s\n",
2589 if (
strcmp(name,
"-") == 0)
2596 f = fopen(name,
"r");
2599 fprintf(stderr,
"pcregrep: Failed to open %s: %s\n", name,
strerror(errno));
2611 if (
buffer[0] == 0)
continue;
2619 if (*patlastptr ==
NULL)
2621 if (
f != stdin) fclose(
f);
2624 if (*patptr ==
NULL) *patptr = *patlastptr;
2636 if (
f != stdin) fclose(
f);
2639 (*patlastptr)->string =
NULL;
2640 if ((*patlastptr)->next ==
NULL)
break;
2641 *patlastptr = (*patlastptr)->
next;
2645 if (
f != stdin) fclose(
f);
2662 BOOL only_one_at_top;
2665 const char *locale_from =
"--locale";
2668 #ifdef SUPPORT_PCREGREP_JIT
2681 default:
newline = (
char *)
"lf";
break;
2682 case 13:
newline = (
char *)
"cr";
break;
2683 case (13 << 8) | 10:
newline = (
char *)
"crlf";
break;
2684 case -1:
newline = (
char *)
"any";
break;
2685 case -2:
newline = (
char *)
"anycrlf";
break;
2690 for (
i = 1;
i < argc;
i++)
2693 char *option_data = (
char *)
"";
2697 if (argv[
i][0] !=
'-')
break;
2702 if (argv[
i][1] == 0)
2710 if (argv[
i][1] ==
'-')
2712 char *arg = argv[
i] + 2;
2713 char *argequals = strchr(arg,
'=');
2732 char *opbra = strchr(op->
long_name,
'(');
2733 char *equals = strchr(op->
long_name,
'=');
2746 int arglen = (argequals ==
NULL)?
2747 (
int)strlen(arg) : (
int)(argequals - arg);
2750 option_data = arg + arglen;
2751 if (*option_data ==
'=')
2754 longopwasequals =
TRUE;
2770 int arglen = (argequals ==
NULL || equals ==
NULL)?
2771 (
int)strlen(arg) : (
int)(argequals - arg);
2773 if (snprintf(buff1,
sizeof(buff1),
"%.*s", baselen, op->
long_name) >
2774 (
int)
sizeof(buff1) ||
2775 snprintf(buff2,
sizeof(buff2),
"%s%.*s", buff1,
2776 fulllen - baselen - 2, opbra + 1) > (
int)
sizeof(buff2))
2778 fprintf(stderr,
"pcregrep: Buffer overflow when parsing %s option\n",
2783 if (
strncmp(arg, buff1, arglen) == 0 ||
2784 strncmp(arg, buff2, arglen) == 0)
2786 if (equals !=
NULL && argequals !=
NULL)
2788 option_data = argequals;
2789 if (*option_data ==
'=')
2792 longopwasequals =
TRUE;
2802 fprintf(stderr,
"pcregrep: Unknown option %s\n", argv[
i]);
2813 #ifdef JFRIEDL_DEBUG
2814 else if (
strcmp(argv[
i],
"-pre") == 0) {
2815 jfriedl_prefix = argv[++
i];
2817 }
else if (
strcmp(argv[
i],
"-post") == 0) {
2818 jfriedl_postfix = argv[++
i];
2820 }
else if (
strcmp(argv[
i],
"-XT") == 0) {
2821 sscanf(argv[++
i],
"%d", &jfriedl_XT);
2823 }
else if (
strcmp(argv[
i],
"-XR") == 0) {
2824 sscanf(argv[++
i],
"%d", &jfriedl_XR);
2835 char *s = argv[
i] + 1;
2846 fprintf(stderr,
"pcregrep: Unknown option letter '%c' in \"%s\"\n",
2856 if (*option_data == 0)
break;
2865 if (
isdigit((
unsigned char)s[1]))
break;
2894 if (*option_data == 0 &&
2909 #ifdef JFRIEDL_DEBUG
2920 if (*option_data == 0)
2922 if (
i >= argc - 1 || longopwasequals)
2924 fprintf(stderr,
"pcregrep: Data missing after %s\n", argv[
i]);
2927 option_data = argv[++
i];
2962 fprintf(stderr,
"pcregrep: malloc failed\n");
2966 fn->
name = option_data;
2978 if (
strcmp(option_data,
"binary") == 0)
2980 else if (
strcmp(option_data,
"without-match") == 0)
2982 else if (
strcmp(option_data,
"text") == 0)
2986 fprintf(stderr,
"pcregrep: unknown value \"%s\" for binary-files\n",
2997 *((
char **)op->
dataptr) = option_data;
3023 fprintf(stderr,
"pcregrep: Cannot mix --only-matching, --file-offsets "
3024 "and/or --line-offsets\n");
3036 locale = getenv(
"LC_ALL");
3037 locale_from =
"LCC_ALL";
3042 locale = getenv(
"LC_CTYPE");
3043 locale_from =
"LC_CTYPE";
3053 fprintf(stderr,
"pcregrep: Failed to set locale %s (obtained from %s)\n",
3068 fprintf(stderr,
"pcregrep: Unknown colour setting \"%s\"\n",
3074 char *cs = getenv(
"PCREGREP_COLOUR");
3075 if (cs ==
NULL) cs = getenv(
"PCREGREP_COLOR");
3109 fprintf(stderr,
"pcregrep: Invalid newline specifier \"%s\"\n",
newline);
3122 fprintf(stderr,
"pcregrep: Invalid value \"%s\" for -d\n",
dee_option);
3133 fprintf(stderr,
"pcregrep: Invalid value \"%s\" for -D\n",
DEE_option);
3140 #ifdef JFRIEDL_DEBUG
3143 fprintf(stderr,
"pcregrep: bad value for -S option\n");
3146 if (jfriedl_XT != 0 || jfriedl_XR != 0)
3148 if (jfriedl_XT == 0) jfriedl_XT = 1;
3149 if (jfriedl_XR == 0) jfriedl_XR = 1;
3160 fprintf(stderr,
"pcregrep: malloc failed\n");
3169 if (
i >= argc)
return usage(2);
3204 #ifdef SUPPORT_PCREGREP_JIT
3215 fprintf(stderr,
"pcregrep: Error while studying regex: %s\n",
error);
3217 fprintf(stderr,
"pcregrep: Error while studying regex number %d: %s\n",
3221 #ifdef SUPPORT_PCREGREP_JIT
3250 for (j = 0; j < 4; j++)
3256 (k == 1 && cp->next ==
NULL)? 0 : k))
3292 if (
strcmp(fn->
name,
"-") == 0) fl = stdin;
else
3294 fl = fopen(fn->
name,
"rb");
3297 fprintf(stderr,
"pcregrep: Failed to open %s: %s\n", fn->
name,
3311 if (frc > 1) rc = frc;
3312 else if (frc == 0 && rc == 1) rc = 0;
3315 if (fl != stdin) fclose(fl);
3324 for (;
i < argc;
i++)
3328 if (frc > 1) rc = frc;
3329 else if (frc == 0 && rc == 1) rc = 0;
3333 #ifdef SUPPORT_PCREGREP_JIT
BZFILE * BZ2_bzopen(const char *path, const char *mode)
int BZ2_bzread(BZFILE *b, void *buf, int len)
#define BZ_DATA_ERROR_MAGIC
const char * BZ2_bzerror(BZFILE *b, int *errnum)
void BZ2_bzclose(BZFILE *b)
static DLIST_TYPE *DLIST_NAME() first(DLIST_LIST_TYPE *list)
static DLIST_TYPE *DLIST_NAME() last(DLIST_LIST_TYPE *list)
static DLIST_TYPE *DLIST_NAME() next(DLIST_LIST_TYPE *list, DLIST_TYPE *item)
#define INVALID_HANDLE_VALUE
A value for an invalid file handle.
#define HANDLE
An abstraction for a file handle.
unsigned int
A callback function used to compare two keys in a database.
where boath are integers</td > n< td ></td > n</tr > n< tr > n< td > tse</td > n< td > optional</td > n< td > String</td > n< td class=\"description\"> TSE option controls what blob is orig
int strncmp(const char *str1, const char *str2, size_t count)
int strcmp(const char *str1, const char *str2)
std::istream & in(std::istream &in_, double &x_)
Int4 delta(size_t dimension_, const Int4 *score_)
double f(double x_, const double &y_)
void pcre_jit_stack_free(pcre_jit_stack *)
#define PCRE_STUDY_EXTRA_NEEDED
#define PCRE_ERROR_JIT_STACKLIMIT
#define PCRE_EXTRA_MATCH_LIMIT
struct real_pcre_jit_stack pcre_jit_stack
void pcre_assign_jit_stack(pcre_extra *, pcre_jit_callback, void *)
void(* pcre_free)(void *)
#define PCRE_NEWLINE_CRLF
pcre_jit_stack * pcre_jit_stack_alloc(int, int)
const char * pcre_version(void)
const unsigned char * pcre_maketables(void)
#define PCRE_ERROR_RECURSIONLIMIT
pcre_extra * pcre_study(const pcre *, int, const char **)
int pcre_config(int, void *)
#define PCRE_ERROR_MATCHLIMIT
#define PCRE_CONFIG_NEWLINE
#define PCRE_EXTRA_MATCH_LIMIT_RECURSION
#define PCRE_ERROR_NOMATCH
void pcre_free_study(pcre_extra *)
#define PCRE_STUDY_JIT_COMPILE
pcre * pcre_compile(const char *, int, const char **, int *, const unsigned char *)
int pcre_exec(const pcre *, const pcre_extra *, const char *, int, int, int, int *, int)
#define PCRE_NEWLINE_ANYCRLF
static int match(register const pcre_uchar *eptr, register const pcre_uchar *ecode, const pcre_uchar *mstart, int offset_top, match_data *md, eptrblock *eptrb, unsigned int rdepth)
static unsigned long int match_limit_recursion
static int before_context
static patstr * exclude_dir_patterns
static patstr * include_dir_patterns_last
static void pcregrep_exit(int rc)
static BOOL is_file_tty(FILE *f)
static BOOL is_stdout_tty(void)
static patstr * exclude_patterns_last
#define FWRITE(a, b, c, d)
directory_type * opendirectory(char *filename)
static char * om_separator
struct omdatastr omdatastr
struct fndatastr fndatastr
static char * main_buffer
static fnstr * include_from_last
static patstr * include_patterns
static int process_options
static const char * suffix[]
static omdatastr only_matching_data
static BOOL omit_zero_count
static BOOL read_pattern_file(char *name, patstr **patptr, patstr **patlastptr, int popts)
static fnstr * file_lists
static unsigned int read_one_line(char *buffer, int length, FILE *f)
static unsigned long int match_limit
static fndatastr exclude_from_data
int main(int argc, char **argv)
static BOOL compile_pattern(patstr *p, int options, int popts, int fromfile, const char *fromtext, int count)
static void free_pattern_chain(patstr *pc)
static patstr * exclude_dir_patterns_last
static const unsigned char * pcretables
static fnstr * pattern_files
static omstr * only_matching_last
static fndatastr file_lists_data
static char * ordin(int n)
static int handle_option(int letter, int options)
static int pcregrep(void *handle, int frtype, char *filename, char *printname)
void closedirectory(directory_type *dir)
static patstr * include_dir_patterns
static BOOL match_patterns(char *matchptr, size_t length, unsigned int options, int startoffset, int *offsets, int *mrc)
static patdatastr match_patdata
static void do_after_lines(unsigned long int lastmatchnumber, char *lastmatchrestart, char *endptr, char *printname)
static BOOL hyphenpending
static fndatastr pattern_files_data
static fnstr * file_lists_last
static const char * prefix[]
int isregfile(char *filename)
static BOOL line_buffered
struct patdatastr patdatastr
static omstr * only_matching
static BOOL resource_error
static BOOL test_incexc(char *path, patstr *ip, patstr *ep)
static patdatastr include_patdata
static patstr * add_pattern(char *s, patstr *after)
static fnstr * include_from
static patdatastr exclude_patdata
struct option_item option_item
static int grep_or_recurse(char *pathname, BOOL dir_recurse, BOOL only_one_at_top)
static patstr ** incexlist[4]
static const char * incexname[4]
static char * colour_string
static char * colour_option
static char * previous_line(char *p, char *startptr)
static fnstr * pattern_files_last
static patstr * exclude_patterns
static fnstr * exclude_from_last
static omstr * add_number(int n, omstr *after)
static char * end_of_line(char *p, char *endptr, int *lenptr)
static void free_file_chain(fnstr *fn)
static patstr * patterns_last
static patstr * include_patterns_last
static fnstr * exclude_from
static fndatastr include_from_data
static BOOL show_only_matching
int isdirectory(char *filename)
static patdatastr exclude_dir_patdata
char * readdirectory(directory_type *dir)
static long int decode_number(char *option_data, option_item *op, BOOL longop)
static patdatastr include_dir_patdata
static option_item optionlist[]
static pcre_uint8 * buffer
CRef< objects::CObjectManager > om
static Uint4 letter(char c)