-
Notifications
You must be signed in to change notification settings - Fork 45
/
ansisys.cpp
215 lines (183 loc) · 4.43 KB
/
ansisys.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
// Implements ansi.sys display class
// public domain
// by David Nugent <[email protected]>
// 3:632/348@fidonet
#include <iostream.h>
#include <ctype.h>
#include <string.h>
#include "ansisys.hpp"
ansisys::ansisys(video & v)
: scrinterp(v),
dispfunc(0),
savx(0),
savy(0)
{}
void
ansisys::reset()
{
dispfunc = 0;
savx = savy = 0;
scrinterp::reset();
}
// This is the top level dispatcher. It really only
// Looks for special control characters
void
ansisys::putch(int ch)
{
if (dispfunc)
(this->*dispfunc)(ch);
else if (ch == '\x1b')
{
flushbuf();
dispfunc = &ansisys::esc; // Hand over to ESC routine
}
else
scrinterp::putch(ch);
}
// This parses for the leadin for an escape sequence
void
ansisys::esc(int ch)
{
switch (ch)
{
case '[': // ANSI lead-in
dispfunc = &ansisys::seq; // Now parse the sequences
break;
default: // Huh?
putbuf('?');
dispfunc = 0;
break;
}
}
int
ansisys::evalargs(cell_t * plist)
{
chrbuf[chridx] = '\0';
char const * s = chrbuf;
int idx = 0;
while (idx < 32)
{
cell_t res = 0;
while (*s && isdigit(*s)) // Evaluate numeric
res = cell_t((res * 10) + (*s++ - '0'));
plist[idx++] = res;
if (*s == ';') // Skip over
++s;
else break; // Not a valid delimiter, quit
}
return idx;
}
// Parses esc sequences
void
ansisys::seq(int ch)
{
if (ch == ';' || isdigit(ch)) // Accumulate parameters
{
if (chridx < CBUFSZ)
chrbuf[chridx++] = char(ch);
}
else
{
cell_t plist[32];
cell_t x,
y,
mx,
my;
int nx =0,
ny =0;
int args = evalargs(plist);
switch (ch)
{
default: // Unknown sequences
reset();
break;
case 'A': // Cursor up
ny = -((args < 1 || plist[0] == 0) ? 1 : plist[0]);
goto mvxy;
case 'B': // Cursor down
ny = ((args < 1 || plist[0] == 0) ? 1 : plist[0]);
goto mvxy;
case 'C': // Cursor right
nx = ((args < 1 || plist[0] == 0) ? 1 : plist[0]);
goto mvxy;
case 'D': // Cursor left
nx = -((args < 1 || plist[0] == 0) ? 1 : plist[0]);
mvxy:
vs.wherexy(x, y);
x = cell_t(x + nx);
y = cell_t(y + ny);
goto goxy;
case 'H': // Cursor position
y = cell_t(((args < 1 || plist[0] == 0) ? 1 : plist[0]) - 1);
x = cell_t(((args < 2 || plist[1] == 0) ? 1 : plist[1]) - 1);
goxy:
vs.maxxy(mx, my);
if (x < mx && y < my)
vs.gotoxy(x, y);
break;
case 'J': // Erase display
if (args && plist[0] == 2)
vs.gotoxy(0, 0);
vs.cls();
break;
case 'K': // Erase line
vs.wherexy(x, y);
vs.maxxy(mx, my);
vs.repchr(' ', mx - x);
vs.gotoxy(x, y);
break;
case 'n': // Device status report
// Not supported here
// Need to send back ESC [ x+1 ; y+1 R
break;
case 's': // Save cursor position
vs.wherexy(savx, savy);
break;
case 'u': // Restore cursor position
vs.gotoxy(savx, savy);
break;
case 'm': // Set graphics rendition (colour)
if (!args)
plist[args++] = 0;
for (int idx = 0 ; idx < args ; ++idx)
{
cell_t val = plist[idx];
static cell_t colmap[] = { 0, 4, 2, 6, 1, 5, 3, 7 };
switch (val)
{
case 0:
vs.setattr(7);
break;
case 1:
vs.setfg(cell_t(vs.getfg() | 0x8));
break;
case 2:
vs.setfg(cell_t(vs.getbg() | 0x8));
break;
case 3:
vs.setfg(cell_t((vs.getbg() & ~0x8) | 1));
break;
case 5:
vs.setbg(cell_t(vs.getbg() | 0x8));
break;
case 7:
vs.setfg(0);
vs.setbg(cell_t(7 + (vs.getbg() & 0x8)));
break;
case 8:
vs.setfg(cell_t(vs.getbg() & ~0x8));
break;
default:
if (val >= 30 && val <= 37)
vs.setfg(cell_t(colmap[val-30] | (vs.getfg() & 0x8)));
else if (val >= 40 && val <= 47)
vs.setbg(cell_t(colmap[val-40] | (vs.getbg() & 0x8)));
break;
}
}
break;
}
chridx = 0;
dispfunc = 0;
}
}