1 /* $Header: /src/pub/tcsh/vms.termcap.c,v 1.8 2005/03/03 16:49:16 kim 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("$Id: vms.termcap.c,v 1.8 2005/03/03 16:49:16 kim Exp $")
13 #if defined(_VMS_POSIX) || defined(_OSD_POSIX)
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.
55 *ptr, /* temporary pointer */
56 tmp[1024]; /* buffer for terminal name */
57 short len = strlen(name);
61 /* Use TERMCAP to override default. */
63 termfile = getenv("TERMCAP");
64 if (termfile == NULL ) termfile = "/etc/termcap";
66 if ((fp = fopen(termfile, "r")) == (FILE *) NULL) {
67 fprintf(stderr, CGETS(31, 1,
68 "Can't open TERMCAP: [%s]\n"), termfile);
69 fprintf(stderr, CGETS(31, 2, "Can't open %s.\n"), termfile);
74 while (fgets(bp, 1024, fp) != NULL) {
75 /* Any line starting with # or NL is skipped as a comment */
76 if ((*bp == '#') || (*bp == '\n')) continue;
78 /* Look for lines which end with two backslashes,
79 and then append the next line. */
80 while (*(cp = &bp[strlen(bp) - 2]) == '\\')
83 /* Skip over any spaces or tabs */
84 for (++cp ; ISSPACE(*cp) ; cp++);
86 /* Make sure "name" matches exactly (efth) */
88 /* Here we might want to look at any aliases as well. We'll use
89 sscanf to look at aliases. These are delimited by '|'. */
91 sscanf(bp,"%[^|]",tmp);
92 if (strncmp(name, tmp, len) == 0) {
95 fprintf(stderr, CGETS(31, 3, "Found %s in %s.\n"), name, termfile);
101 while ((ptr = strchr(ptr,'|')) != NULL) {
103 if (strchr(ptr,'|') == NULL) break;
104 sscanf(ptr,"%[^|]",tmp);
105 if (strncmp(name, tmp, len) == 0) {
108 fprintf(stderr,CGETS(31, 3, "Found %s in %s.\n"), name, termfile);
115 /* If we get here, then we haven't found a match. */
118 fprintf(stderr,CGETS(31, 4, "No match found for %s in file %s\n"),
127 * tgetnum - get the numeric terminal capability corresponding
128 * to id. Returns the value, -1 if invalid.
137 if ((cp = capab) == NULL || id == NULL)
141 for (++cp ; *cp ; cp++) {
144 if (strncmp(cp, id, CAPABLEN) == 0) {
145 while (*cp && *cp != ':' && *cp != '#')
149 for (ret = 0, cp++ ; *cp && ISDIGIT(*cp) ; cp++)
150 ret = ret * 10 + *cp - '0';
153 while (*cp && *cp != ':')
160 * tgetflag - get the boolean flag corresponding to id. Returns -1
161 * if invalid, 0 if the flag is not in termcap entry, or 1 if it is
170 if ((cp = capab) == NULL || id == NULL)
174 for (++cp ; *cp ; cp++) {
177 if (strncmp(cp, id, CAPABLEN) == 0)
179 while (*cp && *cp != ':')
186 * tgetstr - get the string capability corresponding to id and place
187 * it in area (advancing area at same time). Expand escape sequences
188 * etc. Returns the string, or NULL if it can't do it.
199 if ((cp = capab) == NULL || id == NULL)
203 for (++cp ; *cp ; cp++) {
206 if (strncmp(cp, id, CAPABLEN) == 0) {
207 while (*cp && *cp != ':' && *cp != '=')
211 for (ret = *area, cp++; *cp && *cp != ':' ;
215 **area = *++cp - '@'; /* fix (efth)*/
220 **area = CTL_ESC('\033');
241 for (i=0 ; *cp && ISDIGIT(*cp) ;
243 i = i * 8 + *cp - '0';
259 while (*cp && *cp != ':')
266 * tgoto - given the cursor motion string cm, make up the string
267 * for the cursor to go to (destcol, destline), and return the string.
268 * Returns "OOPS" if something's gone wrong, or the string otherwise.
271 tgoto(cm, destcol, destline)
279 int argno = 0, numval;
281 for (rp = ret ; *cm ; cm++) {
286 numval = (argno == 0 ? destline : destcol);
288 *rp++ = numval + incr + *++cm;
300 numval = (argno == 0 ? destline : destcol);
303 *rp++ = '0' + (numval/10);
304 *rp++ = '0' + (numval%10);
322 * tputs - put the string cp out onto the terminal, using the function
323 * outc. This should do padding for the terminal, but I can't find a
324 * terminal that needs padding at the moment...
327 tputs(cp, affcnt, outc)
332 unsigned long delay = 0;
336 /* do any padding interpretation - left null for MINIX just now */
337 for (delay = 0; *cp && ISDIGIT(*cp) ; cp++)
338 delay = delay * 10 + *cp - '0';
342 usleep(delay*100); /* strictly spoken, it should be *1000 */
346 #endif /* _VMS_POSIX || _OSD_POSIX */