doc: clarify a --help sentence
[grep.git] / lib / colorize-w32.c
blob347e59b117ab8f1313053cf9ac35207a2177c93a
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)
7 any later version.
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. */
19 #include <config.h>
21 #include "colorize.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #undef DATADIR /* conflicts with objidl.h, which is included by windows.h */
29 #include <windows.h>
31 static HANDLE hstdout = INVALID_HANDLE_VALUE;
32 static SHORT norm_attr;
34 /* Initialize the normal text attribute used by the console. */
35 void
36 init_colorize (void)
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;
44 else
45 hstdout = INVALID_HANDLE_VALUE;
48 /* Return non-zero if we should highlight matches in output. */
49 int
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
55 set TERM=dumb. */
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. */
66 static int
67 w32_sgr2attr (const char *sgr_seq)
69 const char *s, *p;
70 int code, fg = norm_attr & 15, bg = norm_attr & (15 << 4);
71 int bright = 0, inverse = 0;
72 static const int fg_color[] = {
73 0, /* black */
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[] = {
83 0, /* black */
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);
98 s = p + (*p != '\0');
100 switch (code)
102 case 0: /* all attributes off */
103 fg = norm_attr & 15;
104 bg = norm_attr & (15 << 4);
105 bright = 0;
106 inverse = 0;
107 break;
108 case 1: /* intensity on */
109 bright = 1;
110 break;
111 case 7: /* inverse video */
112 inverse = 1;
113 break;
114 case 22: /* intensity off */
115 bright = 0;
116 break;
117 case 27: /* inverse off */
118 inverse = 0;
119 break;
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];
123 break;
124 case 39: /* default foreground */
125 fg = norm_attr & 15;
126 break;
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];
130 break;
131 case 49: /* default background */
132 bg = norm_attr & (15 << 4);
133 break;
134 default:
135 break;
139 if (inverse)
141 int t = fg;
142 fg = (bg >> 4);
143 bg = (t << 4);
145 if (bright)
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. */
153 void
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
161 matches. */
162 if (hstdout != INVALID_HANDLE_VALUE)
164 SHORT attr = w32_sgr2attr (sgr_seq);
165 SetConsoleTextAttribute (hstdout, attr);
167 else
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
177 lines. */
178 static void
179 w32_clreol (void)
181 DWORD nchars;
182 COORD start_pos;
183 DWORD written;
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,
191 &written);
192 FillConsoleOutputCharacter (hstdout, ' ', nchars, start_pos, &written);
195 /* Restore the normal text attribute using the SGR_END string. */
196 void
197 print_end_colorize (char const *sgr_end)
199 if (hstdout != INVALID_HANDLE_VALUE)
201 SetConsoleTextAttribute (hstdout, norm_attr);
202 w32_clreol ();
204 else
205 fputs (sgr_end, stdout);