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
|
/* $Header: /p/tcsh/cvsroot/tcsh/vms.termcap.c,v 1.12 2011/01/09 16:25:29 christos Exp $ */
/*
* termcap.c 1.1 20/7/87 agc Joypace Ltd
*
* Copyright Joypace Ltd, London, UK, 1987. All rights reserved.
* This file may be freely distributed provided that this notice
* remains attached.
*
* A public domain implementation of the termcap(3) routines.
*/
#include "sh.h"
RCSID("$tcsh: vms.termcap.c,v 1.12 2011/01/09 16:25:29 christos Exp $")
#if defined(_VMS_POSIX) || defined(_OSD_POSIX) || defined(__ANDROID__)
/* efth 1988-Apr-29
- Correct when TERM != name and TERMCAP is defined [tgetent]
- Correct the comparison for the terminal name [tgetent]
- Correct the value of ^x escapes [tgetstr]
- Added %r to reverse row/column [tgoto]
Paul Gillingwater <paul@actrix.gen.nz> July 1992
- Modified to allow terminal aliases in termcap file
- Uses TERMCAP environment variable for file only
*/
#include <stdio.h>
#include <string.h>
#define CAPABLEN 2
#define ISSPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\r' || (c) == '\n')
#define ISDIGIT(x) ((x) >= '0' && (x) <= '9')
char *capab; /* the capability itself */
extern char *getenv(); /* new, improved getenv */
#ifndef fopen
extern FILE *fopen(); /* old fopen */
#endif
/*
* tgetent - get the termcap entry for terminal name, and put it
* in bp (which must be an array of 1024 chars). Returns 1 if
* termcap entry found, 0 if not found, and -1 if file not found.
*/
int
tgetent(char *bp, char *name)
{
#ifdef __ANDROID__
/* Use static termcap entry since termcap file usually doesn't exist. */
capab = bp;
strcpy(bp,
"linux|linux console:"
":am:eo:mi:ms:xn:xo:"
":it#8:"
":AL=\\E[%dL:DC=\\E[%dP:DL=\\E[%dM:IC=\\E[%d@:K2=\\E[G:al=\\E[L:"
":bl=^G:cd=\\E[J:ce=\\E[K:cl=\\E[H\\E[J:cm=\\E[%i%d;%dH:cr=^M:"
":cs=\\E[%i%d;%dr:ct=\\E[3g:dc=\\E[P:dl=\\E[M:do=^J:ec=\\E[%dX:"
":ei=\\E[4l:ho=\\E[H:ic=\\E[@:im=\\E[4h:k1=\\E[[A:k2=\\E[[B:"
":k3=\\E[[C:k4=\\E[[D:k5=\\E[[E:k6=\\E[17~:k7=\\E[18~:k8=\\E[19~:"
":k9=\\E[20~:kD=\\E[3~:kI=\\E[2~:kN=\\E[6~:kP=\\E[5~:kb=\\177:"
":kd=\\E[B:kh=\\E[1~:kl=\\E[D:kr=\\E[C:ku=\\E[A:le=^H:mb=\\E[5m:"
":md=\\E[1m:me=\\E[0m:mh=\\E[2m:mr=\\E[7m:nd=\\E[C:nw=^M^J:"
":rc=\\E8:sc=\\E7:se=\\E[27m:sf=^J:so=\\E[7m:sr=\\EM:st=\\EH:ta=^I:"
":ue=\\E[24m:up=\\E[A:us=\\E[4m:vb=200\\E[?5h\\E[?5l:"
":ve=\\E[?25h\\E[?0c:vi=\\E[?25l\\E[?1c:vs=\\E[?25h\\E[?0c:"
);
return(1);
#else
FILE *fp;
char *termfile;
char *cp,
*ptr, /* temporary pointer */
tmp[1024]; /* buffer for terminal name *//*FIXBUF*/
size_t len = strlen(name);
capab = bp;
/* Use TERMCAP to override default. */
termfile = getenv("TERMCAP");
if (termfile == NULL ) termfile = "/etc/termcap";
if ((fp = fopen(termfile, "r")) == (FILE *) NULL) {
fprintf(stderr, CGETS(31, 1,
"Can't open TERMCAP: [%s]\n"), termfile);
fprintf(stderr, CGETS(31, 2, "Can't open %s.\n"), termfile);
sleep(1);
return(-1);
}
while (fgets(bp, 1024, fp) != NULL) {
/* Any line starting with # or NL is skipped as a comment */
if ((*bp == '#') || (*bp == '\n')) continue;
/* Look for lines which end with two backslashes,
and then append the next line. */
while (*(cp = &bp[strlen(bp) - 2]) == '\\')
fgets(cp, 1024, fp);
/* Skip over any spaces or tabs */
for (++cp ; ISSPACE(*cp) ; cp++);
/* Make sure "name" matches exactly (efth) */
/* Here we might want to look at any aliases as well. We'll use
sscanf to look at aliases. These are delimited by '|'. */
sscanf(bp,"%[^|]",tmp);
if (strncmp(name, tmp, len) == 0) {
fclose(fp);
#ifdef DEBUG
fprintf(stderr, CGETS(31, 3, "Found %s in %s.\n"), name, termfile);
sleep(1);
#endif /* DEBUG */
return(1);
}
ptr = bp;
while ((ptr = strchr(ptr,'|')) != NULL) {
ptr++;
if (strchr(ptr,'|') == NULL) break;
sscanf(ptr,"%[^|]",tmp);
if (strncmp(name, tmp, len) == 0) {
fclose(fp);
#ifdef DEBUG
fprintf(stderr,CGETS(31, 3, "Found %s in %s.\n"), name, termfile);
sleep(1);
#endif /* DEBUG */
return(1);
}
}
}
/* If we get here, then we haven't found a match. */
fclose(fp);
#ifdef DEBUG
fprintf(stderr,CGETS(31, 4, "No match found for %s in file %s\n"),
name, termfile);
sleep(1);
#endif /* DEBUG */
return(0);
#endif /* ANDROID */
}
/*
* tgetnum - get the numeric terminal capability corresponding
* to id. Returns the value, -1 if invalid.
*/
int
tgetnum(char *id)
{
char *cp;
int ret;
if ((cp = capab) == NULL || id == NULL)
return(-1);
while (*++cp != ':')
;
for (++cp ; *cp ; cp++) {
while (ISSPACE(*cp))
cp++;
if (strncmp(cp, id, CAPABLEN) == 0) {
while (*cp && *cp != ':' && *cp != '#')
cp++;
if (*cp != '#')
return(-1);
for (ret = 0, cp++ ; *cp && ISDIGIT(*cp) ; cp++)
ret = ret * 10 + *cp - '0';
return(ret);
}
while (*cp && *cp != ':')
cp++;
}
return(-1);
}
/*
* tgetflag - get the boolean flag corresponding to id. Returns -1
* if invalid, 0 if the flag is not in termcap entry, or 1 if it is
* present.
*/
int
tgetflag(char *id)
{
char *cp;
if ((cp = capab) == NULL || id == NULL)
return(-1);
while (*++cp != ':')
;
for (++cp ; *cp ; cp++) {
while (ISSPACE(*cp))
cp++;
if (strncmp(cp, id, CAPABLEN) == 0)
return(1);
while (*cp && *cp != ':')
cp++;
}
return(0);
}
/*
* tgetstr - get the string capability corresponding to id and place
* it in area (advancing area at same time). Expand escape sequences
* etc. Returns the string, or NULL if it can't do it.
*/
char *
tgetstr(char *id, char **area)
{
char *cp;
char *ret;
int i;
if ((cp = capab) == NULL || id == NULL)
return(NULL);
while (*++cp != ':')
;
for (++cp ; *cp ; cp++) {
while (ISSPACE(*cp))
cp++;
if (strncmp(cp, id, CAPABLEN) == 0) {
while (*cp && *cp != ':' && *cp != '=')
cp++;
if (*cp != '=')
return(NULL);
for (ret = *area, cp++; *cp && *cp != ':' ;
(*area)++, cp++)
switch(*cp) {
case '^' :
**area = *++cp - '@'; /* fix (efth)*/
break;
case '\\' :
switch(*++cp) {
case 'E' :
**area = CTL_ESC('\033');
break;
case 'n' :
**area = '\n';
break;
case 'r' :
**area = '\r';
break;
case 't' :
**area = '\t';
break;
case 'b' :
**area = '\b';
break;
case 'f' :
**area = '\f';
break;
case '0' :
case '1' :
case '2' :
case '3' :
for (i=0 ; *cp && ISDIGIT(*cp) ;
cp++)
i = i * 8 + *cp - '0';
**area = i;
cp--;
break;
case '^' :
case '\\' :
**area = *cp;
break;
}
break;
default :
**area = *cp;
}
*(*area)++ = '\0';
return(ret);
}
while (*cp && *cp != ':')
cp++;
}
return(NULL);
}
/*
* tgoto - given the cursor motion string cm, make up the string
* for the cursor to go to (destcol, destline), and return the string.
* Returns "OOPS" if something's gone wrong, or the string otherwise.
*/
char *
tgoto(char *cm, int destcol, int destline)
{
char *rp;
static char ret[24];
int incr = 0;
int argno = 0, numval;
for (rp = ret ; *cm ; cm++) {
switch(*cm) {
case '%' :
switch(*++cm) {
case '+' :
numval = (argno == 0 ? destline : destcol);
argno = 1 - argno;
*rp++ = numval + incr + *++cm;
break;
case '%' :
*rp++ = '%';
break;
case 'i' :
incr = 1;
break;
case 'd' :
numval = (argno == 0 ? destline : destcol);
numval += incr;
argno = 1 - argno;
*rp++ = '0' + (numval/10);
*rp++ = '0' + (numval%10);
break;
case 'r' :
argno = 1;
break;
}
break;
default :
*rp++ = *cm;
}
}
*rp = '\0';
return(ret);
}
/*
* tputs - put the string cp out onto the terminal, using the function
* outc. This should do padding for the terminal, but I can't find a
* terminal that needs padding at the moment...
*/
int
tputs(char *cp, int affcnt, int (*outc)())
{
unsigned long delay = 0;
if (cp == NULL)
return(1);
/* do any padding interpretation - left null for MINIX just now */
for (delay = 0; *cp && ISDIGIT(*cp) ; cp++)
delay = delay * 10 + *cp - '0';
while (*cp)
(*outc)(*cp++);
#ifdef _OSD_POSIX
usleep(delay*100); /* strictly spoken, it should be *1000 */
#endif
return(1);
}
#endif /* _VMS_POSIX || _OSD_POSIX */
|