-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.c
323 lines (242 loc) · 8.51 KB
/
colors.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* colors.c */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <regex.h>
#include <dlfcn.h>
#include <errno.h>
#include <math.h>
/* Declare a hooked function. */
#define DECLARE_HOOK(name, returns, ...) \
static returns (*__orig_##name)(__VA_ARGS__) = NULL
/* Initialize a hooked function. */
#define INITIALIZE_HOOK(name) \
do { \
*(void **)(&__orig_##name) = dlsym(RTLD_NEXT, #name); \
if (__orig_##name == NULL) { \
errno = ENOSYS; \
abort(); \
} \
} while(0)
/* The name of the running program. */
extern char *program_invocation_short_name;
/* A blacklist of programs we can't break, yet. */
static regex_t blacklist_regex;
/* Strip all ANSI color codes. */
static regex_t ansi_escape_regex;
/* Bring in magic, we're going to need it. */
extern unsigned char __magic[];
extern unsigned int __magic_len;
DECLARE_HOOK(write, ssize_t, int, const void *, size_t);
DECLARE_HOOK(__printf_chk, int, int, const char *, ...);
DECLARE_HOOK(printf, int, const char *, ...);
DECLARE_HOOK(fwrite, size_t, const void *, size_t, size_t, FILE *);
DECLARE_HOOK(fwrite_unlocked, size_t, const void *, size_t, size_t, FILE *);
DECLARE_HOOK(__fprintf_chk, int, FILE *, int, const char *, ...);
DECLARE_HOOK(vfprintf, int, FILE *, const char *, va_list);
DECLARE_HOOK(fprintf, int, FILE *, const char *, ...);
DECLARE_HOOK(fputc, int, int, FILE *);
DECLARE_HOOK(fputc_unlocked, int, int, FILE *);
DECLARE_HOOK(fputs, int, const char *, FILE *);
DECLARE_HOOK(fputs_unlocked, int, const char *, FILE *);
DECLARE_HOOK(putchar, int, int);
DECLARE_HOOK(puts, int, const char *);
/* Predicate function for allowing colorization. */
static unsigned int should_colorize(const int fd) {
const char *bypass = getenv("COLOR_ME_SHOCKED");
/* Only colorize interactive terminals. */
if (isatty(fd) == 0)
return 0;
/* If the bypass is enabled, skip colorization. */
if (bypass != NULL && strcmp(bypass, "1") == 0)
return 0;
/* We should only ever need to apply this to stderr/stdout. */
if (fd != STDOUT_FILENO && fd != STDERR_FILENO)
return 0;
/* Don't colorize specific blacklisted programs. No guarantees. */
if (regexec(&blacklist_regex, program_invocation_short_name, 0, NULL, 0) == 0)
return 0;
return 1;
}
/* Set the terminal output color. */
static ssize_t set_terminal_color(const int fd, const double frequency, const double i) {
unsigned char buffer[16];
size_t length = 0;
/* Calculate the wave rotation frequency. */
double base = (frequency * i);
/* Generate three cyclic waves with varying phases. */
double red = sin(base + 0 * M_PI / 3) * 127 + 128;
double green = sin(base + 2 * M_PI / 3) * 127 + 128;
double blue = sin(base + 4 * M_PI / 3) * 127 + 128;
/* Find the red/green/blue indexes into the ANSI colors. */
unsigned char ri = (unsigned char)(6 * (red / 256.0));
unsigned char gi = (unsigned char)(6 * (green / 256.0));
unsigned char bi = (unsigned char)(6 * (blue / 256.0));
/* Derive the ANSI color code from the indexes. */
unsigned char code = ((36 * ri) + (6 * gi) + bi) + 16;
/* Print the code into a buffer. */
length = snprintf(buffer, sizeof buffer, "\x1b[38;5;%hhum", code);
/* Write the code to the fd, bypassing modifications. */
return (*__orig_write)(fd, buffer, length);
}
/* Reset the terminal to use normal color. */
static ssize_t reset_terminal_color(const int fd) {
unsigned const char *reset = "\x1b[0;00m";
return (*__orig_write)(fd, reset, 7);
}
/* Skip to the next byte after an ANSI escape code. */
static size_t skip_ansi_color_codes(const char *p) {
regmatch_t match;
/* Use a regex to find the next byte after an ANSI color escape. */
if (regexec(&ansi_escape_regex, p, 1, &match, 0) == 0) {
return match.rm_eo;
}
/* No match. */
return 0;
}
ssize_t write(int fd, const void *buffer, size_t count) {
unsigned char *p = (unsigned char *) buffer;
size_t skip = 0, written = 0;
if (count == 0)
return 0;
if (should_colorize(fd)) {
do {
set_terminal_color(fd, 0.25, written);
skip = skip_ansi_color_codes(p + written);
written += (skip == 0) ? (*__orig_write)(fd, p + written, 1) : skip;
} while(written < count);
reset_terminal_color(fd);
return written;
}
return (*__orig_write)(fd, buffer, count);
}
size_t fwrite(const void *data, size_t size, size_t count, FILE *stream) {
unsigned char *p = (unsigned char *) data;
const int fd = fileno(stream);
ssize_t skip = 0, written = 0;
if ((size * count) == 0)
return 0;
if (should_colorize(fd)) {
do {
set_terminal_color(fd, 0.25, written);
skip = skip_ansi_color_codes(p + written);
written += (skip == 0) ? (*__orig_fwrite)(p + written, 1, 1, stream) : skip;
fflush(stream);
} while(written < (size * count));
reset_terminal_color(fd);
return written;
}
return (*__orig_fwrite)(data, size, count, stream);
}
size_t fwrite_unlocked(const void *data, size_t size, size_t count, FILE *stream) {
unsigned char *p = (unsigned char *) data;
const int fd = fileno(stream);
ssize_t skip = 0, written = 0;
if ((size * count) == 0)
return 0;
if (should_colorize(fd)) {
do {
set_terminal_color(fd, 0.25, written);
skip = skip_ansi_color_codes(p + written);
written += (skip == 0) ? (*__orig_fwrite_unlocked)(p + written, 1, 1, stream) : skip;
fflush(stream);
} while(written < (size * count));
reset_terminal_color(fd);
return written;
}
return (*__orig_fwrite_unlocked)(data, size, count, stream);
}
int vfprintf(FILE *stream, const char *format, va_list ap) {
char *buffer = NULL;
size_t written = -1;
if (vasprintf(&buffer, format, ap) > 0) {
written = (*fwrite)(buffer, 1, strlen(buffer), stream);
free(buffer);
}
return written;
}
int __fprintf_chk(FILE *stream, int flags, const char *format, ...) {
va_list args;
int result;
va_start(args, format);
result = (*vfprintf)(stream, format, args);
va_end(args);
return result;
}
int fprintf(FILE *stream, const char *format, ...) {
va_list args;
int result;
va_start(args, format);
result = (*vfprintf)(stream, format, args);
va_end(args);
return result;
}
int __printf_chk(int flags, const char *format, ...) {
va_list args;
int result;
va_start(args, format);
result = (*vfprintf)(stdout, format, args);
va_end(args);
return result;
}
int printf(const char *format, ...) {
va_list args;
int result;
va_start(args, format);
result = (*vfprintf)(stdout, format, args);
va_end(args);
return result;
}
int fputc(int chr, FILE *stream) {
const unsigned char c = (unsigned char) chr;
return (*fwrite)(&c, 1, 1, stream);
}
int fputc_unlocked(int chr, FILE *stream) {
const unsigned char c = (unsigned char) chr;
return (*fwrite_unlocked)(&c, 1, 1, stream);
}
int fputs(const char *str, FILE *stream) {
return (*fwrite)(str, 1, strlen(str), stream);
}
int fputs_unlocked(const char *str, FILE *stream) {
return (*fwrite_unlocked)(str, 1, strlen(str), stream);
}
int puts(const char *s) {
int written = (*fputs)(s, stdout);
(*fputc)('\n', stdout); /* WAT */
return written;
}
int putchar(int c) {
return (*fputc)(c, stdout);
}
__attribute__((constructor))
static void colorify(void) {
/* All programs to blacklist from colorization at runtime. */
regcomp(&blacklist_regex, "^(less|pager|vi)$", REG_NOSUB | REG_EXTENDED);
/* Compile a regexp to match ANSI color escape codes. */
regcomp(&ansi_escape_regex, "^(\x1b\\[[0-9;]+[mK])", REG_EXTENDED);
INITIALIZE_HOOK(write);
INITIALIZE_HOOK(__printf_chk);
INITIALIZE_HOOK(printf);
INITIALIZE_HOOK(fwrite);
INITIALIZE_HOOK(fwrite_unlocked);
INITIALIZE_HOOK(__fprintf_chk);
INITIALIZE_HOOK(vfprintf);
INITIALIZE_HOOK(fprintf);
INITIALIZE_HOOK(fputc);
INITIALIZE_HOOK(fputc_unlocked);
INITIALIZE_HOOK(fputs);
INITIALIZE_HOOK(fputs_unlocked);
INITIALIZE_HOOK(putchar);
INITIALIZE_HOOK(puts);
}
void _start(void) {
INITIALIZE_HOOK(write);
/* The magics. Let me show you them. */
(*__orig_write)(STDOUT_FILENO, &__magic, __magic_len);
_exit(9002);
}