-
Notifications
You must be signed in to change notification settings - Fork 12
/
TrmTERM.c.sv
384 lines (353 loc) · 8.63 KB
/
TrmTERM.c.sv
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
/* terminal control module for terminals described by TERMCAP */
/* Copyright (c) 1981,1980 James Gosling */
/* Modified 1-Dec-80 by Dan Hoey (DJH) to understand C100 underlines */
/* Modified 2-Dec-80 (DJH) to turn off highlighting on insertline */
/* Modified 4 Aug 81 by JQ Johnson: use "dm","ei","pc","mi" */
/* Modified 24-Aug-81 by Jeff Mogul (JCM) at Stanford
* - uses "nl" instead of \n in case \n is destructive
* Modified 8-Sept-81 by JCM @ Stanford
* - re-integrated changes from Gosling since July '81
*/
#include <stdio.h>
#include <ctype.h>
#include "config.h"
#include "keyboard.h"
#include "display.h"
static
int curX, curY;
char *tgetstr ();
char *UP;
char *BC;
char PC;
short ospeed;
static char *ILstr, *DLstr, *ICstr, *DCstr, *ELstr, *ESstr, *HLBstr,
*HLEstr, *ICPstr, *ICPDstr, *CursStr, *BEGINstr, *ENDstr,
*TIstr, *TEstr,
*ICEstr, *NDstr, *VBstr, *EDstr, *DMstr, *NLstr;
static int ULflag; /* DJH -- 1 if terminal has underline */
static int MIflag; /* JQJ -- 1 if safe to move while in insert mode */
static
dumpchar (c) {
putchar (c);
}
static
enum IDmode { m_insert = 1, m_overwrite = 0 }
CurMode, DesMode;
static
INSmode (new)
enum IDmode new; {
DesMode = new;
if(DesMode==m_insert && ICstr==0) abort();
};
static curHL, desHL;
static
HLmode (on) {
desHL = on;
}
static
setHL () {
register char *com;
if (tt.t_modeline == 0 || curHL == desHL)
return;
if(com = desHL ? HLBstr : HLEstr)
tputs (com, 0, dumpchar);
curHL = desHL;
}
static
clearHL () {
if (curHL) {
register oldes = desHL;
desHL = 0;
setHL ();
desHL = oldes;
}
}
static
setmode () {
if (DesMode == CurMode)
return;
tputs(DesMode==m_insert ? ICstr : ICEstr, 0, dumpchar);
CurMode = DesMode;
};
static
inslines (n) {
HLmode (0); /* DJH -- Don't highlight the inserted line */
setHL ();
while (--n >= 0)
tputs (ILstr, tt.t_length-curY, dumpchar);
};
static
dellines (n) {
while (--n >= 0)
tputs(DLstr, tt.t_length-curY, dumpchar);
};
static
writechars (start, end)
register char *start,
*end; {
setmode ();
setHL();
while (start <= end) {
if(CurMode == m_insert && ICPstr) tputs(ICPstr, tt.t_width-curX, dumpchar);
/* DJH -- blank out space before underlines */
if(*start == '_' && CurMode != m_insert && ULflag != 0) {
putchar (' ');
putchar (*BC);
}
putchar (*start++);
if(CurMode == m_insert && ICPDstr) tputs(ICPDstr, tt.t_width-curX, dumpchar);
curX++;
}
};
static
blanks (n) {
setmode ();
setHL ();
while (--n >= 0) {
if (CurMode == m_insert && ICPstr)
tputs (ICPstr, tt.t_width - curX, dumpchar);
putchar (' ');
if (CurMode == m_insert && ICPDstr)
tputs (ICPDstr, 1 /* tt.t_width - curX */, dumpchar);
curX++;
}
};
static float BaudFactor;
static pad(n,f)
float f; {
register k = n * f * BaudFactor;
while (--k >= 0)
putchar (PC);
};
static
topos (row, column) {
clearHL (); /* many terminals can't hack highlighting
around cursor positioning. Silly twits! */
if (CurMode==m_insert && ! MIflag) {
tputs(ICEstr, 0, dumpchar); /* some terminals can't move in */
CurMode = m_overwrite; /* insert mode -- JQJ */
}
if (curY == row) {
if (curX == column)
return;
if (curX == column + 1 && (CurMode != m_insert)) {
tputs (BC, 0, dumpchar);
goto done;
}
if (curX == column - 1 && NDstr && CurMode != m_insert){
tputs (NDstr, 0, dumpchar);
goto done;
}
}
if (curY - 1 == row && curX == column && UP != 0
&& CurMode != m_insert){
tputs (UP, 0, dumpchar);
goto done;
}
if ( (curY + 1 == row && (column == 1 || column==curX))
&& (CurMode != m_insert) ){
if(column!=curX) putchar (015);
tputs (NLstr, 0, dumpchar);
/* putchar (012); JCM */
goto done;
}
tputs(tgoto (CursStr, column-1, row-1), 0, dumpchar);
done:
curX = column;
curY = row;
};
static
flash () { /* dump a visible bell */
tputs (VBstr, 0, dumpchar);
}
static
init (BaudRate) {
static char tbuf[1024];
static char combuf[1024];
extern old;
char *fill = combuf;
int mf, ov; /* for timing calculations */
static inited;
if (!inited)
{
FluidStatic(&inited, sizeof(inited)); /* reset on restart */
if (tgetent (tbuf, getenv ("TERM")) <= 0) {
stty (1, &old);
#ifdef OneEmacsPerTty /* TPM 31-Jan-82 */
UnlockTty();
#endif
quit (1, "No environment-specified terminal type -- see TSET(1), sh(1)\n");
}
}
inited = 1;
ILstr = tgetstr ("al", &fill);
DLstr = tgetstr ("dl", &fill);
ICstr = tgetstr ("im", &fill);
ICEstr = tgetstr ("ei", &fill);
MIflag = tgetflag ("mi"); /* can move in insert mode */
DCstr = tgetstr ("dc", &fill);
ELstr = tgetstr ("ce", &fill);
ESstr = tgetstr ("cl", &fill);
HLBstr = tgetstr ("so", &fill);
HLEstr = tgetstr ("se", &fill);
ICPstr = tgetstr ("ic", &fill);
ICPDstr = tgetstr ("ip", &fill);
CursStr = tgetstr ("cm", &fill);
UP = tgetstr ("up", &fill);
NDstr = tgetstr ("nd", &fill);
VBstr = tgetstr ("vb", &fill);
TIstr = tgetstr ("ti", &fill);
TEstr = tgetstr ("te", &fill);
DMstr = tgetstr ("dm", &fill);/* start delete mode */
EDstr = tgetstr ("ed", &fill);/* end delete mode */
BC = tgetstr ("bc", &fill);
if (BC == 0)
BC = "\b";
ULflag = tgetflag ("ul"); /* DJH -- Find out about underline */
NLstr = tgetstr ("nl", &fill);/* JCM -- find out about newline */
if (NLstr == 0)
NLstr = "\n"; /* use default if none specified */
MetaFlag = tgetflag ("MT") || tgetflag("km");
BEGINstr = tgetstr ("ti", &fill);
/* PC = (tgetstr("pc", &fill)!=0) ? *tgetstr("pc", &fill) : 0; JQJ */
PC = 0;
BaudFactor = BaudRate / 10000.;
if (CursStr == 0 || UP == 0 || ELstr == 0 || ESstr == 0) {
stty (1, &old);
#ifdef OneEmacsPerTty /* TPM 31-Jan-82 */
UnlockTty();
#endif
quit (1, "Sorry, this terminal isn't powerful enough to run Emacs.\n\
It is missing some important features.\n");
}
if (ILstr)
costof(ILstr, &ov, &mf);
else
ov = mf = MissingFeature;
tt.t_ILmf = mf;
tt.t_ILov = ov;
if (!ILstr)
tt.t_inslines = tt.t_dellines = (int (*) ()) - 1;
if (VBstr)
tt.t_flash = flash;
if (ICstr && DCstr) {
costof(ICstr, &ov, &mf);
tt.t_ICov = ov + (ICPstr ? mf : 0);
if (ICPstr)
costof(ICPstr, &ov, &mf);
else
ov = mf + 1;
tt.t_ICmf = ov;
tt.t_DCov = tt.t_ICov;
costof(DCstr, &ov, &mf);
tt.t_DCmf = ov + mf;
}
else {
tt.t_ICmf = MissingFeature;
tt.t_ICov = MissingFeature;
tt.t_DCmf = MissingFeature;
tt.t_DCov = MissingFeature;
}
tt.t_length = tgetnum ("li");
/* tt.t_width = tgetnum ("co") - (tgetflag ("in") ? 1 : 0); */
tt.t_width = tgetnum ("co") - 1;/* Always lie about the width */
};
static
costof (str, ov, mf)
char *str;
int *ov, *mf;
{
register char *cp;
int pad;
*ov = *mf = pad = 0;
for (cp = str; isdigit(*cp); cp++)
pad = pad * 10 + (*cp - '0');
if (*cp == '*')
{
*mf = pad * BaudFactor;
cp++;
}
else
*ov = pad;
*ov += strlen(cp);
return *ov;
}
static
reset () {
curX = -1;
curY = -1;
if (BEGINstr) tputs(BEGINstr, 0, dumpchar);
if (TIstr) tputs(TIstr, 0, dumpchar);
topos(1,1);
tputs(ESstr, 0, dumpchar);
CurMode = m_insert;
DesMode = m_overwrite;
};
static
cleanup () {
HLmode (0);
DesMode = m_overwrite;
setmode();
if (TEstr) tputs(TEstr, 0, dumpchar);
};
static
wipeline () {
setHL ();
tputs (ELstr, tt.t_width-curX, dumpchar);
};
static
wipescreen () {
tputs(ESstr, 0, dumpchar);
curX = curY = -1;
};
static
delchars (n) {
if (DMstr) { /* we may have delete mode, or delete == insert */
if (strcmp(DMstr,ICstr)) {
if (CurMode == m_overwrite) {
tputs(ICstr,0,dumpchar);
CurMode = m_insert; /* we're now in both */
}
}
else {
if (CurMode == m_insert) {
tputs(ICEstr, 0, dumpchar);
CurMode = m_overwrite;
}
tputs(DMstr,0,dumpchar);
}
}
while (--n >= 0) {
tputs(DCstr, tt.t_width-curX, dumpchar);
}
if (EDstr) { /* for some, insert mode == delete mode */
/* bug! /etc/termcap pads ICEstr but not EDstr */
if (strcmp(DMstr,ICstr))
CurMode = m_insert;
else
tputs(EDstr,0,dumpchar);
}
};
TrmTERM () {
tt.t_INSmode = INSmode;
tt.t_HLmode = HLmode;
tt.t_inslines = inslines;
tt.t_dellines = dellines;
tt.t_blanks = blanks;
tt.t_init = init;
tt.t_cleanup = cleanup;
tt.t_wipeline = wipeline;
tt.t_wipescreen = wipescreen;
tt.t_topos = topos;
tt.t_reset = reset;
tt.t_delchars = delchars;
tt.t_writechars = writechars;
tt.t_window = 0;
tt.t_ILmf = 0;
tt.t_ILov = 0;
tt.t_ICmf = 0;
tt.t_ICov = 0;
tt.t_length = 24;
tt.t_width = 80;
tt.t_modeline = 1; /* do highlight */
};