text plugin: add color support
[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 goto end;
192 }
193
194 if (!isatty(1)) {
195 goto end;
196 }
197
198 supports_colors = true;
199
200 end:
201 return supports_colors;
202 }
203
204 BT_HIDDEN
205 const char *bt_common_color_reset(void)
206 {
207 return bt_common_colors_supported() ? BT_COMMON_COLOR_RESET : "";
208 }
209
210 BT_HIDDEN
211 const char *bt_common_color_bold(void)
212 {
213 return bt_common_colors_supported() ? BT_COMMON_COLOR_BOLD : "";
214 }
215
216 BT_HIDDEN
217 const char *bt_common_color_fg_default(void)
218 {
219 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_DEFAULT : "";
220 }
221
222 BT_HIDDEN
223 const char *bt_common_color_fg_red(void)
224 {
225 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_RED : "";
226 }
227
228 BT_HIDDEN
229 const char *bt_common_color_fg_green(void)
230 {
231 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_GREEN : "";
232 }
233
234 BT_HIDDEN
235 const char *bt_common_color_fg_yellow(void)
236 {
237 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_YELLOW : "";
238 }
239
240 BT_HIDDEN
241 const char *bt_common_color_fg_blue(void)
242 {
243 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_BLUE : "";
244 }
245
246 BT_HIDDEN
247 const char *bt_common_color_fg_magenta(void)
248 {
249 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_MAGENTA : "";
250 }
251
252 BT_HIDDEN
253 const char *bt_common_color_fg_cyan(void)
254 {
255 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_CYAN : "";
256 }
257
258 BT_HIDDEN
259 const char *bt_common_color_fg_light_gray(void)
260 {
261 return bt_common_colors_supported() ?
262 BT_COMMON_COLOR_FG_LIGHT_GRAY : "";
263 }
264
265 BT_HIDDEN
266 const char *bt_common_color_bg_default(void)
267 {
268 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_DEFAULT : "";
269 }
270
271 BT_HIDDEN
272 const char *bt_common_color_bg_red(void)
273 {
274 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_RED : "";
275 }
276
277 BT_HIDDEN
278 const char *bt_common_color_bg_green(void)
279 {
280 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_GREEN : "";
281 }
282
283 BT_HIDDEN
284 const char *bt_common_color_bg_yellow(void)
285 {
286 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_YELLOW : "";
287 }
288
289 BT_HIDDEN
290 const char *bt_common_color_bg_blue(void)
291 {
292 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_BLUE : "";
293 }
294
295 BT_HIDDEN
296 const char *bt_common_color_bg_magenta(void)
297 {
298 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_MAGENTA : "";
299 }
300
301 BT_HIDDEN
302 const char *bt_common_color_bg_cyan(void)
303 {
304 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_CYAN : "";
305 }
306
307 BT_HIDDEN
308 const char *bt_common_color_bg_light_gray(void)
309 {
310 return bt_common_colors_supported() ?
311 BT_COMMON_COLOR_BG_LIGHT_GRAY : "";
312 }
This page took 0.035991 seconds and 5 git commands to generate.