xref: /dflybsd-src/contrib/grep/lib/colorize-posix.c (revision 91b9ed38d3db6a8a8ac5b66da1d43e6e331e259a)
1cf28ed85SJohn Marino /* Output colorization.
2*09d4459fSDaniel Fojt    Copyright 2011-2020 Free Software Foundation, Inc.
3cf28ed85SJohn Marino 
4cf28ed85SJohn Marino    This program is free software; you can redistribute it and/or modify
5cf28ed85SJohn Marino    it under the terms of the GNU General Public License as published by
6cf28ed85SJohn Marino    the Free Software Foundation; either version 3, or (at your option)
7cf28ed85SJohn Marino    any later version.
8cf28ed85SJohn Marino 
9cf28ed85SJohn Marino    This program is distributed in the hope that it will be useful,
10cf28ed85SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
11cf28ed85SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12cf28ed85SJohn Marino    GNU General Public License for more details.
13cf28ed85SJohn Marino 
14cf28ed85SJohn Marino    You should have received a copy of the GNU General Public License
15cf28ed85SJohn Marino    along with this program; if not, write to the Free Software
16cf28ed85SJohn Marino    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
17cf28ed85SJohn Marino    02110-1301, USA.  */
18cf28ed85SJohn Marino 
19cf28ed85SJohn Marino /* Without this pragma, gcc 4.7.0 20120102 suggests that the
20cf28ed85SJohn Marino    init_colorize function might be candidate for attribute 'const'  */
21cf28ed85SJohn Marino #if (__GNUC__ == 4 && 6 <= __GNUC_MINOR__) || 4 < __GNUC__
22cf28ed85SJohn Marino # pragma GCC diagnostic ignored "-Wsuggest-attribute=const"
23cf28ed85SJohn Marino #endif
24cf28ed85SJohn Marino 
25cf28ed85SJohn Marino #include <config.h>
26cf28ed85SJohn Marino 
27cf28ed85SJohn Marino #include "colorize.h"
28cf28ed85SJohn Marino 
29cf28ed85SJohn Marino #include <stdio.h>
30cf28ed85SJohn Marino #include <stdlib.h>
31cf28ed85SJohn Marino #include <string.h>
32cf28ed85SJohn Marino #include <unistd.h>
33cf28ed85SJohn Marino 
34cf28ed85SJohn Marino /* Return non-zero if we should highlight matches in output to file
35cf28ed85SJohn Marino    descriptor FD.  */
36cf28ed85SJohn Marino int
should_colorize(void)37cf28ed85SJohn Marino should_colorize (void)
38cf28ed85SJohn Marino {
39cf28ed85SJohn Marino   char const *t = getenv ("TERM");
40cf28ed85SJohn Marino   return t && strcmp (t, "dumb") != 0;
41cf28ed85SJohn Marino }
42cf28ed85SJohn Marino 
init_colorize(void)43cf28ed85SJohn Marino void init_colorize (void) { }
44cf28ed85SJohn Marino 
45cf28ed85SJohn Marino /* Start a colorized text attribute on stdout using the SGR_START
46cf28ed85SJohn Marino    format; the attribute is specified by SGR_SEQ.  */
47cf28ed85SJohn Marino void
print_start_colorize(char const * sgr_start,char const * sgr_seq)48cf28ed85SJohn Marino print_start_colorize (char const *sgr_start, char const *sgr_seq)
49cf28ed85SJohn Marino {
50cf28ed85SJohn Marino   printf (sgr_start, sgr_seq);
51cf28ed85SJohn Marino }
52cf28ed85SJohn Marino 
53cf28ed85SJohn Marino /* Restore the normal text attribute using the SGR_END string.  */
54cf28ed85SJohn Marino void
print_end_colorize(char const * sgr_end)55cf28ed85SJohn Marino print_end_colorize (char const *sgr_end)
56cf28ed85SJohn Marino {
57cf28ed85SJohn Marino   fputs (sgr_end, stdout);
58cf28ed85SJohn Marino }
59