-
Notifications
You must be signed in to change notification settings - Fork 4
/
csi.c
350 lines (323 loc) · 7.64 KB
/
csi.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
/*
* msTERM
* ANSI CSI parser
*
* Copyright (c) 2019 joshua stein <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "mailstation.h"
unsigned char csibuf[TEXT_COLS];
unsigned int csibuflen;
unsigned char in_csi;
void
parseCSI(void)
{
int x, y, serviced;
int param1 = -1, param2 = -1;
char c = csibuf[csibuflen - 1];
char parambuf[4];
int parambuflen;
#ifdef DEBUG
char sb[TEXT_COLS];
#endif
if (c < 'A' || (c > 'Z' && c < 'a') || c > 'z')
return;
switch (c) {
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'J':
case 'K':
case 'S':
case 'T':
case 'd':
case 'g':
case 's':
case 'u':
case '7':
case '8':
/* optional multiplier */
if (c == 'J' || c == 'K')
param1 = 0;
else
param1 = 1;
if (csibuflen > 1) {
for (x = 0; x < csibuflen - 1, x < 4; x++) {
parambuf[x] = csibuf[x];
parambuf[x + 1] = '\0';
}
param1 = atoi(parambuf);
}
break;
case 'H':
case 'f':
/* two optional parameters separated by ; each defaulting to 1 */
param1 = 1;
param2 = 1;
y = -1;
for (x = 0; x < csibuflen; x++) {
if (csibuf[x] == ';') {
y = x;
break;
}
}
if (y == -1)
/* CSI 17H -> CSI 17; */
y = csibuflen - 1;
if (y > 0) {
for (x = 0; x < y && x < 4; x++) {
parambuf[x] = csibuf[x];
parambuf[x + 1] = '\0';
}
param1 = atoi(parambuf);
if (y < csibuflen - 2) {
parambuf[0] = '\0';
for (x = 0; x < (csibuflen - 1 - y) && x < 4; x++) {
parambuf[x] = csibuf[y + 1 + x];
parambuf[x + 1] = '\0';
}
param2 = atoi(parambuf);
}
}
break;
}
serviced = 1;
uncursor();
#ifdef DEBUG
sprintf(sb, "CSI:");
for (x = 0; x < csibuflen; x++)
sprintf(sb, "%s%c", sb, csibuf[x]);
sprintf(sb, "%s params:%d,%d", sb, param1, param2);
update_statusbar(sb);
#endif
switch (c) {
case 'A': /* CUU - cursor up, stop at top of screen */
for (x = 0; x < param1 && cursory > 0; x++)
cursory--;
break;
case 'B': /* CUD - cursor down, stop at bottom of screen */
for (x = 0; x < param1 && cursory < TEXT_ROWS - 1; x++)
cursory++;
break;
case 'C': /* CUF - cursor forward, stop at screen edge */
for (x = 0; x < param1 && cursorx < TEXT_COLS; x++)
cursorx++;
break;
case 'D': /* CUB - cursor back, stop at left edge of screen */
for (x = 0; x < param1 && cursorx > 0; x++)
cursorx--;
break;
case 'E': /* CNL - cursor next line */
cursorx = 0;
for (x = 0; x < param1 && cursory < TEXT_ROWS - 1; x++)
cursory++;
break;
case 'F': /* CPL - cursor previous line */
cursorx = 0;
for (x = 0; x < param1 && cursory > 0; x++)
cursory--;
break;
case 'G': /* CHA - cursor horizontal absolute */
if (param1 > TEXT_COLS)
param1 = TEXT_COLS;
cursorx = param1;
break;
case 'H': /* CUP - cursor absolute position */
case 'f': /* HVP - horizontal vertical position */
if (param1 - 1 < 0)
cursory = 0;
else if (param1 > TEXT_ROWS)
cursory = TEXT_ROWS - 1;
else
cursory = param1 - 1;
if (param2 - 1 < 0)
cursorx = 0;
else if (param2 > TEXT_COLS)
cursorx = TEXT_COLS - 1;
else
cursorx = param2 - 1;
break;
case 'J': /* ED - erase in display */
switch (param1) {
case 0:
/* clear from cursor to end of screen */
for (y = cursory; y < TEXT_ROWS; y++) {
for (x = 0; x < TEXT_COLS; x++) {
if (y == cursory && x < cursorx)
continue;
putchar_attr(y, x, ' ', 0);
}
}
break;
case 1:
/* clear from cursor to beginning of the screen */
for (y = cursory; y >= 0; y--) {
for (x = TEXT_COLS; x >= 0; x--) {
if (y == cursory && x > cursorx)
continue;
putchar_attr(y, x, ' ', 0);
}
}
break;
case 2:
/* clear entire screen */
for (y = 0; y < TEXT_ROWS; y++) {
for (x = 0; x < TEXT_COLS; x++)
putchar_attr(y, x, ' ', 0);
}
}
break;
case 'K': /* EL - erase in line */
switch (param1) {
case 0:
/* clear from cursor to end of line */
if (cursorx >= TEXT_COLS)
break;
for (x = cursorx; x < TEXT_COLS; x++)
putchar_attr(cursory, x, ' ', 0);
break;
case 1:
/* clear from cursor to beginning of line */
if (cursorx == 0)
break;
for (x = cursorx; x >= 0; x--)
putchar_attr(cursory, x, ' ', 0);
break;
case 2:
/* clear entire line */
for (x = 0; x < TEXT_COLS - 1; x++)
putchar_attr(cursory, x, ' ', 0);
}
break;
case 'S': /* SU - scroll up */
/* TODO */
break;
case 'T': /* SD - scroll down */
/* TODO */
break;
case 'd': /* absolute line number */
if (param1 < 1)
cursory = 0;
else if (param1 > TEXT_ROWS)
cursory = TEXT_ROWS;
else
cursory = param1 - 1;
break;
case 'g': /* clear tabs, ignore */
break;
case 'h': /* reset, ignore */
break;
case 'm': /* graphic changes */
parambuf[0] = '\0';
parambuflen = 0;
param2 = putchar_sgr;
for (x = 0; x < csibuflen; x++) {
/* all the way to csibuflen to catch 'm' */
if (csibuf[x] == ';' || csibuf[x] == 'm') {
param1 = atoi(parambuf);
switch (param1) {
case 0: /* reset */
case 22: /* normal color */
param2 = 0;
break;
case 1: /* bold */
param2 |= ATTR_BOLD;
break;
case 4: /* underline */
param2 |= ATTR_UNDERLINE;
break;
case 7: /* reverse */
param2 |= ATTR_REVERSE;
break;
case 21: /* bold off */
param2 &= ~(ATTR_BOLD);
break;
case 24: /* underline off */
param2 &= ~(ATTR_UNDERLINE);
break;
case 27: /* inverse off */
param2 &= ~(ATTR_REVERSE);
break;
}
parambuf[0] = '\0';
parambuflen = 0;
} else if (parambuflen < 4) {
parambuf[parambuflen] = csibuf[x];
parambuflen++;
parambuf[parambuflen] = '\0';
}
}
putchar_sgr = param2;
break;
case 'n': /* DSR - device status report */
switch (param1) {
case 5: /* terminal is ready */
obuf[obuf_pos++] = ESC;
obuf[obuf_pos++] = '[';
obuf[obuf_pos++] = '0';
obuf[obuf_pos++] = 'n';
break;
case 6: /* CPR - report cursor position */
obuf[obuf_pos++] = ESC;
obuf[obuf_pos++] = '[';
itoa(cursory + 1, parambuf, 10);
for (x = 0; x < sizeof(parambuf); x++) {
if (parambuf[x] == '\0')
break;
obuf[obuf_pos++] = parambuf[x];
}
obuf[obuf_pos++] = ';';
itoa(cursorx + 1, parambuf, 10);
for (x = 0; x < sizeof(parambuf); x++) {
if (parambuf[x] == '\0')
break;
obuf[obuf_pos++] = parambuf[x];
}
obuf[obuf_pos++] = 'R';
break;
}
break;
case '7': /* DECSC - save cursor */
case 's': /* from ANSI.SYS */
saved_cursorx = cursorx;
saved_cursory = cursory;
break;
case '8': /* DECRC - restore cursor */
case 'u': /* from ANSI.SYS */
cursorx = saved_cursorx;
cursory = saved_cursory;
break;
default:
/*
* if the last character is a letter and we haven't serviced
* it, assume it's a sequence we don't support and should just
* suppress
*/
if (c < 65 || (c > 90 && c < 97) || c > 122)
serviced = 0;
}
if (serviced) {
recursor();
csibuflen = 0;
csibuf[0] = '\0';
in_csi = 0;
}
}