struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / cpp / gcc / diagnostic-color.cc
blob968c0e7a88c5c27cc913aa456e63b4699427d5b7
1 /* Output colorization.
2 Copyright (C) 2011-2022 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, write to the Free Software
16 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
17 02110-1301, USA. */
19 #include "config.h"
20 #include "system.h"
21 #include "diagnostic-color.h"
22 #include "diagnostic-url.h"
24 #ifdef __MINGW32__
25 # define WIN32_LEAN_AND_MEAN
26 # include <windows.h>
27 #endif
29 #include "color-macros.h"
31 /* The context and logic for choosing default --color screen attributes
32 (foreground and background colors, etc.) are the following.
33 -- There are eight basic colors available, each with its own
34 nominal luminosity to the human eye and foreground/background
35 codes (black [0 %, 30/40], blue [11 %, 34/44], red [30 %, 31/41],
36 magenta [41 %, 35/45], green [59 %, 32/42], cyan [70 %, 36/46],
37 yellow [89 %, 33/43], and white [100 %, 37/47]).
38 -- Sometimes, white as a background is actually implemented using
39 a shade of light gray, so that a foreground white can be visible
40 on top of it (but most often not).
41 -- Sometimes, black as a foreground is actually implemented using
42 a shade of dark gray, so that it can be visible on top of a
43 background black (but most often not).
44 -- Sometimes, more colors are available, as extensions.
45 -- Other attributes can be selected/deselected (bold [1/22],
46 underline [4/24], standout/inverse [7/27], blink [5/25], and
47 invisible/hidden [8/28]). They are sometimes implemented by
48 using colors instead of what their names imply; e.g., bold is
49 often achieved by using brighter colors. In practice, only bold
50 is really available to us, underline sometimes being mapped by
51 the terminal to some strange color choice, and standout best
52 being left for use by downstream programs such as less(1).
53 -- We cannot assume that any of the extensions or special features
54 are available for the purpose of choosing defaults for everyone.
55 -- The most prevalent default terminal backgrounds are pure black
56 and pure white, and are not necessarily the same shades of
57 those as if they were selected explicitly with SGR sequences.
58 Some terminals use dark or light pictures as default background,
59 but those are covered over by an explicit selection of background
60 color with an SGR sequence; their users will appreciate their
61 background pictures not be covered like this, if possible.
62 -- Some uses of colors attributes is to make some output items
63 more understated (e.g., context lines); this cannot be achieved
64 by changing the background color.
65 -- For these reasons, the GCC color defaults should strive not
66 to change the background color from its default, unless it's
67 for a short item that should be highlighted, not understated.
68 -- The GCC foreground color defaults (without an explicitly set
69 background) should provide enough contrast to be readable on any
70 terminal with either a black (dark) or white (light) background.
71 This only leaves red, magenta, green, and cyan (and their bold
72 counterparts) and possibly bold blue. */
73 /* Default colors. The user can overwrite them using environment
74 variable GCC_COLORS. */
75 struct color_cap
77 const char *name;
78 const char *val;
79 unsigned char name_len;
80 bool free_val;
83 /* For GCC_COLORS. */
84 static struct color_cap color_dict[] =
86 { "error", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_RED), 5, false },
87 { "warning", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_MAGENTA),
88 7, false },
89 { "note", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN), 4, false },
90 { "range1", SGR_SEQ (COLOR_FG_GREEN), 6, false },
91 { "range2", SGR_SEQ (COLOR_FG_BLUE), 6, false },
92 { "locus", SGR_SEQ (COLOR_BOLD), 5, false },
93 { "quote", SGR_SEQ (COLOR_BOLD), 5, false },
94 { "path", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_CYAN), 4, false },
95 { "fixit-insert", SGR_SEQ (COLOR_FG_GREEN), 12, false },
96 { "fixit-delete", SGR_SEQ (COLOR_FG_RED), 12, false },
97 { "diff-filename", SGR_SEQ (COLOR_BOLD), 13, false },
98 { "diff-hunk", SGR_SEQ (COLOR_FG_CYAN), 9, false },
99 { "diff-delete", SGR_SEQ (COLOR_FG_RED), 11, false },
100 { "diff-insert", SGR_SEQ (COLOR_FG_GREEN), 11, false },
101 { "type-diff", SGR_SEQ (COLOR_BOLD COLOR_SEPARATOR COLOR_FG_GREEN), 9, false },
102 { NULL, NULL, 0, false }
105 const char *
106 colorize_start (bool show_color, const char *name, size_t name_len)
108 struct color_cap const *cap;
110 if (!show_color)
111 return "";
113 for (cap = color_dict; cap->name; cap++)
114 if (cap->name_len == name_len
115 && memcmp (cap->name, name, name_len) == 0)
116 break;
117 if (cap->name == NULL)
118 return "";
120 return cap->val;
123 const char *
124 colorize_stop (bool show_color)
126 return show_color ? SGR_RESET : "";
129 /* Parse GCC_COLORS. The default would look like:
130 GCC_COLORS='error=01;31:warning=01;35:note=01;36:\
131 range1=32:range2=34:locus=01:quote=01:path=01;36:\
132 fixit-insert=32:fixit-delete=31:'\
133 diff-filename=01:diff-hunk=32:diff-delete=31:diff-insert=32:\
134 type-diff=01;32'
135 No character escaping is needed or supported. */
136 static bool
137 parse_gcc_colors (void)
139 const char *p, *q, *name, *val;
140 char *b;
141 size_t name_len = 0, val_len = 0;
143 p = getenv ("GCC_COLORS"); /* Plural! */
144 if (p == NULL)
145 return true;
146 if (*p == '\0')
147 return false;
149 name = q = p;
150 val = NULL;
151 /* From now on, be well-formed or you're gone. */
152 for (;;)
153 if (*q == ':' || *q == '\0')
155 struct color_cap *cap;
157 if (val)
158 val_len = q - val;
159 else
160 name_len = q - name;
161 /* Empty name without val (empty cap)
162 won't match and will be ignored. */
163 for (cap = color_dict; cap->name; cap++)
164 if (cap->name_len == name_len
165 && memcmp (cap->name, name, name_len) == 0)
166 break;
167 /* If name unknown, go on for forward compatibility. */
168 if (cap->val && val)
170 if (cap->free_val)
171 free (CONST_CAST (char *, cap->val));
172 b = XNEWVEC (char, val_len + sizeof (SGR_SEQ ("")));
173 memcpy (b, SGR_START, strlen (SGR_START));
174 memcpy (b + strlen (SGR_START), val, val_len);
175 memcpy (b + strlen (SGR_START) + val_len, SGR_END,
176 sizeof (SGR_END));
177 cap->val = (const char *) b;
178 cap->free_val = true;
180 if (*q == '\0')
181 return true;
182 name = ++q;
183 val = NULL;
185 else if (*q == '=')
187 if (q == name || val)
188 return true;
190 name_len = q - name;
191 val = ++q; /* Can be the empty string. */
193 else if (val == NULL)
194 q++; /* Accumulate name. */
195 else if (*q == ';' || (*q >= '0' && *q <= '9'))
196 q++; /* Accumulate val. Protect the terminal from being sent
197 garbage. */
198 else
199 return true;
202 /* Return true if we should use color when in auto mode, false otherwise. */
203 static bool
204 should_colorize (void)
206 #ifdef __MINGW32__
207 /* For consistency reasons, one should check the handle returned by
208 _get_osfhandle(_fileno(stderr)) because the function
209 pp_write_text_to_stream() in pretty-print.cc calls fputs() on
210 that stream. However, the code below for non-Windows doesn't seem
211 to care about it either... */
212 HANDLE h;
213 DWORD m;
215 h = GetStdHandle (STD_ERROR_HANDLE);
216 return (h != INVALID_HANDLE_VALUE) && (h != NULL)
217 && GetConsoleMode (h, &m);
218 #else
219 char const *t = getenv ("TERM");
220 /* emacs M-x shell sets TERM="dumb". */
221 return t && strcmp (t, "dumb") != 0 && isatty (STDERR_FILENO);
222 #endif
225 bool
226 colorize_init (diagnostic_color_rule_t rule)
228 switch (rule)
230 case DIAGNOSTICS_COLOR_NO:
231 return false;
232 case DIAGNOSTICS_COLOR_YES:
233 return parse_gcc_colors ();
234 case DIAGNOSTICS_COLOR_AUTO:
235 if (should_colorize ())
236 return parse_gcc_colors ();
237 else
238 return false;
239 default:
240 gcc_unreachable ();
244 /* Return URL_FORMAT_XXX which tells how we should emit urls
245 when in always mode.
246 We use GCC_URLS and if that is not defined TERM_URLS.
247 If neither is defined the feature is enabled by default. */
249 static diagnostic_url_format
250 parse_env_vars_for_urls ()
252 const char *p;
254 p = getenv ("GCC_URLS"); /* Plural! */
255 if (p == NULL)
256 p = getenv ("TERM_URLS");
258 if (p == NULL)
259 return URL_FORMAT_DEFAULT;
261 if (*p == '\0')
262 return URL_FORMAT_NONE;
264 if (!strcmp (p, "no"))
265 return URL_FORMAT_NONE;
267 if (!strcmp (p, "st"))
268 return URL_FORMAT_ST;
270 if (!strcmp (p, "bel"))
271 return URL_FORMAT_BEL;
273 return URL_FORMAT_DEFAULT;
276 /* Return true if we should use urls when in auto mode, false otherwise. */
278 static bool
279 auto_enable_urls ()
281 #ifdef __MINGW32__
282 return false;
283 #else
284 const char *term, *colorterm;
286 /* First check the terminal is capable of printing color escapes,
287 if not URLs won't work either. */
288 if (!should_colorize ())
289 return false;
291 /* xfce4-terminal is known to not implement URLs at this time.
292 Recently new installations (0.8) will safely ignore the URL escape
293 sequences, but a large number of legacy installations (0.6.3) print
294 garbage when URLs are printed. Therefore we lose nothing by
295 disabling this feature for that specific terminal type. */
296 colorterm = getenv ("COLORTERM");
297 if (colorterm && !strcmp (colorterm, "xfce4-terminal"))
298 return false;
300 /* Old versions of gnome-terminal where URL escapes cause screen
301 corruptions set COLORTERM="gnome-terminal", recent versions
302 with working URL support set this to "truecolor". */
303 if (colorterm && !strcmp (colorterm, "gnome-terminal"))
304 return false;
306 /* Since the following checks are less specific than the ones
307 above, let GCC_URLS and TERM_URLS override the decision. */
308 if (getenv ("GCC_URLS") || getenv ("TERM_URLS"))
309 return true;
311 /* In an ssh session the COLORTERM is not there, but TERM=xterm
312 can be used as an indication of a incompatible terminal while
313 TERM=xterm-256color appears to be a working terminal. */
314 term = getenv ("TERM");
315 if (!colorterm && term && !strcmp (term, "xterm"))
316 return false;
318 /* When logging in a linux over serial line, we see TERM=linux
319 and no COLORTERM, it is unlikely that the URL escapes will
320 work in that environmen either. */
321 if (!colorterm && term && !strcmp (term, "linux"))
322 return false;
324 return true;
325 #endif
328 /* Determine if URLs should be enabled, based on RULE,
329 and, if so, which format to use.
330 This reuses the logic for colorization. */
332 diagnostic_url_format
333 determine_url_format (diagnostic_url_rule_t rule)
335 switch (rule)
337 case DIAGNOSTICS_URL_NO:
338 return URL_FORMAT_NONE;
339 case DIAGNOSTICS_URL_YES:
340 return parse_env_vars_for_urls ();
341 case DIAGNOSTICS_URL_AUTO:
342 if (auto_enable_urls ())
343 return parse_env_vars_for_urls ();
344 else
345 return URL_FORMAT_NONE;
346 default:
347 gcc_unreachable ();