-
Notifications
You must be signed in to change notification settings - Fork 10
/
term.cpp
219 lines (175 loc) · 3.75 KB
/
term.cpp
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
//
//
// term.cpp
//
// (C) R.P.Bellis 1994
//
#include <cstdlib>
#include <cassert>
#include "term.h"
//------------------------------------------------------------------------
// Machine dependent Terminal implementations
//------------------------------------------------------------------------
//------------------------------------------------------------------------
#if defined(_POSIX_SOURCE)
#include <cstdio>
#include <unistd.h>
#include <ncurses.h>
#include <termios.h>
#include <sys/select.h>
#include <sys/types.h>
#include <string.h>
Terminal::Terminal(USim& sys) : sys(sys)
{
input = stdin;
output = stdout;
input_fd = fileno(input);
// Set input and output to be unbuffered
setbuf(input, (char *)0);
setbuf(output, (char *)0);
// Get copies of startup terminal attributes
tcgetattr(input_fd, &oattr);
tcgetattr(input_fd, &nattr);
setup();
}
Terminal::~Terminal()
{
reset();
}
void Terminal::setup()
{
nattr.c_lflag &= ~ICANON;
nattr.c_lflag &= ~ISIG;
nattr.c_lflag &= ~ECHO;
nattr.c_lflag |= ECHOE;
nattr.c_iflag &= ~ICRNL;
nattr.c_oflag &= ~ONLCR;
tcsetattr(input_fd, TCSANOW, &nattr);
}
void Terminal::reset()
{
tcsetattr(input_fd, TCSANOW, &oattr);
}
bool Terminal::real_poll_read()
{
fd_set fds;
// Uses minimal (10us) delay in select(2) call to
// ensure that idling simulations don't chew
// up masses of CPU time
static struct timeval tv = { 0L, 10L };
FD_ZERO(&fds);
FD_SET(input_fd, &fds);
(void)select(FD_SETSIZE, &fds, NULL, NULL, &tv);
return FD_ISSET(input_fd, &fds);
}
Byte Terminal::real_read()
{
return fgetc(input);
}
void Terminal::write(Byte ch)
{
fputc(ch, output);
}
//------------------------------------------------------------------------
#elif defined(__MSDOS__) || defined(__BORLANDC__)
#include <conio.h>
Terminal::Terminal()
{
}
Terminal::~Terminal()
{
}
bool Terminal::real_poll_read()
{
return kbhit();
}
Byte Terminal::real_read()
{
return getch();
}
void Terminal::write(Byte ch)
{
putch(ch);
}
void Terminal::setup()
{
}
void Terminal::reset()
{
}
#endif
//------------------------------------------------------------------------
//------------------------------------------------------------------------
// Machine independent part, handles ssh-like tilde-escapes
//------------------------------------------------------------------------
void Terminal::tilde_escape_help()
{
fprintf(stderr, "\r\nSupported escape sequences:\r\n");
fprintf(stderr, " ~. - terminate emulator\r\n");
fprintf(stderr, " ~? - this message\r\n");
fprintf(stderr, " ~~ - send the escape character by typing it twice\r\n");
tilde_escape_help_other();
fprintf(stderr, "(Note that escapes are only recognized immediately after newline.)\r\n");
}
bool Terminal::poll_read()
{
if (read_data_available) {
return true;
}
bool ready = real_poll_read();
if (!ready) {
read_data_available = false;
return read_data_available;
}
Byte ch = read_data = real_read();
read_data_available = true;
switch (tilde_escape_phase) {
case 0:
if (ch == 0x0a || ch == 0x0d) {
tilde_escape_phase = 1;
}
break;
case 1:
if (ch == '~') {
tilde_escape_phase = 2;
read_data_available = false;
} else {
tilde_escape_phase = 0;
}
break;
case 2:
tilde_escape_phase = 0;
read_data_available = false;
switch (ch) {
case '~':
read_data_available = true;
break;
case '.':
sys.halt();
break;
case '?':
tilde_escape_help();
break;
default:
tilde_escape_do_other(ch);
break;
}
break;
default:
assert(false);
break;
}
return read_data_available;
}
void Terminal::tilde_escape_do_other(char ch)
{
(void)ch;
}
void Terminal::tilde_escape_help_other()
{
}
Byte Terminal::read()
{
read_data_available = false;
return read_data;
}