xref: /openbmc/linux/tools/perf/util/color_config.c (revision fa0d9846)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/kernel.h>
3 #include <subcmd/pager.h>
4 #include <string.h>
5 #include "config.h"
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include "color.h"
9 #include <math.h>
10 #include <unistd.h>
11 
perf_config_colorbool(const char * var,const char * value,int stdout_is_tty)12 int perf_config_colorbool(const char *var, const char *value, int stdout_is_tty)
13 {
14 	if (value) {
15 		if (!strcasecmp(value, "never"))
16 			return 0;
17 		if (!strcasecmp(value, "always"))
18 			return 1;
19 		if (!strcasecmp(value, "auto"))
20 			goto auto_color;
21 	}
22 
23 	/* Missing or explicit false to turn off colorization */
24 	if (!perf_config_bool(var, value))
25 		return 0;
26 
27 	/* any normal truth value defaults to 'auto' */
28  auto_color:
29 	if (stdout_is_tty < 0)
30 		stdout_is_tty = isatty(1);
31 	if (stdout_is_tty || pager_in_use()) {
32 		char *term = getenv("TERM");
33 		if (term && strcmp(term, "dumb"))
34 			return 1;
35 	}
36 	return 0;
37 }
38 
perf_color_default_config(const char * var,const char * value,void * cb __maybe_unused)39 int perf_color_default_config(const char *var, const char *value,
40 			      void *cb __maybe_unused)
41 {
42 	if (!strcmp(var, "color.ui")) {
43 		perf_use_color_default = perf_config_colorbool(var, value, -1);
44 		return 0;
45 	}
46 
47 	return 0;
48 }
49