"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "pcregrep.c" between
pcre-8.41.tar.bz2 and pcre-8.42.tar.bz2

About: The PCRE library implements Perl compatible regular expression pattern matching.

pcregrep.c  (pcre-8.41.tar.bz2):pcregrep.c  (pcre-8.42.tar.bz2)
skipping to change at line 1350 skipping to change at line 1350
Arguments: Arguments:
lastmatchnumber the number of the last matching line, plus one lastmatchnumber the number of the last matching line, plus one
lastmatchrestart where we restarted after the last match lastmatchrestart where we restarted after the last match
endptr end of available data endptr end of available data
printname filename for printing printname filename for printing
Returns: nothing Returns: nothing
*/ */
static void static void
do_after_lines(int lastmatchnumber, char *lastmatchrestart, char *endptr, do_after_lines(unsigned long int lastmatchnumber, char *lastmatchrestart,
char *printname) char *endptr, char *printname)
{ {
if (after_context > 0 && lastmatchnumber > 0) if (after_context > 0 && lastmatchnumber > 0)
{ {
int count = 0; int count = 0;
while (lastmatchrestart < endptr && count++ < after_context) while (lastmatchrestart < endptr && count++ < after_context)
{ {
int ellength; int ellength;
char *pp = lastmatchrestart; char *pp = lastmatchrestart;
if (printname != NULL) fprintf(stdout, "%s-", printname); if (printname != NULL) fprintf(stdout, "%s-", printname);
if (number) fprintf(stdout, "%d-", lastmatchnumber++); if (number) fprintf(stdout, "%lu-", lastmatchnumber++);
pp = end_of_line(pp, endptr, &ellength); pp = end_of_line(pp, endptr, &ellength);
FWRITE(lastmatchrestart, 1, pp - lastmatchrestart, stdout); FWRITE(lastmatchrestart, 1, pp - lastmatchrestart, stdout);
lastmatchrestart = pp; lastmatchrestart = pp;
} }
hyphenpending = TRUE; hyphenpending = TRUE;
} }
} }
/************************************************* /*************************************************
* Apply patterns to subject till one matches * * Apply patterns to subject till one matches *
skipping to change at line 1461 skipping to change at line 1461
Returns: 0 if there was at least one match Returns: 0 if there was at least one match
1 otherwise (no matches) 1 otherwise (no matches)
2 if an overlong line is encountered 2 if an overlong line is encountered
3 if there is a read error on a .bz2 file 3 if there is a read error on a .bz2 file
*/ */
static int static int
pcregrep(void *handle, int frtype, char *filename, char *printname) pcregrep(void *handle, int frtype, char *filename, char *printname)
{ {
int rc = 1; int rc = 1;
int linenumber = 1;
int lastmatchnumber = 0;
int count = 0;
int filepos = 0; int filepos = 0;
int offsets[OFFSET_SIZE]; int offsets[OFFSET_SIZE];
unsigned long int linenumber = 1;
unsigned long int lastmatchnumber = 0;
unsigned long int count = 0;
char *lastmatchrestart = NULL; char *lastmatchrestart = NULL;
char *ptr = main_buffer; char *ptr = main_buffer;
char *endptr; char *endptr;
size_t bufflength; size_t bufflength;
BOOL binary = FALSE; BOOL binary = FALSE;
BOOL endhyphenpending = FALSE; BOOL endhyphenpending = FALSE;
BOOL input_line_buffered = line_buffered; BOOL input_line_buffered = line_buffered;
FILE *in = NULL; /* Ensure initialized */ FILE *in = NULL; /* Ensure initialized */
#ifdef SUPPORT_LIBZ #ifdef SUPPORT_LIBZ
skipping to change at line 1567 skipping to change at line 1567
t = end_of_line(t, endptr, &endlinelength); t = end_of_line(t, endptr, &endlinelength);
linelength = t - ptr - endlinelength; linelength = t - ptr - endlinelength;
length = multiline? (size_t)(endptr - ptr) : linelength; length = multiline? (size_t)(endptr - ptr) : linelength;
/* Check to see if the line we are looking at extends right to the very end /* Check to see if the line we are looking at extends right to the very end
of the buffer without a line terminator. This means the line is too long to of the buffer without a line terminator. This means the line is too long to
handle. */ handle. */
if (endlinelength == 0 && t == main_buffer + bufsize) if (endlinelength == 0 && t == main_buffer + bufsize)
{ {
fprintf(stderr, "pcregrep: line %d%s%s is too long for the internal buffer\n " fprintf(stderr, "pcregrep: line %lu%s%s is too long for the internal buffer\ n"
"pcregrep: check the --buffer-size option\n", "pcregrep: check the --buffer-size option\n",
linenumber, linenumber,
(filename == NULL)? "" : " of file ", (filename == NULL)? "" : " of file ",
(filename == NULL)? "" : filename); (filename == NULL)? "" : filename);
return 2; return 2;
} }
/* Extra processing for Jeffrey Friedl's debugging. */ /* Extra processing for Jeffrey Friedl's debugging. */
#ifdef JFRIEDL_DEBUG #ifdef JFRIEDL_DEBUG
skipping to change at line 1704 skipping to change at line 1704
substring will be returned. The PCRE1 library does not return the substring will be returned. The PCRE1 library does not return the
bumpalong offset, so all we can do is ignore repeated strings. (PCRE2 bumpalong offset, so all we can do is ignore repeated strings. (PCRE2
does this better.) */ does this better.) */
if (prevoffsets[0] != offsets[0] || prevoffsets[1] != offsets[1]) if (prevoffsets[0] != offsets[0] || prevoffsets[1] != offsets[1])
{ {
prevoffsets[0] = offsets[0]; prevoffsets[0] = offsets[0];
prevoffsets[1] = offsets[1]; prevoffsets[1] = offsets[1];
if (printname != NULL) fprintf(stdout, "%s:", printname); if (printname != NULL) fprintf(stdout, "%s:", printname);
if (number) fprintf(stdout, "%d:", linenumber); if (number) fprintf(stdout, "%lu:", linenumber);
/* Handle --line-offsets */ /* Handle --line-offsets */
if (line_offsets) if (line_offsets)
fprintf(stdout, "%d,%d\n", (int)(matchptr + offsets[0] - ptr), fprintf(stdout, "%d,%d\n", (int)(matchptr + offsets[0] - ptr),
offsets[1] - offsets[0]); offsets[1] - offsets[0]);
/* Handle --file-offsets */ /* Handle --file-offsets */
else if (file_offsets) else if (file_offsets)
skipping to change at line 1819 skipping to change at line 1819
} }
/* It is important to advance lastmatchrestart during this printing so /* It is important to advance lastmatchrestart during this printing so
that it interacts correctly with any "before" printing below. Print that it interacts correctly with any "before" printing below. Print
each line's data using fwrite() in case there are binary zeroes. */ each line's data using fwrite() in case there are binary zeroes. */
while (lastmatchrestart < p) while (lastmatchrestart < p)
{ {
char *pp = lastmatchrestart; char *pp = lastmatchrestart;
if (printname != NULL) fprintf(stdout, "%s-", printname); if (printname != NULL) fprintf(stdout, "%s-", printname);
if (number) fprintf(stdout, "%d-", lastmatchnumber++); if (number) fprintf(stdout, "%lu-", lastmatchnumber++);
pp = end_of_line(pp, endptr, &ellength); pp = end_of_line(pp, endptr, &ellength);
FWRITE(lastmatchrestart, 1, pp - lastmatchrestart, stdout); FWRITE(lastmatchrestart, 1, pp - lastmatchrestart, stdout);
lastmatchrestart = pp; lastmatchrestart = pp;
} }
if (lastmatchrestart != ptr) hyphenpending = TRUE; if (lastmatchrestart != ptr) hyphenpending = TRUE;
} }
/* If there were non-contiguous lines printed above, insert hyphens. */ /* If there were non-contiguous lines printed above, insert hyphens. */
if (hyphenpending) if (hyphenpending)
skipping to change at line 1859 skipping to change at line 1859
} }
if (lastmatchnumber > 0 && p > lastmatchrestart && !hyphenprinted) if (lastmatchnumber > 0 && p > lastmatchrestart && !hyphenprinted)
fprintf(stdout, "--\n"); fprintf(stdout, "--\n");
while (p < ptr) while (p < ptr)
{ {
int ellength; int ellength;
char *pp = p; char *pp = p;
if (printname != NULL) fprintf(stdout, "%s-", printname); if (printname != NULL) fprintf(stdout, "%s-", printname);
if (number) fprintf(stdout, "%d-", linenumber - linecount--); if (number) fprintf(stdout, "%lu-", linenumber - linecount--);
pp = end_of_line(pp, endptr, &ellength); pp = end_of_line(pp, endptr, &ellength);
FWRITE(p, 1, pp - p, stdout); FWRITE(p, 1, pp - p, stdout);
p = pp; p = pp;
} }
} }
/* Now print the matching line(s); ensure we set hyphenpending at the end /* Now print the matching line(s); ensure we set hyphenpending at the end
of the file if any context lines are being output. */ of the file if any context lines are being output. */
if (after_context > 0 || before_context > 0) if (after_context > 0 || before_context > 0)
endhyphenpending = TRUE; endhyphenpending = TRUE;
if (printname != NULL) fprintf(stdout, "%s:", printname); if (printname != NULL) fprintf(stdout, "%s:", printname);
if (number) fprintf(stdout, "%d:", linenumber); if (number) fprintf(stdout, "%lu:", linenumber);
/* In multiline mode, we want to print to the end of the line in which /* In multiline mode, we want to print to the end of the line in which
the end of the matched string is found, so we adjust linelength and the the end of the matched string is found, so we adjust linelength and the
line number appropriately, but only when there actually was a match line number appropriately, but only when there actually was a match
(invert not set). Because the PCRE_FIRSTLINE option is set, the start of (invert not set). Because the PCRE_FIRSTLINE option is set, the start of
the match will always be before the first newline sequence. */ the match will always be before the first newline sequence. */
if (multiline & !invert) if (multiline & !invert)
{ {
char *endmatch = ptr + offsets[1]; char *endmatch = ptr + offsets[1];
skipping to change at line 2069 skipping to change at line 2069
} }
/* Print the match count if wanted */ /* Print the match count if wanted */
if (count_only && !quiet) if (count_only && !quiet)
{ {
if (count > 0 || !omit_zero_count) if (count > 0 || !omit_zero_count)
{ {
if (printname != NULL && filenames != FN_NONE) if (printname != NULL && filenames != FN_NONE)
fprintf(stdout, "%s:", printname); fprintf(stdout, "%s:", printname);
fprintf(stdout, "%d\n", count); fprintf(stdout, "%lu\n", count);
} }
} }
return rc; return rc;
} }
/************************************************* /*************************************************
* Grep a file or recurse into a directory * * Grep a file or recurse into a directory *
*************************************************/ *************************************************/
skipping to change at line 2187 skipping to change at line 2187
/* Handle directories: common code for all OS */ /* Handle directories: common code for all OS */
if (isdirectory(pathname)) if (isdirectory(pathname))
{ {
if (dee_action == dee_SKIP || if (dee_action == dee_SKIP ||
!test_incexc(lastcomp, include_dir_patterns, exclude_dir_patterns)) !test_incexc(lastcomp, include_dir_patterns, exclude_dir_patterns))
return -1; return -1;
if (dee_action == dee_RECURSE) if (dee_action == dee_RECURSE)
{ {
char buffer[1024]; char buffer[2048];
char *nextfile; char *nextfile;
directory_type *dir = opendirectory(pathname); directory_type *dir = opendirectory(pathname);
if (dir == NULL) if (dir == NULL)
{ {
if (!silent) if (!silent)
fprintf(stderr, "pcregrep: Failed to open directory %s: %s\n", pathname, fprintf(stderr, "pcregrep: Failed to open directory %s: %s\n", pathname,
strerror(errno)); strerror(errno));
return 2; return 2;
} }
while ((nextfile = readdirectory(dir)) != NULL) while ((nextfile = readdirectory(dir)) != NULL)
{ {
int frc; int frc;
sprintf(buffer, "%.512s%c%.128s", pathname, FILESEP, nextfile); int fnlength = strlen(pathname) + strlen(nextfile) + 2;
if (fnlength > 2048)
{
fprintf(stderr, "pcre2grep: recursive filename is too long\n");
rc = 2;
break;
}
sprintf(buffer, "%s%c%s", pathname, FILESEP, nextfile);
frc = grep_or_recurse(buffer, dir_recurse, FALSE); frc = grep_or_recurse(buffer, dir_recurse, FALSE);
if (frc > 1) rc = frc; if (frc > 1) rc = frc;
else if (frc == 0 && rc == 1) rc = 0; else if (frc == 0 && rc == 1) rc = 0;
} }
closedirectory(dir); closedirectory(dir);
return rc; return rc;
} }
} }
skipping to change at line 2466 skipping to change at line 2473
char *eop = ps + patlen; char *eop = ps + patlen;
char *pe = end_of_line(ps, eop, &ellength); char *pe = end_of_line(ps, eop, &ellength);
if (ellength != 0) if (ellength != 0)
{ {
if (add_pattern(pe, p) == NULL) return FALSE; if (add_pattern(pe, p) == NULL) return FALSE;
patlen = (int)(pe - ps - ellength); patlen = (int)(pe - ps - ellength);
} }
} }
sprintf(buffer, "%s%.*s%s", prefix[popts], patlen, ps, suffix[popts]); if (snprintf(buffer, PATBUFSIZE, "%s%.*s%s", prefix[popts], patlen, ps,
suffix[popts]) > PATBUFSIZE)
{
fprintf(stderr, "pcregrep: Buffer overflow while compiling \"%s\"\n",
ps);
return FALSE;
}
p->compiled = pcre_compile(buffer, options, &error, &errptr, pcretables); p->compiled = pcre_compile(buffer, options, &error, &errptr, pcretables);
if (p->compiled != NULL) return TRUE; if (p->compiled != NULL) return TRUE;
/* Handle compile errors */ /* Handle compile errors */
errptr -= (int)strlen(prefix[popts]); errptr -= (int)strlen(prefix[popts]);
if (errptr > patlen) errptr = patlen; if (errptr > patlen) errptr = patlen;
if (fromfile) if (fromfile)
{ {
skipping to change at line 2698 skipping to change at line 2712
else else
{ {
char buff1[24]; char buff1[24];
char buff2[24]; char buff2[24];
int baselen = (int)(opbra - op->long_name); int baselen = (int)(opbra - op->long_name);
int fulllen = (int)(strchr(op->long_name, ')') - op->long_name + 1); int fulllen = (int)(strchr(op->long_name, ')') - op->long_name + 1);
int arglen = (argequals == NULL || equals == NULL)? int arglen = (argequals == NULL || equals == NULL)?
(int)strlen(arg) : (int)(argequals - arg); (int)strlen(arg) : (int)(argequals - arg);
sprintf(buff1, "%.*s", baselen, op->long_name); if (snprintf(buff1, sizeof(buff1), "%.*s", baselen, op->long_name) >
sprintf(buff2, "%s%.*s", buff1, fulllen - baselen - 2, opbra + 1); (int)sizeof(buff1) ||
snprintf(buff2, sizeof(buff2), "%s%.*s", buff1,
fulllen - baselen - 2, opbra + 1) > (int)sizeof(buff2))
{
fprintf(stderr, "pcregrep: Buffer overflow when parsing %s option\n",
op->long_name);
pcregrep_exit(2);
}
if (strncmp(arg, buff1, arglen) == 0 || if (strncmp(arg, buff1, arglen) == 0 ||
strncmp(arg, buff2, arglen) == 0) strncmp(arg, buff2, arglen) == 0)
{ {
if (equals != NULL && argequals != NULL) if (equals != NULL && argequals != NULL)
{ {
option_data = argequals; option_data = argequals;
if (*option_data == '=') if (*option_data == '=')
{ {
option_data++; option_data++;
 End of changes. 14 change blocks. 
17 lines changed or deleted 38 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)