872 |
static void |
static void |
873 |
prline (char const *beg, char const *lim, int sep) |
prline (char const *beg, char const *lim, int sep) |
874 |
{ |
{ |
875 |
|
const char *line_color; |
876 |
|
|
877 |
if (!only_matching) |
if (!only_matching) |
878 |
print_line_head(beg, lim, sep); |
print_line_head(beg, lim, sep); |
879 |
else |
|
|
{ |
|
|
size_t match_size; |
|
|
size_t match_offset; |
|
|
if(match_icase) |
|
|
{ |
|
|
char *buf = (char*) xmalloc (lim - beg); |
|
|
char *ibeg = buf; |
|
|
char *ilim = ibeg + (lim - beg); |
|
|
int i; |
|
|
for (i = 0; i < lim - beg; i++) |
|
|
ibeg[i] = tolower (beg[i]); |
|
|
|
|
|
while ((match_offset = (*execute) (ibeg, ilim - ibeg, &match_size, 1)) |
|
|
!= (size_t) -1) |
|
|
{ |
|
|
char const *b = beg + match_offset; |
|
|
if (b == lim) |
|
|
break; |
|
|
if (match_size == 0) |
|
|
break; |
|
|
print_line_head(b, lim, sep); |
|
|
PR_SGR_START_IF(grep_color); |
|
|
fwrite(b, sizeof (char), match_size, stdout); |
|
|
PR_SGR_END_IF(grep_color); |
|
|
fputs("\n", stdout); |
|
|
beg = b + match_size; |
|
|
ibeg += match_offset + match_size; |
|
|
} |
|
|
free (buf); |
|
|
lastout = lim; |
|
|
if(line_buffered) |
|
|
fflush(stdout); |
|
|
return; |
|
|
} |
|
|
while ((match_offset = (*execute) (beg, lim - beg, &match_size, 1)) |
|
|
!= (size_t) -1) |
|
|
{ |
|
|
char const *b = beg + match_offset; |
|
|
if (b == lim) |
|
|
break; |
|
|
if (match_size == 0) |
|
|
break; |
|
|
print_line_head(b, lim, sep); |
|
|
PR_SGR_START_IF(grep_color); |
|
|
fwrite(b, sizeof (char), match_size, stdout); |
|
|
PR_SGR_END_IF(grep_color); |
|
|
fputs("\n", stdout); |
|
|
beg = b + match_size; |
|
|
} |
|
|
lastout = lim; |
|
|
if(line_buffered) |
|
|
fflush(stdout); |
|
|
return; |
|
|
} |
|
880 |
if (color_option) |
if (color_option) |
881 |
|
line_color = ((sep == SEP_CHAR_MATCH) ? mlines_color : context_color); |
882 |
|
|
883 |
|
if (only_matching || (color_option && (*grep_color || *line_color))) |
884 |
{ |
{ |
885 |
size_t match_size; |
/* We already know that context lines have no match (to colorize). */ |
886 |
size_t match_offset; |
if (only_matching || (*grep_color && sep == SEP_CHAR_MATCH)) |
887 |
if(match_icase) |
beg = print_line_middle(beg, lim); |
888 |
{ |
|
889 |
/* Yuck, this is tricky */ |
if (!only_matching && *line_color); |
890 |
char *buf = (char*) xmalloc (lim - beg); |
beg = print_line_tail(beg, lim, line_color); |
891 |
char *ibeg = buf; |
|
892 |
char *ilim = ibeg + (lim - beg); |
/* This global is modified by print_line_middle() |
893 |
int i; |
and checked by print_line_tail(). Now reset it. */ |
894 |
for (i = 0; i < lim - beg; i++) |
if (pseudo_markup < 0) |
895 |
ibeg[i] = tolower (beg[i]); |
pseudo_markup = 1; |
|
while ((match_offset = (*execute) (ibeg, ilim-ibeg, &match_size, 1)) |
|
|
!= (size_t) -1) |
|
|
{ |
|
|
char const *b = beg + match_offset; |
|
|
if (b == lim) |
|
|
break; |
|
|
if (match_size == 0) |
|
|
break; |
|
|
fwrite (beg, sizeof (char), match_offset, stdout); |
|
|
PR_SGR_START(grep_color); |
|
|
fwrite (b, sizeof (char), match_size, stdout); |
|
|
PR_SGR_END(grep_color); |
|
|
beg = b + match_size; |
|
|
ibeg = ibeg + match_offset + match_size; |
|
|
} |
|
|
fwrite (beg, 1, lim - beg, stdout); |
|
|
free (buf); |
|
|
lastout = lim; |
|
|
return; |
|
|
} |
|
|
while (lim-beg && (match_offset = (*execute) (beg, lim - beg, &match_size, 1)) |
|
|
!= (size_t) -1) |
|
|
{ |
|
|
char const *b = beg + match_offset; |
|
|
/* Avoid matching the empty line at the end of the buffer. */ |
|
|
if (b == lim) |
|
|
break; |
|
|
/* Avoid hanging on grep --color "" foo */ |
|
|
if (match_size == 0) |
|
|
break; |
|
|
fwrite (beg, sizeof (char), match_offset, stdout); |
|
|
PR_SGR_START(grep_color); |
|
|
fwrite (b, sizeof (char), match_size, stdout); |
|
|
PR_SGR_END(grep_color); |
|
|
beg = b + match_size; |
|
|
} |
|
896 |
} |
} |
897 |
fwrite (beg, 1, lim - beg, stdout); |
|
898 |
|
if (!only_matching && lim > beg) |
899 |
|
fwrite (beg, 1, lim - beg, stdout); |
900 |
|
|
901 |
if (ferror (stdout)) |
if (ferror (stdout)) |
902 |
error (0, errno, _("writing output")); |
error (0, errno, _("writing output")); |
903 |
|
|
904 |
lastout = lim; |
lastout = lim; |
905 |
|
|
906 |
if (line_buffered) |
if (line_buffered) |
907 |
fflush (stdout); |
fflush (stdout); |
908 |
} |
} |