1 /* $Header: /p/tcsh/cvsroot/tcsh/vms.termcap.c,v 1.12 2011/01/09 16:25:29 christos Exp $ */
3 * termcap.c 1.1 20/7/87 agc Joypace Ltd
5 * Copyright Joypace Ltd, London, UK, 1987. All rights reserved.
6 * This file may be freely distributed provided that this notice
9 * A public domain implementation of the termcap(3) routines.
12 RCSID("$tcsh: vms.termcap.c,v 1.12 2011/01/09 16:25:29 christos Exp $")
13 #if defined(_VMS_POSIX) || defined(_OSD_POSIX) || defined(__ANDROID__)
16 - Correct when TERM != name and TERMCAP is defined [tgetent]
17 - Correct the comparison for the terminal name [tgetent]
18 - Correct the value of ^x escapes [tgetstr]
19 - Added %r to reverse row/column [tgoto]
21 Paul Gillingwater <paul@actrix.gen.nz> July 1992
22 - Modified to allow terminal aliases in termcap file
23 - Uses TERMCAP environment variable for file only
31 #define ISSPACE(c) ((c) == ' ' || (c) == '\t' || (c) == '\r' || (c) == '\n')
32 #define ISDIGIT(x) ((x) >= '0' && (x) <= '9')
34 char *capab; /* the capability itself */
36 extern char *getenv(); /* new, improved getenv */
38 extern FILE *fopen(); /* old fopen */
42 * tgetent - get the termcap entry for terminal name, and put it
43 * in bp (which must be an array of 1024 chars). Returns 1 if
44 * termcap entry found, 0 if not found, and -1 if file not found.
47 tgetent(char *bp, char *name)
50 /* Use static termcap entry since termcap file usually doesn't exist. */
53 "linux|linux console:"
56 ":AL=\\E[%dL:DC=\\E[%dP:DL=\\E[%dM:IC=\\E[%d@:K2=\\E[G:al=\\E[L:"
57 ":bl=^G:cd=\\E[J:ce=\\E[K:cl=\\E[H\\E[J:cm=\\E[%i%d;%dH:cr=^M:"
58 ":cs=\\E[%i%d;%dr:ct=\\E[3g:dc=\\E[P:dl=\\E[M:do=^J:ec=\\E[%dX:"
59 ":ei=\\E[4l:ho=\\E[H:ic=\\E[@:im=\\E[4h:k1=\\E[[A:k2=\\E[[B:"
60 ":k3=\\E[[C:k4=\\E[[D:k5=\\E[[E:k6=\\E[17~:k7=\\E[18~:k8=\\E[19~:"
61 ":k9=\\E[20~:kD=\\E[3~:kI=\\E[2~:kN=\\E[6~:kP=\\E[5~:kb=\\177:"
62 ":kd=\\E[B:kh=\\E[1~:kl=\\E[D:kr=\\E[C:ku=\\E[A:le=^H:mb=\\E[5m:"
63 ":md=\\E[1m:me=\\E[0m:mh=\\E[2m:mr=\\E[7m:nd=\\E[C:nw=^M^J:"
64 ":rc=\\E8:sc=\\E7:se=\\E[27m:sf=^J:so=\\E[7m:sr=\\EM:st=\\EH:ta=^I:"
65 ":ue=\\E[24m:up=\\E[A:us=\\E[4m:vb=200\\E[?5h\\E[?5l:"
66 ":ve=\\E[?25h\\E[?0c:vi=\\E[?25l\\E[?1c:vs=\\E[?25h\\E[?0c:"
73 *ptr, /* temporary pointer */
74 tmp[1024]; /* buffer for terminal name *//*FIXBUF*/
75 size_t len = strlen(name);
79 /* Use TERMCAP to override default. */
81 termfile = getenv("TERMCAP");
82 if (termfile == NULL ) termfile = "/etc/termcap";
84 if ((fp = fopen(termfile, "r")) == (FILE *) NULL) {
85 fprintf(stderr, CGETS(31, 1,
86 "Can't open TERMCAP: [%s]\n"), termfile);
87 fprintf(stderr, CGETS(31, 2, "Can't open %s.\n"), termfile);
92 while (fgets(bp, 1024, fp) != NULL) {
93 /* Any line starting with # or NL is skipped as a comment */
94 if ((*bp == '#') || (*bp == '\n')) continue;
96 /* Look for lines which end with two backslashes,
97 and then append the next line. */
98 while (*(cp = &bp[strlen(bp) - 2]) == '\\')
101 /* Skip over any spaces or tabs */
102 for (++cp ; ISSPACE(*cp) ; cp++);
104 /* Make sure "name" matches exactly (efth) */
106 /* Here we might want to look at any aliases as well. We'll use
107 sscanf to look at aliases. These are delimited by '|'. */
109 sscanf(bp,"%[^|]",tmp);
110 if (strncmp(name, tmp, len) == 0) {
113 fprintf(stderr, CGETS(31, 3, "Found %s in %s.\n"), name, termfile);
119 while ((ptr = strchr(ptr,'|')) != NULL) {
121 if (strchr(ptr,'|') == NULL) break;
122 sscanf(ptr,"%[^|]",tmp);
123 if (strncmp(name, tmp, len) == 0) {
126 fprintf(stderr,CGETS(31, 3, "Found %s in %s.\n"), name, termfile);
133 /* If we get here, then we haven't found a match. */
136 fprintf(stderr,CGETS(31, 4, "No match found for %s in file %s\n"),
145 * tgetnum - get the numeric terminal capability corresponding
146 * to id. Returns the value, -1 if invalid.
154 if ((cp = capab) == NULL || id == NULL)
158 for (++cp ; *cp ; cp++) {
161 if (strncmp(cp, id, CAPABLEN) == 0) {
162 while (*cp && *cp != ':' && *cp != '#')
166 for (ret = 0, cp++ ; *cp && ISDIGIT(*cp) ; cp++)
167 ret = ret * 10 + *cp - '0';
170 while (*cp && *cp != ':')
177 * tgetflag - get the boolean flag corresponding to id. Returns -1
178 * if invalid, 0 if the flag is not in termcap entry, or 1 if it is
186 if ((cp = capab) == NULL || id == NULL)
190 for (++cp ; *cp ; cp++) {
193 if (strncmp(cp, id, CAPABLEN) == 0)
195 while (*cp && *cp != ':')
202 * tgetstr - get the string capability corresponding to id and place
203 * it in area (advancing area at same time). Expand escape sequences
204 * etc. Returns the string, or NULL if it can't do it.
207 tgetstr(char *id, char **area)
213 if ((cp = capab) == NULL || id == NULL)
217 for (++cp ; *cp ; cp++) {
220 if (strncmp(cp, id, CAPABLEN) == 0) {
221 while (*cp && *cp != ':' && *cp != '=')
225 for (ret = *area, cp++; *cp && *cp != ':' ;
229 **area = *++cp - '@'; /* fix (efth)*/
234 **area = CTL_ESC('\033');
255 for (i=0 ; *cp && ISDIGIT(*cp) ;
257 i = i * 8 + *cp - '0';
273 while (*cp && *cp != ':')
280 * tgoto - given the cursor motion string cm, make up the string
281 * for the cursor to go to (destcol, destline), and return the string.
282 * Returns "OOPS" if something's gone wrong, or the string otherwise.
285 tgoto(char *cm, int destcol, int destline)
290 int argno = 0, numval;
292 for (rp = ret ; *cm ; cm++) {
297 numval = (argno == 0 ? destline : destcol);
299 *rp++ = numval + incr + *++cm;
311 numval = (argno == 0 ? destline : destcol);
314 *rp++ = '0' + (numval/10);
315 *rp++ = '0' + (numval%10);
333 * tputs - put the string cp out onto the terminal, using the function
334 * outc. This should do padding for the terminal, but I can't find a
335 * terminal that needs padding at the moment...
338 tputs(char *cp, int affcnt, int (*outc)())
340 unsigned long delay = 0;
344 /* do any padding interpretation - left null for MINIX just now */
345 for (delay = 0; *cp && ISDIGIT(*cp) ; cp++)
346 delay = delay * 10 + *cp - '0';
350 usleep(delay*100); /* strictly spoken, it should be *1000 */
354 #endif /* _VMS_POSIX || _OSD_POSIX */