1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/* Output colorization on MS-Windows.
Copyright 2011-2018 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
02110-1301, USA. */
/* Written by Eli Zaretskii. */
#include <config.h>
#include "colorize.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#undef DATADIR /* conflicts with objidl.h, which is included by windows.h */
#include <windows.h>
static HANDLE hstdout = INVALID_HANDLE_VALUE;
static SHORT norm_attr;
/* Initialize the normal text attribute used by the console. */
void
init_colorize (void)
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
hstdout = GetStdHandle (STD_OUTPUT_HANDLE);
if (hstdout != INVALID_HANDLE_VALUE
&& GetConsoleScreenBufferInfo (hstdout, &csbi))
norm_attr = csbi.wAttributes;
else
hstdout = INVALID_HANDLE_VALUE;
}
/* Return non-zero if we should highlight matches in output. */
int
should_colorize (void)
{
/* $TERM is not normally defined on DOS/Windows, so don't require
it for highlighting. But some programs, like Emacs, do define
it when running Grep as a subprocess, so make sure they don't
set TERM=dumb. */
char const *t = getenv ("TERM");
return ! (t && strcmp (t, "dumb") == 0);
}
/* Convert a color spec, a semi-colon separated list of the form
"NN;MM;KK;...", where each number is a value of the SGR parameter,
into the corresponding Windows console text attribute.
This function supports a subset of the SGR rendition aspects that
the Windows console can display. */
static int
w32_sgr2attr (const char *sgr_seq)
{
const char *s, *p;
int code, fg = norm_attr & 15, bg = norm_attr & (15 << 4);
int bright = 0, inverse = 0;
static const int fg_color[] = {
0, /* black */
FOREGROUND_RED, /* red */
FOREGROUND_GREEN, /* green */
FOREGROUND_GREEN | FOREGROUND_RED, /* yellow */
FOREGROUND_BLUE, /* blue */
FOREGROUND_BLUE | FOREGROUND_RED, /* magenta */
FOREGROUND_BLUE | FOREGROUND_GREEN, /* cyan */
FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE /* gray */
};
static const int bg_color[] = {
0, /* black */
BACKGROUND_RED, /* red */
BACKGROUND_GREEN, /* green */
BACKGROUND_GREEN | BACKGROUND_RED, /* yellow */
BACKGROUND_BLUE, /* blue */
BACKGROUND_BLUE | BACKGROUND_RED, /* magenta */
BACKGROUND_BLUE | BACKGROUND_GREEN, /* cyan */
BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE /* gray */
};
for (s = p = sgr_seq; *s; p++)
{
if (*p == ';' || *p == '\0')
{
code = strtol (s, NULL, 10);
s = p + (*p != '\0');
switch (code)
{
case 0: /* all attributes off */
fg = norm_attr & 15;
bg = norm_attr & (15 << 4);
bright = 0;
inverse = 0;
break;
case 1: /* intensity on */
bright = 1;
break;
case 7: /* inverse video */
inverse = 1;
break;
case 22: /* intensity off */
bright = 0;
break;
case 27: /* inverse off */
inverse = 0;
break;
case 30: case 31: case 32: case 33: /* foreground color */
case 34: case 35: case 36: case 37:
fg = fg_color[code - 30];
break;
case 39: /* default foreground */
fg = norm_attr & 15;
break;
case 40: case 41: case 42: case 43: /* background color */
case 44: case 45: case 46: case 47:
bg = bg_color[code - 40];
break;
case 49: /* default background */
bg = norm_attr & (15 << 4);
break;
default:
break;
}
}
}
if (inverse)
{
int t = fg;
fg = (bg >> 4);
bg = (t << 4);
}
if (bright)
fg |= FOREGROUND_INTENSITY;
return (bg & (15 << 4)) | (fg & 15);
}
/* Start a colorized text attribute on stdout using the SGR_START
format; the attribute is specified by SGR_SEQ. */
void
print_start_colorize (char const *sgr_start, char const *sgr_seq)
{
/* If stdout is connected to a console, set the console text
attribute directly instead of using SGR_START. Otherwise, use
SGR_START to emit the SGR escape sequence as on Posix platforms;
this is needed when Grep is invoked as a subprocess of another
program, such as Emacs, which will handle the display of the
matches. */
if (hstdout != INVALID_HANDLE_VALUE)
{
SHORT attr = w32_sgr2attr (sgr_seq);
SetConsoleTextAttribute (hstdout, attr);
}
else
printf (sgr_start, sgr_seq);
}
/* Clear to the end of the current line with the default attribute.
This is needed for reasons similar to those that require the "EL to
Right after SGR" operation on Posix platforms: if we don't do this,
setting the 'mt', 'ms', or 'mc' capabilities to use a non-default
background color spills that color to the empty space at the end of
the last screen line in a match whose line spans multiple screen
lines. */
static void
w32_clreol (void)
{
DWORD nchars;
COORD start_pos;
DWORD written;
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo (hstdout, &csbi);
start_pos = csbi.dwCursorPosition;
nchars = csbi.dwSize.X - start_pos.X;
FillConsoleOutputAttribute (hstdout, norm_attr, nchars, start_pos,
&written);
FillConsoleOutputCharacter (hstdout, ' ', nchars, start_pos, &written);
}
/* Restore the normal text attribute using the SGR_END string. */
void
print_end_colorize (char const *sgr_end)
{
if (hstdout != INVALID_HANDLE_VALUE)
{
SetConsoleTextAttribute (hstdout, norm_attr);
w32_clreol ();
}
else
fputs (sgr_end, stdout);
}
|