-
Notifications
You must be signed in to change notification settings - Fork 239
/
tcap.c
356 lines (316 loc) · 6.3 KB
/
tcap.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
351
352
353
354
355
356
/* tcap.c
*
* Unix V7 SysV and BS4 Termcap video driver
*
* modified by Petri Kutvonen
*/
/*
* Defining this to 1 breaks tcapopen() - it doesn't check if the
* sceen size has changed.
* -lbt
*/
#define USE_BROKEN_OPTIMIZATION 0
#define termdef 1 /* Don't define "term" external. */
#include <curses.h>
#include <stdio.h>
#include <term.h>
#include "estruct.h"
#include "edef.h"
#include "efunc.h"
#if TERMCAP
#if UNIX
#include <signal.h>
#endif
#define MARGIN 8
#define SCRSIZ 64
#define NPAUSE 10 /* # times thru update to pause. */
#define BEL 0x07
#define ESC 0x1B
static void tcapkopen(void);
static void tcapkclose(void);
static void tcapmove(int, int);
static void tcapeeol(void);
static void tcapeeop(void);
static void tcapbeep(void);
static void tcaprev(int);
static int tcapcres(char *);
static void tcapscrollregion(int top, int bot);
static void putpad(char *str);
static void tcapopen(void);
#if PKCODE
static void tcapclose(void);
#endif
#if COLOR
static void tcapfcol(void);
static void tcapbcol(void);
#endif
#if SCROLLCODE
static void tcapscroll_reg(int from, int to, int linestoscroll);
static void tcapscroll_delins(int from, int to, int linestoscroll);
#endif
#define TCAPSLEN 315
static char tcapbuf[TCAPSLEN];
static char *UP, PC, *CM, *CE, *CL, *SO, *SE;
#if PKCODE
static char *TI, *TE;
#if USE_BROKEN_OPTIMIZATION
static int term_init_ok = 0;
#endif
#endif
#if SCROLLCODE
static char *CS, *DL, *AL, *SF, *SR;
#endif
struct terminal term = {
0, /* These four values are set dynamically at open time. */
0,
0,
0,
MARGIN,
SCRSIZ,
NPAUSE,
tcapopen,
#if PKCODE
tcapclose,
#else
ttclose,
#endif
tcapkopen,
tcapkclose,
ttgetc,
ttputc,
ttflush,
tcapmove,
tcapeeol,
tcapeeop,
tcapbeep,
tcaprev,
tcapcres
#if COLOR
, tcapfcol,
tcapbcol
#endif
#if SCROLLCODE
, NULL /* set dynamically at open time */
#endif
};
static void tcapopen(void)
{
char *t, *p;
char tcbuf[1024];
char *tv_stype;
char err_str[72];
int int_col, int_row;
#if PKCODE && USE_BROKEN_OPTIMIZATION
if (!term_init_ok) {
#endif
if ((tv_stype = getenv("TERM")) == NULL) {
puts("Environment variable TERM not defined!");
exit(1);
}
if ((tgetent(tcbuf, tv_stype)) != 1) {
sprintf(err_str, "Unknown terminal type %s!",
tv_stype);
puts(err_str);
exit(1);
}
/* Get screen size from system, or else from termcap. */
getscreensize(&int_col, &int_row);
term.t_nrow = int_row - 1;
term.t_ncol = int_col;
if ((term.t_nrow <= 0)
&& (term.t_nrow = (short) tgetnum("li") - 1) == -1) {
puts("termcap entry incomplete (lines)");
exit(1);
}
if ((term.t_ncol <= 0)
&& (term.t_ncol = (short) tgetnum("co")) == -1) {
puts("Termcap entry incomplete (columns)");
exit(1);
}
#ifdef SIGWINCH
term.t_mrow = MAXROW;
term.t_mcol = MAXCOL;
#else
term.t_mrow = term.t_nrow > MAXROW ? MAXROW : term.t_nrow;
term.t_mcol = term.t_ncol > MAXCOL ? MAXCOL : term.t_ncol;
#endif
p = tcapbuf;
t = tgetstr("pc", &p);
if (t)
PC = *t;
else
PC = 0;
CL = tgetstr("cl", &p);
CM = tgetstr("cm", &p);
CE = tgetstr("ce", &p);
UP = tgetstr("up", &p);
SE = tgetstr("se", &p);
SO = tgetstr("so", &p);
if (SO != NULL)
revexist = TRUE;
#if PKCODE
if (tgetnum("sg") > 0) { /* can reverse be used? P.K. */
revexist = FALSE;
SE = NULL;
SO = NULL;
}
TI = tgetstr("ti", &p); /* terminal init and exit */
TE = tgetstr("te", &p);
#endif
if (CL == NULL || CM == NULL || UP == NULL) {
puts("Incomplete termcap entry\n");
exit(1);
}
if (CE == NULL) /* will we be able to use clear to EOL? */
eolexist = FALSE;
#if SCROLLCODE
CS = tgetstr("cs", &p);
SF = tgetstr("sf", &p);
SR = tgetstr("sr", &p);
DL = tgetstr("dl", &p);
AL = tgetstr("al", &p);
if (CS && SR) {
if (SF == NULL) /* assume '\n' scrolls forward */
SF = "\n";
term.t_scroll = tcapscroll_reg;
} else if (DL && AL) {
term.t_scroll = tcapscroll_delins;
} else {
term.t_scroll = NULL;
}
#endif
if (p >= &tcapbuf[TCAPSLEN]) {
puts("Terminal description too big!\n");
exit(1);
}
#if PKCODE && USE_BROKEN_OPTIMIZATION
term_init_ok = 1;
}
#endif
ttopen();
}
#if PKCODE
static void tcapclose(void)
{
putpad(tgoto(CM, 0, term.t_nrow));
putpad(TE);
ttflush();
ttclose();
}
#endif
static void tcapkopen(void)
{
#if PKCODE
putpad(TI);
ttflush();
ttrow = 999;
ttcol = 999;
sgarbf = TRUE;
#endif
strcpy(sres, "NORMAL");
}
static void tcapkclose(void)
{
#if PKCODE
putpad(TE);
ttflush();
#endif
}
static void tcapmove(int row, int col)
{
putpad(tgoto(CM, col, row));
}
static void tcapeeol(void)
{
putpad(CE);
}
static void tcapeeop(void)
{
putpad(CL);
}
/*
* Change reverse video status
*
* @state: FALSE = normal video, TRUE = reverse video.
*/
static void tcaprev(int state)
{
if (state) {
if (SO != NULL)
putpad(SO);
} else if (SE != NULL)
putpad(SE);
}
/* Change screen resolution. */
static int tcapcres(char *res)
{
return TRUE;
}
#if SCROLLCODE
/* move howmanylines lines starting at from to to */
static void tcapscroll_reg(int from, int to, int howmanylines)
{
int i;
if (to == from)
return;
if (to < from) {
tcapscrollregion(to, from + howmanylines - 1);
tcapmove(from + howmanylines - 1, 0);
for (i = from - to; i > 0; i--)
putpad(SF);
} else { /* from < to */
tcapscrollregion(from, to + howmanylines - 1);
tcapmove(from, 0);
for (i = to - from; i > 0; i--)
putpad(SR);
}
tcapscrollregion(0, term.t_nrow);
}
/* move howmanylines lines starting at from to to */
static void tcapscroll_delins(int from, int to, int howmanylines)
{
int i;
if (to == from)
return;
if (to < from) {
tcapmove(to, 0);
for (i = from - to; i > 0; i--)
putpad(DL);
tcapmove(to + howmanylines, 0);
for (i = from - to; i > 0; i--)
putpad(AL);
} else {
tcapmove(from + howmanylines, 0);
for (i = to - from; i > 0; i--)
putpad(DL);
tcapmove(from, 0);
for (i = to - from; i > 0; i--)
putpad(AL);
}
}
/* cs is set up just like cm, so we use tgoto... */
static void tcapscrollregion(int top, int bot)
{
ttputc(PC);
putpad(tgoto(CS, bot, top));
}
#endif
#if COLOR
/* No colors here, ignore this. */
static void tcapfcol(void)
{
}
/* No colors here, ignore this. */
static void tcapbcol(void)
{
}
#endif
static void tcapbeep(void)
{
ttputc(BEL);
}
static void putpad(char *str)
{
tputs(str, 1, ttputc);
}
#endif /* TERMCAP */