forked from bellard/mquickjs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreadline_tty.c
More file actions
246 lines (219 loc) · 6.13 KB
/
readline_tty.c
File metadata and controls
246 lines (219 loc) · 6.13 KB
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
/*
* Readline TTY support
*
* Copyright (c) 2017-2025 Fabrice Bellard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <inttypes.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include <time.h>
#include <sys/time.h>
#include <math.h>
#include <fcntl.h>
#ifdef _WIN32
#include <windows.h>
#include <conio.h>
#include <io.h>
#else
#include <signal.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#endif
#include "readline_tty.h"
static int ctrl_c_pressed;
#ifdef _WIN32
/* Windows 10 built-in VT100 emulation */
#define __ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#define __ENABLE_VIRTUAL_TERMINAL_INPUT 0x0200
static BOOL WINAPI ctrl_handler(DWORD type)
{
if (type == CTRL_C_EVENT) {
ctrl_c_pressed++;
if (ctrl_c_pressed >= 4) {
/* just to be able to stop the process if it is hanged */
return FALSE;
} else {
return TRUE;
}
} else {
return FALSE;
}
}
int readline_tty_init(void)
{
HANDLE handle;
CONSOLE_SCREEN_BUFFER_INFO info;
int n_cols;
handle = (HANDLE)_get_osfhandle(0);
SetConsoleMode(handle, ENABLE_WINDOW_INPUT | __ENABLE_VIRTUAL_TERMINAL_INPUT);
_setmode(0, _O_BINARY);
handle = (HANDLE)_get_osfhandle(1); /* corresponding output */
SetConsoleMode(handle, ENABLE_PROCESSED_OUTPUT | ENABLE_WRAP_AT_EOL_OUTPUT | __ENABLE_VIRTUAL_TERMINAL_PROCESSING);
SetConsoleCtrlHandler(ctrl_handler, TRUE);
n_cols = 80;
if (GetConsoleScreenBufferInfo(handle, &info)) {
n_cols = info.dwSize.X;
}
return n_cols;
}
/* if processed input is enabled, Ctrl-C is handled by ctrl_handler() */
static void set_processed_input(BOOL enable)
{
DWORD mode;
HANDLE handle;
handle = (HANDLE)_get_osfhandle(0);
if (!GetConsoleMode(handle, &mode))
return;
if (enable)
mode |= ENABLE_PROCESSED_INPUT;
else
mode &= ~ENABLE_PROCESSED_INPUT;
SetConsoleMode(handle, mode);
}
#else
/* init terminal so that we can grab keys */
/* XXX: merge with cp_utils.c */
static struct termios oldtty;
static int old_fd0_flags;
static void term_exit(void)
{
tcsetattr (0, TCSANOW, &oldtty);
fcntl(0, F_SETFL, old_fd0_flags);
}
static void sigint_handler(int signo)
{
ctrl_c_pressed++;
if (ctrl_c_pressed >= 4) {
/* just to be able to stop the process if it is hanged */
signal(SIGINT, SIG_DFL);
}
}
int readline_tty_init(void)
{
struct termios tty;
struct sigaction sa;
struct winsize ws;
int n_cols;
tcgetattr (0, &tty);
oldtty = tty;
old_fd0_flags = fcntl(0, F_GETFL);
tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
|INLCR|IGNCR|ICRNL|IXON);
tty.c_oflag |= OPOST;
tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
// tty.c_lflag &= ~ISIG; /* ctrl-C returns a signal */
tty.c_cflag &= ~(CSIZE|PARENB);
tty.c_cflag |= CS8;
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
tcsetattr (0, TCSANOW, &tty);
memset(&sa, 0, sizeof(sa));
sa.sa_handler = sigint_handler;
sa.sa_flags = 0;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
atexit(term_exit);
// fcntl(0, F_SETFL, O_NONBLOCK);
n_cols = 80;
if (ioctl(0, TIOCGWINSZ, &ws) == 0 &&
ws.ws_col >= 4 && ws.ws_row >= 4) {
n_cols = ws.ws_col;
}
return n_cols;
}
#endif
void term_printf(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vprintf(fmt, ap);
va_end(ap);
}
void term_flush(void)
{
fflush(stdout);
}
const char *readline_tty(ReadlineState *s,
const char *prompt, BOOL multi_line)
{
int len, i, ctrl_c_count, c, ret;
const char *ret_str;
uint8_t buf[128];
#ifdef _WIN32
set_processed_input(FALSE);
/* ctrl-C is no longer handled by the system */
#endif
ret_str = NULL;
readline_start(s, prompt, FALSE);
ctrl_c_count = 0;
while (ret_str == NULL) {
len = read(0, buf, sizeof(buf));
if (len == 0)
break;
for(i = 0; i < len; i++) {
c = buf[i];
#ifdef _WIN32
if (c == 3) {
/* ctrl-C */
ctrl_c_pressed++;
} else
#endif
{
ret = readline_handle_byte(s, c);
if (ret == READLINE_RET_EXIT) {
goto done;
} else if (ret == READLINE_RET_ACCEPTED) {
ret_str = (const char *)s->term_cmd_buf;
goto done;
}
ctrl_c_count = 0;
}
}
if (ctrl_c_pressed) {
ctrl_c_pressed = 0;
if (ctrl_c_count == 0) {
printf("(Press Ctrl-C again to quit)\n");
ctrl_c_count++;
} else {
printf("Exiting.\n");
break;
}
}
}
done:
#ifdef _WIN32
set_processed_input(TRUE);
#endif
return ret_str;
}
BOOL readline_is_interrupted(void)
{
BOOL ret;
ret = (ctrl_c_pressed != 0);
ctrl_c_pressed = 0;
return ret;
}