-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcap.c
397 lines (339 loc) · 8.67 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
#define termdef 1 /* don't define "term" external */
#include <stdio.h>
#include "estruct.h"
#include "etype.h"
#include "edef.h"
#include "english.h"
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#define MARGIN 8
#define SCRSIZ 64
#define NPAUSE 10 /* # times thru update to pause */
#define BEL 0x07
/* Termcap Sequence definitions */
typedef struct TBIND
{
char p_name[4]; /* sequence name */
short p_code; /* resulting keycode of sequence */
char p_seq[10]; /* terminal escape sequence */
} TBIND;
TBIND ttable[] =
{
"bt", SHFT | CTRL | 'i', "", /* backtab */
"k1", SPEC | '1', "", /* function key 1 */
"k2", SPEC | '2', "", /* function key 2 */
"k3", SPEC | '3', "", /* function key 3 */
"k4", SPEC | '4', "", /* function key 4 */
"k5", SPEC | '5', "", /* function key 5 */
"k6", SPEC | '6', "", /* function key 6 */
"k7", SPEC | '7', "", /* function key 7 */
"k8", SPEC | '8', "", /* function key 8 */
"k9", SPEC | '9', "", /* function key 9 */
"k0", SPEC | '0', "", /* function key 10 */
"kA", CTRL | 'O', "", /* insert line */
"kb", CTRL | 'H', "", /* backspace */
"kC", CTRL | 'L', "", /* clear screen */
"kD", SPEC | 'D', "", /* delete character */
"kd", SPEC | 'N', "", /* down cursor */
"kE", CTRL | 'K', "", /* clear to end of line */
"kF", CTRL | 'V', "", /* scroll down */
"kH", SPEC | '>', "", /* home down [END?] key */
"kh", SPEC | '<', "", /* home */
"kI", SPEC | 'C', "", /* insert character */
"kL", CTRL | 'K', "", /* delete line */
"kl", SPEC | 'B', "", /* left cursor */
"kN", SPEC | 'V', "", /* next page */
"kP", SPEC | 'Z', "", /* previous page */
"kR", CTRL | 'Z', "", /* scroll down */
"kr", SPEC | 'F', "", /* right cursor */
"ku", SPEC | 'P', "", /* up cursor */
};
#define NTBINDS sizeof(ttable)/sizeof(TBIND)
extern int ttopen();
extern int ttgetc();
extern int ttputc();
extern int tgetnum();
extern int ttflush();
extern int ttclose();
extern int tcapkopen();
extern int tcapkclose();
extern int tcapgetc();
extern int tcapmove();
extern int tcapeeol();
extern int tcapeeop();
extern int tcapbeep();
extern int tcaprev();
extern int tcapcres();
extern int tcapopen();
extern int tcapclose();
extern char *tgoto();
extern int ttputs();
#define TCAPSLEN 1024
char tcapbuf[TCAPSLEN];
char *UP, PC, *CM, *CE, *CL, *SO, *SE, *IS, *KS, *KE;
TERM term =
{
0, 0, 0, 0, /* these four values are set dynamically at
* open time */
MARGIN,
SCRSIZ,
NPAUSE,
tcapopen,
tcapclose,
tcapkopen,
tcapkclose,
tcapgetc,
ttputc,
ttflush,
tcapmove,
tcapeeol,
tcapeeop,
tcapbeep,
tcaprev,
tcapcres,
ttputs
};
/* input buffers and pointers */
#define IBUFSIZE 64 /* this must be a power of 2 */
unsigned char in_buf[IBUFSIZE]; /* input character buffer */
int in_next = 0; /* pos to retrieve next input character */
int in_last = 0; /* pos to place most recent input character */
in_init() /* initialize the input buffer */
{
in_next = in_last = 0;
}
in_check() /* is the input buffer non-empty? */
{
if (in_next == in_last)
return (FALSE);
else
return (TRUE);
}
in_put(int event)
{
in_buf[in_last++] = event;
in_last &= (IBUFSIZE - 1);
}
int in_get() /* get an event from the input buffer */
{
register int event; /* event to return */
event = in_buf[in_next++];
in_next &= (IBUFSIZE - 1);
return (event);
}
/*
* Open the terminal put it in RA mode learn about the screen size read
* TERMCAP strings for function keys
*/
tcapopen()
{
register int index; /* general index */
char *t, *p;
char tcbuf[1024];
char *tv_stype;
char err_str[72];
char *getenv();
char *tgetstr();
if ((tv_stype = getenv("TERM")) == (char *) NULL)
{
puts(TEXT182);
/* "Environment variable TERM not defined!" */
meexit(1);
}
if ((tgetent(tcbuf, tv_stype)) != 1)
{
sprintf(err_str, TEXT183, tv_stype);
/* "Unknown terminal type %s!" */
puts(err_str);
meexit(1);
}
if ((term.t_nrow = (short) tgetnum("li") - 2) == -1)
{
puts(TEXT184);
/* "termcap entry incomplete (lines)" */
meexit(1);
}
term.t_mrow = term.t_nrow;
if ((term.t_ncol = (short) tgetnum("co")) == -1)
{
puts(TEXT185);
/* "Termcap entry incomplete (columns)" */
meexit(1);
}
term.t_mcol = term.t_ncol;
p = tcapbuf;
t = tgetstr("pc", &p);
if (t)
PC = *t;
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 != (char *) NULL)
revexist = TRUE;
if (CL == (char *) NULL || CM == (char *) NULL || UP == (char *) NULL)
{
puts(TEXT186);
/* "Incomplete termcap entry\n" */
meexit(1);
}
if (CE == (char *) NULL) /* will we be able to use clear to EOL? */
eolexist = FALSE;
IS = tgetstr("is", &p); /* extract init string */
KS = tgetstr("ks", &p); /* extract keypad transmit string */
KE = tgetstr("ke", &p); /* extract keypad transmit end string */
/* read definitions of various function keys into ttable */
for (index = 0; index < NTBINDS; index++)
{
strcpy(ttable[index].p_seq,
fixnull(tgetstr(ttable[index].p_name, &p)));
}
/* tell unix we are goint to use the terminal */
ttopen();
/* make sure we don't over run the buffer (TOO LATE I THINK) */
if (p >= &tcapbuf[TCAPSLEN])
{
puts(TEXT187);
/* "Terminal description too big!\n" */
meexit(1);
}
/* send init strings if defined */
if (IS != (char *) NULL)
putpad(IS);
if (KS != (char *) NULL)
putpad(KS);
/* initialize the input buffer */
in_init();
}
tcapclose()
{
/* send end-of-keypad-transmit string if defined */
if (KE != (char *) NULL)
putpad(KE);
ttclose();
}
tcapkopen()
{
strcpy(sres, "NORMAL");
}
tcapkclose()
{
}
/*
* TCAPGETC: Get on character. Resolve and setup all the appropriate
* keystroke escapes as defined in the comments at the beginning of input.c
*/
int tcapgetc()
{
int c; /* current extended keystroke */
/* if there are already keys waiting.... send them */
if (in_check())
return (in_get());
/* otherwise... get the char for now */
c = get1key();
/* unfold the control bit back into the character */
if (CTRL & c)
c = (c & ~CTRL) - '@';
/* fold the event type into the input stream as an escape seq */
if ((c & ~255) != 0)
{
in_put(0); /* keyboard escape prefix */
in_put(c >> 8); /* event type */
in_put(c & 255); /* event code */
return (tcapgetc());
}
return (c);
}
/*
* GET1KEY: Get one keystroke. The only prefixs legal here are the SPEC
* and CTRL prefixes.
*
* Note:
*
* Escape sequences that are generated by terminal function and cursor keys
* could be confused with the user typing the default META prefix followed by
* other chars... ie
*
* UPARROW = <ESC>A on some terminals... apropos = M-A
*
* The difference is determined by measuring the time between the input of the
* first and second character... if an <ESC> is types, and is not followed by
* another char in 1/30 of a second (think 300 baud) then it is a user input,
* otherwise it was generated by an escape sequence and should be SPECed.
*/
int get1key()
{
register int c;
register int index; /* index into termcap binding table */
char *sp;
int fdset;
struct timeval timeout;
char cseq[10]; /* current sequence being parsed */
c = ttgetc();
/* if it is not an escape character */
if (c != 27)
return (c);
/* process a possible escape sequence */
/* set up to check the keyboard for input */
fdset = 1;
timeout.tv_sec = 0;
timeout.tv_usec = 35000L;
/* check to see if things are pending soon */
if (select(1, &fdset, (int *) NULL, (int *) NULL, &timeout) == 0)
return (CTRL | '[');
/* a key is pending within 1/30 of a sec... its an escape sequence */
cseq[0] = 27;
sp = &cseq[1];
while (sp < &cseq[6])
{
c = ttgetc();
*sp++ = c;
*sp = 0;
for (index = 0; index < NTBINDS; index++)
{
if (strcmp(cseq, ttable[index].p_seq) == 0)
return (ttable[index].p_code);
}
}
return (SPEC | 0);
}
tcapmove(int row, int col)
{
putpad(tgoto(CM, col, row + 1));
}
tcapeeol()
{
putpad(CE);
}
tcapeeop()
{
putpad(CL);
tcapmove(0, 0);
}
tcaprev(int state) /* change reverse video status */
/* FALSE = normal video, TRUE = reverse */
{
/* static int revstate = FALSE; */
if (state)
{
if (SO != (char *) NULL)
putpad(SO);
}
else if (SE != (char *) NULL)
putpad(SE);
}
tcapcres() /* change screen resolution */
{
return (TRUE);
}
tcapbeep()
{
ttputc(BEL);
}
putpad(char *str)
{
tputs(str, 1, ttputc);
}