forked from PerBothner/DomTerm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preloads.cc
183 lines (168 loc) · 5.46 KB
/
preloads.cc
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
//#define _GNU_SOURCE
// FIXME add dprintf writev
#include <stdio.h>
#include <dlfcn.h>
#include <sys/uio.h>
#include <unistd.h>
#include <stdarg.h>
#include <error.h>
#include <stdlib.h>
#include <termios.h>
#include <string.h>
// Should standard error be redirected to standard output, but
// wrapped with appropriate escape codes?
// This doesn't work completely/reliably, so it is disabled.
#define WRAP_STDERR 1
static char* domtermEnv = getenv("DOMTERM");
static int checkState = 0;
static bool inDomTerm()
{
if (checkState == 0)
{
//char *tname = ttyname(1);
checkState = 1; // FIXME
}
return checkState > 0;
}
static int checkStates[3];
static bool isDomTerm(int fd) {
if (checkStates[fd] == 0)
{
char *tname = ttyname(fd);
if (tname == nullptr || domtermEnv == nullptr)
{
checkStates[fd] = -1;
return false;
}
else if (strstr(domtermEnv, tname) != nullptr)
{
checkStates[fd] = 1;
}
}
return checkStates[fd] > 0;
}
int dup2(int oldfd, int newfd)
{
typedef int (*dup2_type)(int,int);
static dup2_type original_dup2 = (dup2_type) dlsym(RTLD_NEXT, "dup2");
int result = (*original_dup2)(oldfd,newfd);
if (newfd >= 0 && newfd <= 2) {
//SKIP = true;
checkState = 0;
checkStates[newfd] = 0;
//fprintf(stderr, "dup2 %d -> %d tty:%s\n", oldfd, newfd, ttyname(newfd));
}
return result;
}
#if WRAP_STDERR
#define ERR_START "\033[12u"
#define ERR_END "\033[11u"
#define ERR_START_LENGTH 5
#define ERR_END_LENGTH 5
typedef ssize_t (*writev_type)(int, const struct iovec *, int);
static writev_type original_writev = (writev_type) dlsym(RTLD_NEXT, "writev");
ssize_t write(int fd, const void *buf, size_t count)
{
typedef ssize_t (*write_type)(int,const void*,size_t);
static write_type original_write = (write_type) dlsym(RTLD_NEXT, "write");
if (! inDomTerm() || fd != 2)
return (*original_write)(fd, buf, count);
struct iovec iovs[3];
iovs[0].iov_base = (void*) ERR_START;
iovs[0].iov_len = ERR_START_LENGTH;
iovs[1].iov_base = (void*) buf;
iovs[1].iov_len = count;
iovs[2].iov_base = (void*) ERR_END;
iovs[2].iov_len = ERR_END_LENGTH;
return original_writev(1, iovs, 3);
}
ssize_t writev(int fd, const struct iovec *iov, int iovcnt)
{
if (! inDomTerm() || fd != 2 || iovcnt > 8)
return (*original_writev)(fd, iov, iovcnt);
struct iovec iovx[10];
iovx[0].iov_base = (void*) ERR_START;
iovx[0].iov_len = ERR_START_LENGTH;
for (int i = 0; i < iovcnt; i++) {
iovx[i+1].iov_base = iov[i].iov_base;
iovx[i+1].iov_len = iov[i].iov_len;
}
iovx[iovcnt+1].iov_base = (void*) ERR_END;
iovx[iovcnt+1].iov_len = ERR_END_LENGTH;
return original_writev(1, iovx, iovcnt+2);
}
#if 0
// This causes things to break quickly.
// No idea why, but it may be a bad idea to try to
// intercept stdio calls.
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
typedef size_t (*fwrite_type)(const void*,size_t, size_t, FILE*);
static fwrite_type original_fwrite = (fwrite_type) dlsym(RTLD_NEXT, "fwrite");
if (! isDomTerm(1) || ! isDomTerm(2) || fileno(stream) != 2 || fileno(stdout) != 1)
return (*original_fwrite)(ptr, size, nmemb, stream);
if ((*original_fwrite)(ERR_START, ERR_START_LENGTH, 1, stdout) != 1)
return 0;
size_t ret = (*original_fwrite)(ptr, size, nmemb, stdout);
if ((*original_fwrite)(ERR_END, ERR_END_LENGTH, 1, stdout) != 1)
return 0;
return ret;
}
#endif
int fprintf(FILE *stream, const char *format, ...)
{
va_list args;
va_start(args, format);
int result = vfprintf(stream, format, args);
va_end(args);
return result;
}
int vfprintf(FILE *stream, const char *format, va_list ap)
{
typedef int (*vfprintf_type)(FILE *stream, const char *format, va_list ap);
static vfprintf_type original_vfprintf = (vfprintf_type) dlsym(RTLD_NEXT, "vfprintf");
if (! inDomTerm() || stream != stderr)
return (*original_vfprintf)(stream, format, ap);
if (fprintf(stdout, "%s", ERR_START) <= 0)
return -1;
int result = (*original_vfprintf)(stdout, format, ap);
if (fprintf(stdout, "%s", ERR_END) <= 0)
return -1;
return result;
}
void error(int status, int errnum, const char *format, ...)
{
typedef void (*error_type)(int status, int errnum, const char *format, ...);
static error_type original_error = (error_type) dlsym(RTLD_NEXT, "error");
va_list ap;
va_start(ap, format);
int size = vsnprintf(nullptr, 0, format, ap) + 1;
va_end(ap);
char *msg = size <= 0 ? nullptr : (char*) malloc(size);
if (size > 0)
{
va_start(ap, format);
size = vsnprintf(msg, size, format, ap);
va_end(ap);
}
fprintf(stdout, "%s", ERR_START);
(*original_error)(status, errnum, "%s", msg==nullptr ? "???" : msg);
fprintf(stdout, "%s", ERR_END);
free(msg);
}
#endif /* WRAP_STDERR */
int tcsetattr(int fd, int optional_actions, const struct termios *termios_p)
{
typedef int (*tcsetattr_type)(int, int, const struct termios *);
static tcsetattr_type original_tcsetattr = (tcsetattr_type) dlsym(RTLD_NEXT, "tcsetattr");
int result = (*original_tcsetattr)(fd, optional_actions, termios_p);
if (isDomTerm(fd) && fd >= 0 && fd <= 2)
{
const char* icanon_str = (termios_p->c_lflag & ICANON) != 0 ? "icanon" : "-icanon";
const char* echo_str = (termios_p->c_lflag & ECHO) != 0 ? "echo" : "-echo";
char buf[100];
sprintf(buf, "\033]71; %s %s \007", icanon_str, echo_str);
write(1, buf, strlen(buf));
}
return result;
}