Mail Archives: djgpp-workers/2001/02/18/04:11:59
[I took a liberty of CC'ing djgpp-workers.]
On Sun, 18 Feb 2001, Alain Magloire wrote:
> - The new option --color will use the environment variable GREP_COLOR
> (default is red) to highlight the matching string.
This is a welcome feature, but the way it is implemented is IMHO too
Unix-centric. Here's the relevant snippet from grep.c:
if (color_option)
{
size_t match_size;
size_t match_offset;
while ((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;
fwrite (beg, sizeof (char), match_offset, stdout);
printf ("\33[%sm", grep_color);
fwrite (b, sizeof (char), match_size, stdout);
fputs ("\33[00m", stdout);
beg = b + match_size;
}
}
This assumes that the terminal device driver automatically supports ANSI
escape sequences. That assumption doesn't hold on DOS and Windows, and
possibly also on other systems.
Now, I did port to DJGPP GNU `ls' that used the same technique, so the
way to work around this is known. But the solution is highly
non-portable to anything but DJGPP (it uses the Filesystem Extension
feature which AFAIK is unique to DJGPP), and is nothing but a cleverly
disguised kludge.
I'd argue that, once printed text is anything beyond simple ASCII text, a
text-mode application such as Grep should use specialized functions to
print the text with whatever markup it wants. Each platform then could
supply its own implementation of the function(s) that deliver text with
markup to the screen. The default implementation, for Unix and GNU/Linux
systems, could simply use the code above.
In other words, I'm asking for Grep to define special functions to print
text to the screen and use them instead of fwrite/printf/fprintf it used
until now. Each non-Posix port will then have a chance to define its own
implementation of those functions.
- Raw text -