-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconin.c
364 lines (325 loc) · 7.95 KB
/
conin.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// conin.c
// Copyright 2009 Andy Koppe
// Licensed under the terms of the GNU General Public License v3 or later.
#define WINVER 0x501
#define _WIN32_WINNT WINVER
#define _WIN32_IE WINVER
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <wchar.h>
#include <process.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <wait.h>
#include <termios.h>
#include <locale.h>
#include <langinfo.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <windows.h>
static int pid;
static HANDLE conin;
struct termios orig_tattr, raw_tattr;
static char prompt[256];
static int prompt_len;
static bool in_readline_mode = false;
static const struct {
ushort cp;
const char *name;
}
cs_names[] = {
{CP_UTF8, "UTF-8"},
{ 20127, "ASCII"},
{ 20866, "KOI8-R"},
{ 21866, "KOI8-U"},
{ 936, "GBK"},
{ 950, "BIG5"},
{ 932, "SJIS"},
{ 20932, "EUCJP"},
{ 949, "EUCKR"},
};
static int
cs_cp(const char *name)
{
uint iso;
if (sscanf(name, "ISO-8859-%u", &iso) == 1)
return 28590 + iso;
uint cp;
if (sscanf(name, "CP%u", &cp) == 1)
return cp;
for (uint i = 0; i < sizeof(cs_names) / sizeof(*cs_names); i++) {
if (strcmp(name, cs_names[i].name) == 0)
return cs_names[i].cp;
}
return 0;
}
static void
sigchld(int sig)
{
int status;
if (wait(&status) != pid)
return;
tcsetattr(0, TCSANOW, &orig_tattr);
if (WIFEXITED(status))
exit(WEXITSTATUS(status));
else if (WIFSIGNALED(status)) {
signal(SIGINT, SIG_DFL);
signal(SIGHUP, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGABRT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGUSR1, SIG_DFL);
signal(SIGUSR2, SIG_DFL);
signal(SIGWINCH,SIG_DFL);
raise(WTERMSIG(status));
}
}
static void
sigfwd(int sig)
{
kill(pid, sig);
}
static void
error(char *msg)
{
fputs(msg, stderr);
exit(1);
}
static void
sigact(int sig, void (*handler)(int), int flags)
{
struct sigaction action;
action.sa_handler = handler;
action.sa_mask = 0;
action.sa_flags = flags;
sigaction(sig, &action, 0);
}
static void
forward_output(int src_fd, int dest_fd)
{
char buf[256];
int buf_len = read(src_fd, buf, sizeof buf);
write(dest_fd, buf, buf_len);
// Look for a line feed
int p;
for (p = buf_len; p; p--) {
if (buf[p-1] == '\n') {
prompt_len = buf_len - p;
memcpy(prompt, buf + p, prompt_len);
break;
}
}
if (!p) {
int old_prompt_len = prompt_len;
prompt_len += buf_len;
if (prompt_len < sizeof prompt)
memcpy(prompt + old_prompt_len, buf, buf_len);
}
prompt[prompt_len < sizeof prompt ? prompt_len : 0] = 0;
}
static void
trans_char(INPUT_RECORD **ppinrec, char c)
{
wchar_t wc;
switch (mbrtowc(&wc, &c, 1, 0)) {
case -2: return;
case -1:
mbrtowc(0, 0, 0, 0);
return;
}
SHORT vkks = VkKeyScan(c);
UCHAR vk = vkks;
bool shift = vkks & 0x100, ctrl = vkks & 0x200, alt = vkks & 0x400;
WORD vsc = MapVirtualKey(vk, 0 /* MAPVK_VK_TO_VSC */);
(*ppinrec)[0] = (*ppinrec)[1] = (INPUT_RECORD){
.EventType = KEY_EVENT,
.Event = {
.KeyEvent = {
.bKeyDown = false,
.wRepeatCount = 1,
.wVirtualKeyCode = vk,
.wVirtualScanCode = vsc,
.uChar = { .UnicodeChar = wc },
.dwControlKeyState =
shift * SHIFT_PRESSED |
ctrl * LEFT_CTRL_PRESSED |
alt * RIGHT_ALT_PRESSED
}
}
};
(*ppinrec)[0].Event.KeyEvent.bKeyDown = true;
*ppinrec += 2;
}
static void
rl_callback(char *line)
{
in_readline_mode = false;
rl_callback_handler_remove();
if (!line) {
INPUT_RECORD inrecs[2], *pinrec = inrecs;
trans_char(&pinrec, 26); // ^Z
WriteConsoleInputW(conin, inrecs, pinrec - inrecs, &(DWORD){0});
}
else {
if (*line)
add_history(line);
size_t len = strlen(line) + 1;
line[len - 1] = '\r';
INPUT_RECORD inrecs[len * 2], *pinrec = inrecs;
for (int i = 0; i < len; i++)
trans_char(&pinrec, line[i]);
WriteConsoleInputW(conin, inrecs, pinrec - inrecs, &(DWORD){0});
}
// Fresh prompt.
prompt_len = 0;
*prompt = 0;
}
static void
forward_raw_char(void)
{
static enum {START, SEEN_ESC, SEEN_CSI} state = START;
char c = getchar();
INPUT_RECORD inrecs[2];
switch (state) {
case START:
if (c == '\e') {
state = SEEN_ESC;
return;
}
if (c == '\n')
c = '\r';
else if (c == 0x7F)
c = '\b';
INPUT_RECORD *pinrec = inrecs;
trans_char(&pinrec, c);
WriteConsoleInputW(conin, inrecs, pinrec - inrecs, &(DWORD){0});
return;
case SEEN_ESC:
state = c == '[' ? SEEN_CSI : START;
return;
case SEEN_CSI:
state = START;
UCHAR vk;
switch (c) {
case 'A': vk = VK_UP; break;
case 'B': vk = VK_DOWN; break;
case 'C': vk = VK_RIGHT; break;
case 'D': vk = VK_LEFT; break;
case 'F': vk = VK_END; break;
case 'H': vk = VK_HOME; break;
default: return;
}
WORD vsc = MapVirtualKey(vk, 0 /* MAPVK_VK_TO_VSC */);
inrecs[0] = inrecs[1] = (INPUT_RECORD){
.EventType = KEY_EVENT,
.Event = {
.KeyEvent = {
.bKeyDown = false,
.wRepeatCount = 1,
.wVirtualKeyCode = vk,
.wVirtualScanCode = vsc,
.uChar = { .UnicodeChar = 0 },
.dwControlKeyState = 0
}
}
};
inrecs[0].Event.KeyEvent.bKeyDown = true;
WriteConsoleInputW(conin, inrecs, 2, &(DWORD){0});
break;
}
}
static void
forward_input(void)
{
if (!in_readline_mode) {
DWORD mode;
GetConsoleMode(conin, &mode);
if ((mode & 7) == 7) { // PROCESSED_INPUT, LINE_INPUT, ECHO_INPUT
in_readline_mode = true;
tcsetattr(0, TCSANOW, &orig_tattr);
rl_already_prompted = true;
rl_callback_handler_install(prompt, rl_callback);
}
}
if (in_readline_mode) {
rl_callback_read_char();
if (!in_readline_mode)
tcsetattr(0, TCSANOW, &raw_tattr);
}
else
forward_raw_char();
}
int
main(int argc, char *argv[])
{
if (argc < 2)
return 2;
int pipe_fds[2];
if (pipe(pipe_fds) != 0)
error("Could not create output pipe");
// This hack prompts Cygwin to allocate an invisible console for us.
spawnl(_P_WAIT, "/bin/test", "/bin/test", 0);
// Get hold of the console input buffer
conin =
CreateFile(
"CONIN$",
GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE,
0, OPEN_EXISTING, 0, 0
);
if (conin == INVALID_HANDLE_VALUE)
error("Could not open console input buffer");
// Configure charsets
setlocale(LC_CTYPE, "");
int cp = cs_cp(nl_langinfo(CODESET));
SetConsoleCP(cp);
SetConsoleOutputCP(cp);
pid = fork();
if (pid < 0)
error("Could not create child process");
else if (pid == 0) {
setsid();
AttachConsole(-1);
close(0);
if (open("/dev/conin", O_RDONLY) != 0)
error("Could not open /dev/conin");
close(pipe_fds[0]);
int pipe_fd = pipe_fds[1];
dup2(pipe_fd, 1);
dup2(pipe_fd, 2);
if (pipe_fd > 2)
close(pipe_fd);
execvp(argv[1], argv + 1);
error("Could not execute command");
}
sigact(SIGCHLD, sigchld, SA_NOCLDSTOP);
signal(SIGINT, sigfwd);
signal(SIGHUP, sigfwd);
signal(SIGQUIT, sigfwd);
signal(SIGABRT, sigfwd);
signal(SIGTERM, sigfwd);
signal(SIGUSR1, sigfwd);
signal(SIGUSR2, sigfwd);
signal(SIGWINCH,sigfwd);
close(pipe_fds[1]);
int pipe_fd = pipe_fds[0];
tcgetattr (0, &orig_tattr);
raw_tattr = orig_tattr;
raw_tattr.c_lflag &= ~(ECHO|ICANON|IEXTEN);
tcsetattr(0, TCSANOW, &raw_tattr);
rl_catch_signals = false;
rl_bind_key ('\t', rl_insert);
for (;;) {
fd_set fdset;
FD_ZERO(&fdset);
FD_SET(0, &fdset);
FD_SET(pipe_fd, &fdset);
if (select(pipe_fd + 1, &fdset, 0, 0, 0) > 0) {
if (FD_ISSET(pipe_fd, &fdset))
forward_output(pipe_fd, 1);
if (FD_ISSET(0, &fdset))
forward_input();
}
}
}