111
|
1 /* Output colorization.
|
|
2 Copyright (C) 2011-2017 Free Software Foundation, Inc.
|
|
3
|
|
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.
|
|
8
|
|
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.
|
|
13
|
|
14 You should have received a copy of the GNU General Public License
|
|
15 along with this program; if not, write to the Free Software
|
|
16 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
|
|
17 02110-1301, USA. */
|
|
18
|
|
19 #include "config.h"
|
|
20 #include "system.h"
|
|
21 #include "diagnostic-color.h"
|
|
22
|
|
23 #ifdef __MINGW32__
|
|
24 # include <windows.h>
|
|
25 #endif
|
|
26
|
|
27 /* Select Graphic Rendition (SGR, "\33[...m") strings. */
|
|
28 /* Also Erase in Line (EL) to Right ("\33[K") by default. */
|
|
29 /* Why have EL to Right after SGR?
|
|
30 -- The behavior of line-wrapping when at the bottom of the
|
|
31 terminal screen and at the end of the current line is often
|
|
32 such that a new line is introduced, entirely cleared with
|
|
33 the current background color which may be different from the
|
|
34 default one (see the boolean back_color_erase terminfo(5)
|
|
35 capability), thus scrolling the display by one line.
|
|
36 The end of this new line will stay in this background color
|
|
37 even after reverting to the default background color with
|
|
38 "\33[m', unless it is explicitly cleared again with "\33[K"
|
|
39 (which is the behavior the user would instinctively expect
|
|
40 from the whole thing). There may be some unavoidable
|
|
41 background-color flicker at the end of this new line because
|
|
42 of this (when timing with the monitor's redraw is just right).
|
|
43 -- The behavior of HT (tab, "\t") is usually the same as that of
|
|
44 Cursor Forward Tabulation (CHT) with a default parameter
|
|
45 of 1 ("\33[I"), i.e., it performs pure movement to the next
|
|
46 tab stop, without any clearing of either content or screen
|
|
47 attributes (including background color); try
|
|
48 printf 'asdfqwerzxcv\rASDF\tZXCV\n'
|
|
49 in a bash(1) shell to demonstrate this. This is not what the
|
|
50 user would instinctively expect of HT (but is ok for CHT).
|
|
51 The instinctive behavior would include clearing the terminal
|
|
52 cells that are skipped over by HT with blank cells in the
|
|
53 current screen attributes, including background color;
|
|
54 the boolean dest_tabs_magic_smso terminfo(5) capability
|
|
55 indicates this saner behavior for HT, but only some rare
|
|
56 terminals have it (although it also indicates a special
|
|
57 glitch with standout mode in the Teleray terminal for which
|
|
58 it was initially introduced). The remedy is to add "\33K"
|
|
59 after each SGR sequence, be it START (to fix the behavior
|
|
60 of any HT after that before another SGR) or END (to fix the
|
|
61 behavior of an HT in default background color that would
|
|
62 follow a line-wrapping at the bottom of the screen in another
|
|
63 background color, and to complement doing it after START).
|
|
64 Piping GCC's output through a pager such as less(1) avoids
|
|
65 any HT problems since the pager performs tab expansion.
|
|
66
|
|
67 Generic disadvantages of this remedy are:
|
|
68 -- Some very rare terminals might support SGR but not EL (nobody
|
|
69 will use "gcc -fdiagnostics-color" on a terminal that does not
|
|
70 support SGR in the first place).
|
|
71 -- Having these extra control sequences might somewhat complicate
|
|
72 the task of any program trying to parse "gcc -fdiagnostics-color"
|
|
73 output in order to extract structuring information from it.
|
|
74 A specific disadvantage to doing it after SGR START is:
|
|
75 -- Even more possible background color flicker (when timing
|
|
76 with the monitor's redraw is just right), even when not at the
|
|
77 bottom of the screen.
|
|
78 There are no additional disadvantages specific to doing it after
|
|
79 SGR END.
|
|
80
|
|
81 It would be impractical for GCC to become a full-fledged
|
|
82 terminal program linked against ncurses or the like, so it will
|
|
83 not detect terminfo(5) capabilities. */
|
|
84 #define COLOR_SEPARATOR ";"
|
|
85 #define COLOR_NONE "00"
|
|
86 #define COLOR_BOLD "01"
|
|
87 #define COLOR_UNDERSCORE "04"
|
|
88 #define COLOR_BLINK "05"
|
|
89 #define COLOR_REVERSE "07"
|
|
90 #define COLOR_FG_BLACK "30"
|
|
91 #define COLOR_FG_RED "31"
|
|
92 #define COLOR_FG_GREEN "32"
|
|
93 #define COLOR_FG_YELLOW "33"
|
|
94 #define COLOR_FG_BLUE "34"
|
|
95 #define COLOR_FG_MAGENTA "35"
|
|
96 #define COLOR_FG_CYAN "36"
|
|
97 #define COLOR_FG_WHITE "37"
|
|
98 #define COLOR_BG_BLACK "40"
|
|
99 #define COLOR_BG_RED "41"
|
|
100 #define COLOR_BG_GREEN "42"
|
|
101 #define COLOR_BG_YELLOW "43"
|
|
102 #define COLOR_BG_BLUE "44"
|
|
103 #define COLOR_BG_MAGENTA "45"
|
|
104 #define COLOR_BG_CYAN "46"
|
|
105 #define COLOR_BG_WHITE "47"
|
|
106 #define SGR_START "\33["
|
|
107 #define SGR_END "m\33[K"
|
|
108 #define SGR_SEQ(str) SGR_START str SGR_END
|
|
109 #define SGR_RESET SGR_SEQ("")
|
|
110
|
|
111
|
|
112 /* The context and logic for choosing default --color screen attributes
|
|
113 (foreground and background colors, etc.) are the following.
|
|
114 -- There are eight basic colors available, each with its own
|
|
115 nominal luminosity to the human eye and foreground/background
|
|
116 codes (black [0 %, 30/40], blue [11 %, 34/44], red [30 %, 31/41],
|
|
117 magenta [41 %, 35/45], green [59 %, 32/42], cyan [70 %, 36/46],
|
|
118 yellow [89 %, 33/43], and white [100 %, 37/47]).
|
|
119 -- Sometimes, white as a background is actually implemented using
|
|
120 a shade of light gray, so that a foreground white can be visible
|
|
121 on top of it (but most often not).
|
|
122 -- Sometimes, black as a foreground is actually implemented using
|
|
123 a shade of dark gray, so that it can be visible on top of a
|
|
124 background black (but most often not).
|
|
125 -- Sometimes, more colors are available, as extensions.
|
|
126 -- Other attributes can be selected/deselected (bold [1/22],
|
|
127 underline [4/24], standout/inverse [7/27], blink [5/25], and
|
|
128 invisible/hidden [8/28]). They are sometimes implemented by
|
|
129 using colors instead of what their names imply; e.g., bold is
|
|
130 often achieved by using brighter colors. In practice, only bold
|
|
131 is really available to us, underline sometimes being mapped by
|
|
132 the terminal to some strange color choice, and standout best
|
|
133 being left for use by downstream programs such as less(1).
|
|
134 -- We cannot assume that any of the extensions or special features
|
|
135 are available for the purpose of choosing defaults for everyone.
|
|
136 -- The most prevalent default terminal backgrounds are pure black
|
|
137 and pure white, and are not necessarily the same shades of
|
|
138 those as if they were selected explicitly with SGR sequences.
|
|
139 Some terminals use dark or light pictures as default background,
|
|
140 but those are covered over by an explicit selection of background
|
|
141 color with an SGR sequence; their users will appreciate their
|
|
142 background pictures not be covered like this, if possible.
|
|
143 -- Some uses of colors attributes is to make some output items
|
|
144 more understated (e.g., context lines); this cannot be achieved
|
|
145 by changing the background color.
|
|
146 -- For these reasons, the GCC color defaults should strive not
|
|
147 to change the background color from its default, unless it's
|
|
148 for a short item that should be highlighted, not understated.
|
|
149 -- The GCC foreground color defaults (without an explicitly set
|
|
150 background) should provide enough contrast to be readable on any
|
|
151 terminal with either a black (dark) or white (light) background.
|
|
152 This only leaves red, magenta, green, and cyan (and their bold
|
|
153 counterparts) and possibly bold blue. */
|
|
154 /* Default colors. The user can overwrite them using environment
|
|
155 variable GCC_COLORS. */
|
|
156 struct color_cap
|
|
157 {
|
|
158 const char *name;
|
|
159 const char *val;
|
|
160 unsigned char name_len;
|
|
161 bool free_val;
|
|
162 };
|
|
163
|
|
164 /* For GCC_COLORS. */
|
|
165 static struct color_cap color_dict[] =
|
|
166 {
|
|
167 { "error", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED), 5, false },
|
|
168 { "warning", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_MAGENTA),
|
|
169 7, false },
|
|
170 { "note", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN), 4, false },
|
|
171 { "range1", SGR_SEQ (COLOR_FG_GREEN), 6, false },
|
|
172 { "range2", SGR_SEQ (COLOR_FG_BLUE), 6, false },
|
|
173 { "locus", SGR_SEQ (COLOR_BOLD), 5, false },
|
|
174 { "quote", SGR_SEQ (COLOR_BOLD), 5, false },
|
|
175 { "fixit-insert", SGR_SEQ (COLOR_FG_GREEN), 12, false },
|
|
176 { "fixit-delete", SGR_SEQ (COLOR_FG_RED), 12, false },
|
|
177 { "diff-filename", SGR_SEQ (COLOR_BOLD), 13, false },
|
|
178 { "diff-hunk", SGR_SEQ (COLOR_FG_CYAN), 9, false },
|
|
179 { "diff-delete", SGR_SEQ (COLOR_FG_RED), 11, false },
|
|
180 { "diff-insert", SGR_SEQ (COLOR_FG_GREEN), 11, false },
|
|
181 { "type-diff", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 9, false },
|
|
182 { NULL, NULL, 0, false }
|
|
183 };
|
|
184
|
|
185 const char *
|
|
186 colorize_start (bool show_color, const char *name, size_t name_len)
|
|
187 {
|
|
188 struct color_cap const *cap;
|
|
189
|
|
190 if (!show_color)
|
|
191 return "";
|
|
192
|
|
193 for (cap = color_dict; cap->name; cap++)
|
|
194 if (cap->name_len == name_len
|
|
195 && memcmp (cap->name, name, name_len) == 0)
|
|
196 break;
|
|
197 if (cap->name == NULL)
|
|
198 return "";
|
|
199
|
|
200 return cap->val;
|
|
201 }
|
|
202
|
|
203 const char *
|
|
204 colorize_stop (bool show_color)
|
|
205 {
|
|
206 return show_color ? SGR_RESET : "";
|
|
207 }
|
|
208
|
|
209 /* Parse GCC_COLORS. The default would look like:
|
|
210 GCC_COLORS='error=01;31:warning=01;35:note=01;36:\
|
|
211 range1=32:range2=34:locus=01:quote=01:\
|
|
212 fixit-insert=32:fixit-delete=31:'\
|
|
213 diff-filename=01:diff-hunk=32:diff-delete=31:diff-insert=32:\
|
|
214 type-diff=01;32'
|
|
215 No character escaping is needed or supported. */
|
|
216 static bool
|
|
217 parse_gcc_colors (void)
|
|
218 {
|
|
219 const char *p, *q, *name, *val;
|
|
220 char *b;
|
|
221 size_t name_len = 0, val_len = 0;
|
|
222
|
|
223 p = getenv ("GCC_COLORS"); /* Plural! */
|
|
224 if (p == NULL)
|
|
225 return true;
|
|
226 if (*p == '\0')
|
|
227 return false;
|
|
228
|
|
229 name = q = p;
|
|
230 val = NULL;
|
|
231 /* From now on, be well-formed or you're gone. */
|
|
232 for (;;)
|
|
233 if (*q == ':' || *q == '\0')
|
|
234 {
|
|
235 struct color_cap *cap;
|
|
236
|
|
237 if (val)
|
|
238 val_len = q - val;
|
|
239 else
|
|
240 name_len = q - name;
|
|
241 /* Empty name without val (empty cap)
|
|
242 won't match and will be ignored. */
|
|
243 for (cap = color_dict; cap->name; cap++)
|
|
244 if (cap->name_len == name_len
|
|
245 && memcmp (cap->name, name, name_len) == 0)
|
|
246 break;
|
|
247 /* If name unknown, go on for forward compatibility. */
|
|
248 if (cap->val && val)
|
|
249 {
|
|
250 if (cap->free_val)
|
|
251 free (CONST_CAST (char *, cap->val));
|
|
252 b = XNEWVEC (char, val_len + sizeof (SGR_SEQ ("")));
|
|
253 memcpy (b, SGR_START, strlen (SGR_START));
|
|
254 memcpy (b + strlen (SGR_START), val, val_len);
|
|
255 memcpy (b + strlen (SGR_START) + val_len, SGR_END,
|
|
256 sizeof (SGR_END));
|
|
257 cap->val = (const char *) b;
|
|
258 cap->free_val = true;
|
|
259 }
|
|
260 if (*q == '\0')
|
|
261 return true;
|
|
262 name = ++q;
|
|
263 val = NULL;
|
|
264 }
|
|
265 else if (*q == '=')
|
|
266 {
|
|
267 if (q == name || val)
|
|
268 return true;
|
|
269
|
|
270 name_len = q - name;
|
|
271 val = ++q; /* Can be the empty string. */
|
|
272 }
|
|
273 else if (val == NULL)
|
|
274 q++; /* Accumulate name. */
|
|
275 else if (*q == ';' || (*q >= '0' && *q <= '9'))
|
|
276 q++; /* Accumulate val. Protect the terminal from being sent
|
|
277 garbage. */
|
|
278 else
|
|
279 return true;
|
|
280 }
|
|
281
|
|
282 /* Return true if we should use color when in auto mode, false otherwise. */
|
|
283 static bool
|
|
284 should_colorize (void)
|
|
285 {
|
|
286 #ifdef __MINGW32__
|
|
287 /* For consistency reasons, one should check the handle returned by
|
|
288 _get_osfhandle(_fileno(stderr)) because the function
|
|
289 pp_write_text_to_stream() in pretty-print.c calls fputs() on
|
|
290 that stream. However, the code below for non-Windows doesn't seem
|
|
291 to care about it either... */
|
|
292 HANDLE h;
|
|
293 DWORD m;
|
|
294
|
|
295 h = GetStdHandle (STD_ERROR_HANDLE);
|
|
296 return (h != INVALID_HANDLE_VALUE) && (h != NULL)
|
|
297 && GetConsoleMode (h, &m);
|
|
298 #else
|
|
299 char const *t = getenv ("TERM");
|
|
300 return t && strcmp (t, "dumb") != 0 && isatty (STDERR_FILENO);
|
|
301 #endif
|
|
302 }
|
|
303
|
|
304 bool
|
|
305 colorize_init (diagnostic_color_rule_t rule)
|
|
306 {
|
|
307 switch (rule)
|
|
308 {
|
|
309 case DIAGNOSTICS_COLOR_NO:
|
|
310 return false;
|
|
311 case DIAGNOSTICS_COLOR_YES:
|
|
312 return parse_gcc_colors ();
|
|
313 case DIAGNOSTICS_COLOR_AUTO:
|
|
314 if (should_colorize ())
|
|
315 return parse_gcc_colors ();
|
|
316 else
|
|
317 return false;
|
|
318 default:
|
|
319 gcc_unreachable ();
|
|
320 }
|
|
321 }
|