text plugin: add color support
[babeltrace.git] / common / common.c
CommitLineData
1670bffd
PP
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
290725f7 25#include <unistd.h>
1670bffd
PP
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>
ad96d936 33#include <babeltrace/common-internal.h>
1670bffd
PP
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
39BT_HIDDEN
40const char *bt_common_get_system_plugin_path(void)
41{
42 return SYSTEM_PLUGIN_PATH;
43}
44
45BT_HIDDEN
46bool bt_common_is_setuid_setgid(void)
47{
48 return (geteuid() != getuid() || getegid() != getgid());
49}
50
51static 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
61static 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;
76end:
77 return val;
78}
79
80BT_HIDDEN
81char *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
105end:
106 return path;
107}
108
109BT_HIDDEN
110int 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
157error:
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
165end:
166 return ret;
167}
290725f7 168
ad96d936
PP
169BT_HIDDEN
170bool bt_common_colors_supported(void)
290725f7
PP
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
200end:
201 return supports_colors;
202}
203
204BT_HIDDEN
205const char *bt_common_color_reset(void)
206{
ad96d936 207 return bt_common_colors_supported() ? BT_COMMON_COLOR_RESET : "";
290725f7
PP
208}
209
210BT_HIDDEN
211const char *bt_common_color_bold(void)
212{
ad96d936 213 return bt_common_colors_supported() ? BT_COMMON_COLOR_BOLD : "";
290725f7
PP
214}
215
216BT_HIDDEN
217const char *bt_common_color_fg_default(void)
218{
ad96d936 219 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_DEFAULT : "";
290725f7
PP
220}
221
222BT_HIDDEN
223const char *bt_common_color_fg_red(void)
224{
ad96d936 225 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_RED : "";
290725f7
PP
226}
227
228BT_HIDDEN
229const char *bt_common_color_fg_green(void)
230{
ad96d936 231 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_GREEN : "";
290725f7
PP
232}
233
234BT_HIDDEN
235const char *bt_common_color_fg_yellow(void)
236{
ad96d936 237 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_YELLOW : "";
290725f7
PP
238}
239
240BT_HIDDEN
241const char *bt_common_color_fg_blue(void)
242{
ad96d936 243 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_BLUE : "";
290725f7
PP
244}
245
246BT_HIDDEN
247const char *bt_common_color_fg_magenta(void)
248{
ad96d936 249 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_MAGENTA : "";
290725f7
PP
250}
251
252BT_HIDDEN
253const char *bt_common_color_fg_cyan(void)
254{
ad96d936 255 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_CYAN : "";
290725f7
PP
256}
257
258BT_HIDDEN
259const char *bt_common_color_fg_light_gray(void)
260{
ad96d936
PP
261 return bt_common_colors_supported() ?
262 BT_COMMON_COLOR_FG_LIGHT_GRAY : "";
290725f7
PP
263}
264
265BT_HIDDEN
266const char *bt_common_color_bg_default(void)
267{
ad96d936 268 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_DEFAULT : "";
290725f7
PP
269}
270
271BT_HIDDEN
272const char *bt_common_color_bg_red(void)
273{
ad96d936 274 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_RED : "";
290725f7
PP
275}
276
277BT_HIDDEN
278const char *bt_common_color_bg_green(void)
279{
ad96d936 280 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_GREEN : "";
290725f7
PP
281}
282
283BT_HIDDEN
284const char *bt_common_color_bg_yellow(void)
285{
ad96d936 286 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_YELLOW : "";
290725f7
PP
287}
288
289BT_HIDDEN
290const char *bt_common_color_bg_blue(void)
291{
ad96d936 292 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_BLUE : "";
290725f7
PP
293}
294
295BT_HIDDEN
296const char *bt_common_color_bg_magenta(void)
297{
ad96d936 298 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_MAGENTA : "";
290725f7
PP
299}
300
301BT_HIDDEN
302const char *bt_common_color_bg_cyan(void)
303{
ad96d936 304 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_CYAN : "";
290725f7
PP
305}
306
307BT_HIDDEN
308const char *bt_common_color_bg_light_gray(void)
309{
ad96d936
PP
310 return bt_common_colors_supported() ?
311 BT_COMMON_COLOR_BG_LIGHT_GRAY : "";
290725f7 312}
This page took 0.03497 seconds and 4 git commands to generate.