common.c: bt_common_colors_supported(): add a few supported terminal prefixes
[babeltrace.git] / common / common.c
1 /*
2 * Babeltrace common functions
3 *
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include <unistd.h>
26 #include <string.h>
27 #include <sys/types.h>
28 #include <pwd.h>
29 #include <unistd.h>
30 #include <assert.h>
31 #include <glib.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/common-internal.h>
34
35 #define SYSTEM_PLUGIN_PATH INSTALL_LIBDIR "/babeltrace/plugins"
36 #define HOME_ENV_VAR "HOME"
37 #define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace/plugins"
38
39 BT_HIDDEN
40 const char *bt_common_get_system_plugin_path(void)
41 {
42 return SYSTEM_PLUGIN_PATH;
43 }
44
45 BT_HIDDEN
46 bool bt_common_is_setuid_setgid(void)
47 {
48 return (geteuid() != getuid() || getegid() != getgid());
49 }
50
51 static char *bt_secure_getenv(const char *name)
52 {
53 if (bt_common_is_setuid_setgid()) {
54 printf_error("Disregarding %s environment variable for setuid/setgid binary",
55 name);
56 return NULL;
57 }
58 return getenv(name);
59 }
60
61 static const char *get_home_dir(void)
62 {
63 char *val = NULL;
64 struct passwd *pwd;
65
66 val = bt_secure_getenv(HOME_ENV_VAR);
67 if (val) {
68 goto end;
69 }
70 /* Fallback on password file. */
71 pwd = getpwuid(getuid());
72 if (!pwd) {
73 goto end;
74 }
75 val = pwd->pw_dir;
76 end:
77 return val;
78 }
79
80 BT_HIDDEN
81 char *bt_common_get_home_plugin_path(void)
82 {
83 char *path = NULL;
84 const char *home_dir;
85
86 home_dir = get_home_dir();
87 if (!home_dir) {
88 goto end;
89 }
90
91 if (strlen(home_dir) + strlen(HOME_PLUGIN_SUBPATH) + 1 >= PATH_MAX) {
92 printf_error("Home directory path is too long: `%s`\n",
93 home_dir);
94 goto end;
95 }
96
97 path = malloc(PATH_MAX);
98 if (!path) {
99 goto end;
100 }
101
102 strcpy(path, home_dir);
103 strcat(path, HOME_PLUGIN_SUBPATH);
104
105 end:
106 return path;
107 }
108
109 BT_HIDDEN
110 int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs)
111 {
112 int ret = 0;
113 const char *at;
114 const char *end;
115 size_t init_dirs_len;
116
117 assert(dirs);
118 init_dirs_len = dirs->len;
119
120 if (!paths) {
121 /* Nothing to append */
122 goto end;
123 }
124
125 at = paths;
126 end = paths + strlen(paths);
127
128 while (at < end) {
129 GString *path;
130 const char *next_colon;
131
132 next_colon = strchr(at, ':');
133 if (next_colon == at) {
134 /*
135 * Empty path: try next character (supported
136 * to conform to the typical parsing of $PATH).
137 */
138 at++;
139 continue;
140 } else if (!next_colon) {
141 /* No more colon: use the remaining */
142 next_colon = paths + strlen(paths);
143 }
144
145 path = g_string_new(NULL);
146 if (!path) {
147 goto error;
148 }
149
150 g_string_append_len(path, at, next_colon - at);
151 at = next_colon + 1;
152 g_ptr_array_add(dirs, path);
153 }
154
155 goto end;
156
157 error:
158 ret = -1;
159
160 /* Remove the new entries in dirs */
161 while (dirs->len > init_dirs_len) {
162 g_ptr_array_remove_index(dirs, init_dirs_len);
163 }
164
165 end:
166 return ret;
167 }
168
169 BT_HIDDEN
170 bool bt_common_colors_supported(void)
171 {
172 static bool supports_colors = false;
173 static bool supports_colors_set = false;
174 const char *term;
175
176 if (supports_colors_set) {
177 goto end;
178 }
179
180 supports_colors_set = true;
181
182 term = getenv("TERM");
183 if (!term) {
184 goto end;
185 }
186
187 if (strncmp(term, "xterm", 5) != 0 &&
188 strncmp(term, "rxvt", 4) != 0 &&
189 strncmp(term, "konsole", 7) != 0 &&
190 strncmp(term, "gnome", 5) != 0 &&
191 strncmp(term, "screen", 5) != 0 &&
192 strncmp(term, "tmux", 4) != 0 &&
193 strncmp(term, "putty", 5) != 0) {
194 goto end;
195 }
196
197 if (!isatty(1)) {
198 goto end;
199 }
200
201 supports_colors = true;
202
203 end:
204 return supports_colors;
205 }
206
207 BT_HIDDEN
208 const char *bt_common_color_reset(void)
209 {
210 return bt_common_colors_supported() ? BT_COMMON_COLOR_RESET : "";
211 }
212
213 BT_HIDDEN
214 const char *bt_common_color_bold(void)
215 {
216 return bt_common_colors_supported() ? BT_COMMON_COLOR_BOLD : "";
217 }
218
219 BT_HIDDEN
220 const char *bt_common_color_fg_default(void)
221 {
222 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_DEFAULT : "";
223 }
224
225 BT_HIDDEN
226 const char *bt_common_color_fg_red(void)
227 {
228 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_RED : "";
229 }
230
231 BT_HIDDEN
232 const char *bt_common_color_fg_green(void)
233 {
234 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_GREEN : "";
235 }
236
237 BT_HIDDEN
238 const char *bt_common_color_fg_yellow(void)
239 {
240 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_YELLOW : "";
241 }
242
243 BT_HIDDEN
244 const char *bt_common_color_fg_blue(void)
245 {
246 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_BLUE : "";
247 }
248
249 BT_HIDDEN
250 const char *bt_common_color_fg_magenta(void)
251 {
252 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_MAGENTA : "";
253 }
254
255 BT_HIDDEN
256 const char *bt_common_color_fg_cyan(void)
257 {
258 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_CYAN : "";
259 }
260
261 BT_HIDDEN
262 const char *bt_common_color_fg_light_gray(void)
263 {
264 return bt_common_colors_supported() ?
265 BT_COMMON_COLOR_FG_LIGHT_GRAY : "";
266 }
267
268 BT_HIDDEN
269 const char *bt_common_color_bg_default(void)
270 {
271 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_DEFAULT : "";
272 }
273
274 BT_HIDDEN
275 const char *bt_common_color_bg_red(void)
276 {
277 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_RED : "";
278 }
279
280 BT_HIDDEN
281 const char *bt_common_color_bg_green(void)
282 {
283 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_GREEN : "";
284 }
285
286 BT_HIDDEN
287 const char *bt_common_color_bg_yellow(void)
288 {
289 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_YELLOW : "";
290 }
291
292 BT_HIDDEN
293 const char *bt_common_color_bg_blue(void)
294 {
295 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_BLUE : "";
296 }
297
298 BT_HIDDEN
299 const char *bt_common_color_bg_magenta(void)
300 {
301 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_MAGENTA : "";
302 }
303
304 BT_HIDDEN
305 const char *bt_common_color_bg_cyan(void)
306 {
307 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_CYAN : "";
308 }
309
310 BT_HIDDEN
311 const char *bt_common_color_bg_light_gray(void)
312 {
313 return bt_common_colors_supported() ?
314 BT_COMMON_COLOR_BG_LIGHT_GRAY : "";
315 }
This page took 0.038613 seconds and 5 git commands to generate.