| 844 |
int i, endlinelength; |
int i, endlinelength; |
| 845 |
int mrc = 0; |
int mrc = 0; |
| 846 |
BOOL match = FALSE; |
BOOL match = FALSE; |
| 847 |
|
char *matchptr = ptr; |
| 848 |
char *t = ptr; |
char *t = ptr; |
| 849 |
size_t length, linelength; |
size_t length, linelength; |
| 850 |
|
|
| 907 |
} |
} |
| 908 |
#endif |
#endif |
| 909 |
|
|
| 910 |
|
/* We come back here after a match when the -o option (only_matching) is set, |
| 911 |
|
in order to find any further matches in the same line. */ |
| 912 |
|
|
| 913 |
|
ONLY_MATCHING_RESTART: |
| 914 |
|
|
| 915 |
/* Run through all the patterns until one matches. Note that we don't include |
/* Run through all the patterns until one matches. Note that we don't include |
| 916 |
the final newline in the subject string. */ |
the final newline in the subject string. */ |
| 917 |
|
|
| 918 |
for (i = 0; i < pattern_count; i++) |
for (i = 0; i < pattern_count; i++) |
| 919 |
{ |
{ |
| 920 |
mrc = pcre_exec(pattern_list[i], hints_list[i], ptr, length, 0, 0, |
mrc = pcre_exec(pattern_list[i], hints_list[i], matchptr, length, 0, 0, |
| 921 |
offsets, 99); |
offsets, 99); |
| 922 |
if (mrc >= 0) { match = TRUE; break; } |
if (mrc >= 0) { match = TRUE; break; } |
| 923 |
if (mrc != PCRE_ERROR_NOMATCH) |
if (mrc != PCRE_ERROR_NOMATCH) |
| 925 |
fprintf(stderr, "pcregrep: pcre_exec() error %d while matching ", mrc); |
fprintf(stderr, "pcregrep: pcre_exec() error %d while matching ", mrc); |
| 926 |
if (pattern_count > 1) fprintf(stderr, "pattern number %d to ", i+1); |
if (pattern_count > 1) fprintf(stderr, "pattern number %d to ", i+1); |
| 927 |
fprintf(stderr, "this line:\n"); |
fprintf(stderr, "this line:\n"); |
| 928 |
fwrite(ptr, 1, linelength, stderr); /* In case binary zero included */ |
fwrite(matchptr, 1, linelength, stderr); /* In case binary zero included */ |
| 929 |
fprintf(stderr, "\n"); |
fprintf(stderr, "\n"); |
| 930 |
if (error_count == 0 && |
if (error_count == 0 && |
| 931 |
(mrc == PCRE_ERROR_MATCHLIMIT || mrc == PCRE_ERROR_RECURSIONLIMIT)) |
(mrc == PCRE_ERROR_MATCHLIMIT || mrc == PCRE_ERROR_RECURSIONLIMIT)) |
| 972 |
else if (quiet) return 0; |
else if (quiet) return 0; |
| 973 |
|
|
| 974 |
/* The --only-matching option prints just the substring that matched, and |
/* The --only-matching option prints just the substring that matched, and |
| 975 |
does not pring any context. */ |
does not print any context. Afterwards, adjust the start and length, and |
| 976 |
|
then jump back to look for further matches in the same line. If we are in |
| 977 |
|
invert mode, however, nothing is printed - this could be useful still |
| 978 |
|
because the return code is set. */ |
| 979 |
|
|
| 980 |
else if (only_matching) |
else if (only_matching) |
| 981 |
{ |
{ |
| 982 |
if (printname != NULL) fprintf(stdout, "%s:", printname); |
if (!invert) |
| 983 |
if (number) fprintf(stdout, "%d:", linenumber); |
{ |
| 984 |
fwrite(ptr + offsets[0], 1, offsets[1] - offsets[0], stdout); |
if (printname != NULL) fprintf(stdout, "%s:", printname); |
| 985 |
fprintf(stdout, "\n"); |
if (number) fprintf(stdout, "%d:", linenumber); |
| 986 |
|
fwrite(matchptr + offsets[0], 1, offsets[1] - offsets[0], stdout); |
| 987 |
|
fprintf(stdout, "\n"); |
| 988 |
|
matchptr += offsets[1]; |
| 989 |
|
length -= offsets[1]; |
| 990 |
|
match = FALSE; |
| 991 |
|
goto ONLY_MATCHING_RESTART; |
| 992 |
|
} |
| 993 |
} |
} |
| 994 |
|
|
| 995 |
/* This is the default case when none of the above options is set. We print |
/* This is the default case when none of the above options is set. We print |