1 /* Output colorization on MS-Windows.
2 Copyright 2011-2025 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Eli Zaretskii. */
28 #undef DATADIR /* conflicts with objidl.h, which is included by windows.h */
31 static HANDLE hstdout
= INVALID_HANDLE_VALUE
;
32 static SHORT norm_attr
;
34 /* Initialize the normal text attribute used by the console. */
38 CONSOLE_SCREEN_BUFFER_INFO csbi
;
40 hstdout
= GetStdHandle (STD_OUTPUT_HANDLE
);
41 if (hstdout
!= INVALID_HANDLE_VALUE
42 && GetConsoleScreenBufferInfo (hstdout
, &csbi
))
43 norm_attr
= csbi
.wAttributes
;
45 hstdout
= INVALID_HANDLE_VALUE
;
48 /* Return non-zero if we should highlight matches in output. */
50 should_colorize (void)
52 /* $TERM is not normally defined on DOS/Windows, so don't require
53 it for highlighting. But some programs, like Emacs, do define
54 it when running Grep as a subprocess, so make sure they don't
56 char const *t
= getenv ("TERM");
57 return ! (t
&& strcmp (t
, "dumb") == 0);
60 /* Convert a color spec, a semi-colon separated list of the form
61 "NN;MM;KK;...", where each number is a value of the SGR parameter,
62 into the corresponding Windows console text attribute.
64 This function supports a subset of the SGR rendition aspects that
65 the Windows console can display. */
67 w32_sgr2attr (const char *sgr_seq
)
70 int code
, fg
= norm_attr
& 15, bg
= norm_attr
& (15 << 4);
71 int bright
= 0, inverse
= 0;
72 static const int fg_color
[] = {
74 FOREGROUND_RED
, /* red */
75 FOREGROUND_GREEN
, /* green */
76 FOREGROUND_GREEN
| FOREGROUND_RED
, /* yellow */
77 FOREGROUND_BLUE
, /* blue */
78 FOREGROUND_BLUE
| FOREGROUND_RED
, /* magenta */
79 FOREGROUND_BLUE
| FOREGROUND_GREEN
, /* cyan */
80 FOREGROUND_RED
| FOREGROUND_GREEN
| FOREGROUND_BLUE
/* gray */
82 static const int bg_color
[] = {
84 BACKGROUND_RED
, /* red */
85 BACKGROUND_GREEN
, /* green */
86 BACKGROUND_GREEN
| BACKGROUND_RED
, /* yellow */
87 BACKGROUND_BLUE
, /* blue */
88 BACKGROUND_BLUE
| BACKGROUND_RED
, /* magenta */
89 BACKGROUND_BLUE
| BACKGROUND_GREEN
, /* cyan */
90 BACKGROUND_RED
| BACKGROUND_GREEN
| BACKGROUND_BLUE
/* gray */
93 for (s
= p
= sgr_seq
; *s
; p
++)
95 if (*p
== ';' || *p
== '\0')
97 code
= strtol (s
, nullptr, 10);
102 case 0: /* all attributes off */
104 bg
= norm_attr
& (15 << 4);
108 case 1: /* intensity on */
111 case 7: /* inverse video */
114 case 22: /* intensity off */
117 case 27: /* inverse off */
120 case 30: case 31: case 32: case 33: /* foreground color */
121 case 34: case 35: case 36: case 37:
122 fg
= fg_color
[code
- 30];
124 case 39: /* default foreground */
127 case 40: case 41: case 42: case 43: /* background color */
128 case 44: case 45: case 46: case 47:
129 bg
= bg_color
[code
- 40];
131 case 49: /* default background */
132 bg
= norm_attr
& (15 << 4);
146 fg
|= FOREGROUND_INTENSITY
;
148 return (bg
& (15 << 4)) | (fg
& 15);
151 /* Start a colorized text attribute on stdout using the SGR_START
152 format; the attribute is specified by SGR_SEQ. */
154 print_start_colorize (char const *sgr_start
, char const *sgr_seq
)
156 /* If stdout is connected to a console, set the console text
157 attribute directly instead of using SGR_START. Otherwise, use
158 SGR_START to emit the SGR escape sequence as on Posix platforms;
159 this is needed when Grep is invoked as a subprocess of another
160 program, such as Emacs, which will handle the display of the
162 if (hstdout
!= INVALID_HANDLE_VALUE
)
164 SHORT attr
= w32_sgr2attr (sgr_seq
);
165 SetConsoleTextAttribute (hstdout
, attr
);
168 printf (sgr_start
, sgr_seq
);
171 /* Clear to the end of the current line with the default attribute.
172 This is needed for reasons similar to those that require the "EL to
173 Right after SGR" operation on Posix platforms: if we don't do this,
174 setting the 'mt', 'ms', or 'mc' capabilities to use a non-default
175 background color spills that color to the empty space at the end of
176 the last screen line in a match whose line spans multiple screen
184 CONSOLE_SCREEN_BUFFER_INFO csbi
;
186 GetConsoleScreenBufferInfo (hstdout
, &csbi
);
187 start_pos
= csbi
.dwCursorPosition
;
188 nchars
= csbi
.dwSize
.X
- start_pos
.X
;
190 FillConsoleOutputAttribute (hstdout
, norm_attr
, nchars
, start_pos
,
192 FillConsoleOutputCharacter (hstdout
, ' ', nchars
, start_pos
, &written
);
195 /* Restore the normal text attribute using the SGR_END string. */
197 print_end_colorize (char const *sgr_end
)
199 if (hstdout
!= INVALID_HANDLE_VALUE
)
201 SetConsoleTextAttribute (hstdout
, norm_attr
);
205 fputs (sgr_end
, stdout
);