13678Sroot /* Copyright (c) 1979 Regents of the University of California */
2*3841Ssam #define	BUFSIZ	1024
3*3841Ssam #define MAXHOP	32	/* max number of tc= indirections */
43678Sroot 
53678Sroot #include <ctype.h>
6*3841Ssam #include "local/uparm.h"
73678Sroot /*
8*3841Ssam  * termcap - routines for dealing with the terminal capability data base
93678Sroot  *
103678Sroot  * BUG:		Should use a "last" pointer in tbuf, so that searching
113678Sroot  *		for capabilities alphabetically would not be a n**2/2
123678Sroot  *		process when large numbers of capabilities are given.
13*3841Ssam  * Note:	If we add a last pointer now we will screw up the
14*3841Ssam  *		tc capability. We really should compile termcap.
153678Sroot  *
163678Sroot  * Essentially all the work here is scanning and decoding escapes
173678Sroot  * in string capabilities.  We don't use stdio because the editor
183678Sroot  * doesn't, and because living w/o it is not hard.
193678Sroot  */
203678Sroot 
21*3841Ssam #define PRINTCAP
223678Sroot 
23*3841Ssam #ifdef PRINTCAP
24*3841Ssam #define tgetent	pgetent
25*3841Ssam #define tskip	pskip
26*3841Ssam #define tgetstr	pgetstr
27*3841Ssam #define tdecode pdecode
28*3841Ssam #define tgetnum	pgetnum
29*3841Ssam #define	tgetflag pgetflag
30*3841Ssam #define tdecode pdecode
31*3841Ssam #define tnchktc	pnchktc
32*3841Ssam #define	tnamatch pnamatch
33*3841Ssam #undef E_TERMCAP
34*3841Ssam #define E_TERMCAP "/etc/printcap"
35*3841Ssam #define V6
36*3841Ssam #endif
37*3841Ssam 
38*3841Ssam static	char *tbuf;
39*3841Ssam static	int hopcount;	/* detect infinite loops in termcap, init 0 */
40*3841Ssam char	*tskip();
41*3841Ssam char	*tgetstr();
42*3841Ssam char	*tdecode();
43*3841Ssam char	*getenv();
44*3841Ssam 
453678Sroot /*
46*3841Ssam  * Get an entry for terminal name in buffer bp,
47*3841Ssam  * from the termcap file.  Parse is very rudimentary;
483678Sroot  * we just notice escaped newlines.
493678Sroot  */
50*3841Ssam tgetent(bp, name)
513678Sroot 	char *bp, *name;
523678Sroot {
533678Sroot 	register char *cp;
543678Sroot 	register int c;
553678Sroot 	register int i = 0, cnt = 0;
563678Sroot 	char ibuf[BUFSIZ];
57*3841Ssam 	char *cp2;
583678Sroot 	int tf;
593678Sroot 
60*3841Ssam 	tbuf = bp;
61*3841Ssam 	tf = 0;
62*3841Ssam #ifndef V6
63*3841Ssam 	cp = getenv("TERMCAP");
64*3841Ssam 	/*
65*3841Ssam 	 * TERMCAP can have one of two things in it. It can be the
66*3841Ssam 	 * name of a file to use instead of /etc/termcap. In this
67*3841Ssam 	 * case it better start with a "/". Or it can be an entry to
68*3841Ssam 	 * use so we don't have to read the file. In this case it
69*3841Ssam 	 * has to already have the newlines crunched out.
70*3841Ssam 	 */
71*3841Ssam 	if (cp && *cp) {
72*3841Ssam 		if (*cp!='/') {
73*3841Ssam 			cp2 = getenv("TERM");
74*3841Ssam 			if (cp2==(char *) 0 || strcmp(name,cp2)==0) {
75*3841Ssam 				strcpy(bp,cp);
76*3841Ssam 				return(tnchktc());
77*3841Ssam 			} else {
78*3841Ssam 				tf = open(E_TERMCAP, 0);
79*3841Ssam 			}
80*3841Ssam 		} else
81*3841Ssam 			tf = open(cp, 0);
82*3841Ssam 	}
83*3841Ssam 	if (tf==0)
84*3841Ssam 		tf = open(E_TERMCAP, 0);
85*3841Ssam #else
86*3841Ssam 	tf = open(E_TERMCAP, 0);
87*3841Ssam #endif
883678Sroot 	if (tf < 0)
893678Sroot 		return (-1);
903678Sroot 	for (;;) {
913678Sroot 		cp = bp;
923678Sroot 		for (;;) {
933678Sroot 			if (i == cnt) {
943678Sroot 				cnt = read(tf, ibuf, BUFSIZ);
953678Sroot 				if (cnt <= 0) {
963678Sroot 					close(tf);
973678Sroot 					return (0);
983678Sroot 				}
993678Sroot 				i = 0;
1003678Sroot 			}
1013678Sroot 			c = ibuf[i++];
1023678Sroot 			if (c == '\n') {
1033678Sroot 				if (cp > bp && cp[-1] == '\\'){
1043678Sroot 					cp--;
1053678Sroot 					continue;
1063678Sroot 				}
1073678Sroot 				break;
1083678Sroot 			}
109*3841Ssam 			if (cp >= bp+BUFSIZ) {
110*3841Ssam 				write(2,"Termcap entry too long\n", 23);
111*3841Ssam 				break;
112*3841Ssam 			} else
113*3841Ssam 				*cp++ = c;
1143678Sroot 		}
1153678Sroot 		*cp = 0;
1163678Sroot 
1173678Sroot 		/*
1183678Sroot 		 * The real work for the match.
1193678Sroot 		 */
120*3841Ssam 		if (tnamatch(name)) {
1213678Sroot 			close(tf);
122*3841Ssam 			return(tnchktc());
1233678Sroot 		}
1243678Sroot 	}
1253678Sroot }
1263678Sroot 
1273678Sroot /*
128*3841Ssam  * tnchktc: check the last entry, see if it's tc=xxx. If so,
129*3841Ssam  * recursively find xxx and append that entry (minus the names)
130*3841Ssam  * to take the place of the tc=xxx entry. This allows termcap
131*3841Ssam  * entries to say "like an HP2621 but doesn't turn on the labels".
132*3841Ssam  * Note that this works because of the left to right scan.
133*3841Ssam  */
134*3841Ssam tnchktc()
135*3841Ssam {
136*3841Ssam 	register char *p, *q;
137*3841Ssam 	char tcname[16];	/* name of similar terminal */
138*3841Ssam 	char tcbuf[BUFSIZ];
139*3841Ssam 	char *holdtbuf = tbuf;
140*3841Ssam 	int l;
141*3841Ssam 
142*3841Ssam 	p = tbuf + strlen(tbuf) - 2;	/* before the last colon */
143*3841Ssam 	while (*--p != ':')
144*3841Ssam 		if (p<tbuf) {
145*3841Ssam 			write(2, "Bad termcap entry\n", 18);
146*3841Ssam 			return (0);
147*3841Ssam 		}
148*3841Ssam 	p++;
149*3841Ssam 	/* p now points to beginning of last field */
150*3841Ssam 	if (p[0] != 't' || p[1] != 'c')
151*3841Ssam 		return(1);
152*3841Ssam 	strcpy(tcname,p+3);
153*3841Ssam 	q = tcname;
154*3841Ssam 	while (q && *q != ':')
155*3841Ssam 		q++;
156*3841Ssam 	*q = 0;
157*3841Ssam 	if (++hopcount > MAXHOP) {
158*3841Ssam 		write(2, "Infinite tc= loop\n", 18);
159*3841Ssam 		return (0);
160*3841Ssam 	}
161*3841Ssam 	if (tgetent(tcbuf, tcname) != 1)
162*3841Ssam 		return(0);
163*3841Ssam 	for (q=tcbuf; *q != ':'; q++)
164*3841Ssam 		;
165*3841Ssam 	l = p - holdtbuf + strlen(q);
166*3841Ssam 	if (l > BUFSIZ) {
167*3841Ssam 		write(2, "Termcap entry too long\n", 23);
168*3841Ssam 		q[BUFSIZ - (p-tbuf)] = 0;
169*3841Ssam 	}
170*3841Ssam 	strcpy(p, q+1);
171*3841Ssam 	tbuf = holdtbuf;
172*3841Ssam 	return(1);
173*3841Ssam }
174*3841Ssam 
175*3841Ssam /*
176*3841Ssam  * Tnamatch deals with name matching.  The first field of the termcap
1773678Sroot  * entry is a sequence of names separated by |'s, so we compare
1783678Sroot  * against each such name.  The normal : terminator after the last
1793678Sroot  * name (before the first field) stops us.
1803678Sroot  */
181*3841Ssam tnamatch(np)
1823678Sroot 	char *np;
1833678Sroot {
1843678Sroot 	register char *Np, *Bp;
1853678Sroot 
186*3841Ssam 	Bp = tbuf;
187*3841Ssam 	if (*Bp == '#')
188*3841Ssam 		return(0);
1893678Sroot 	for (;;) {
1903678Sroot 		for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
1913678Sroot 			continue;
1923678Sroot 		if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
1933678Sroot 			return (1);
1943678Sroot 		while (*Bp && *Bp != ':' && *Bp != '|')
1953678Sroot 			Bp++;
1963678Sroot 		if (*Bp == 0 || *Bp == ':')
1973678Sroot 			return (0);
1983678Sroot 		Bp++;
1993678Sroot 	}
2003678Sroot }
2013678Sroot 
2023678Sroot /*
2033678Sroot  * Skip to the next field.  Notice that this is very dumb, not
2043678Sroot  * knowing about \: escapes or any such.  If necessary, :'s can be put
205*3841Ssam  * into the termcap file in octal.
2063678Sroot  */
2073678Sroot static char *
208*3841Ssam tskip(bp)
2093678Sroot 	register char *bp;
2103678Sroot {
2113678Sroot 
2123678Sroot 	while (*bp && *bp != ':')
2133678Sroot 		bp++;
2143678Sroot 	if (*bp == ':')
2153678Sroot 		bp++;
2163678Sroot 	return (bp);
2173678Sroot }
2183678Sroot 
2193678Sroot /*
2203678Sroot  * Return the (numeric) option id.
2213678Sroot  * Numeric options look like
2223678Sroot  *	li#80
2233678Sroot  * i.e. the option string is separated from the numeric value by
2243678Sroot  * a # character.  If the option is not found we return -1.
2253678Sroot  * Note that we handle octal numbers beginning with 0.
2263678Sroot  */
227*3841Ssam tgetnum(id)
2283678Sroot 	char *id;
2293678Sroot {
2303678Sroot 	register int i, base;
231*3841Ssam 	register char *bp = tbuf;
2323678Sroot 
2333678Sroot 	for (;;) {
234*3841Ssam 		bp = tskip(bp);
2353678Sroot 		if (*bp == 0)
2363678Sroot 			return (-1);
2373678Sroot 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
2383678Sroot 			continue;
239*3841Ssam 		if (*bp == '@')
240*3841Ssam 			return(-1);
2413678Sroot 		if (*bp != '#')
2423678Sroot 			continue;
2433678Sroot 		bp++;
2443678Sroot 		base = 10;
2453678Sroot 		if (*bp == '0')
2463678Sroot 			base = 8;
2473678Sroot 		i = 0;
2483678Sroot 		while (isdigit(*bp))
2493678Sroot 			i *= base, i += *bp++ - '0';
2503678Sroot 		return (i);
2513678Sroot 	}
2523678Sroot }
2533678Sroot 
2543678Sroot /*
2553678Sroot  * Handle a flag option.
2563678Sroot  * Flag options are given "naked", i.e. followed by a : or the end
2573678Sroot  * of the buffer.  Return 1 if we find the option, or 0 if it is
2583678Sroot  * not given.
2593678Sroot  */
260*3841Ssam tgetflag(id)
2613678Sroot 	char *id;
2623678Sroot {
263*3841Ssam 	register char *bp = tbuf;
2643678Sroot 
2653678Sroot 	for (;;) {
266*3841Ssam 		bp = tskip(bp);
2673678Sroot 		if (!*bp)
2683678Sroot 			return (0);
269*3841Ssam 		if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
270*3841Ssam 			if (!*bp || *bp == ':')
271*3841Ssam 				return (1);
272*3841Ssam 			else if (*bp == '@')
273*3841Ssam 				return(0);
274*3841Ssam 		}
2753678Sroot 	}
2763678Sroot }
2773678Sroot 
2783678Sroot /*
2793678Sroot  * Get a string valued option.
2803678Sroot  * These are given as
2813678Sroot  *	cl=^Z
2823678Sroot  * Much decoding is done on the strings, and the strings are
2833678Sroot  * placed in area, which is a ref parameter which is updated.
2843678Sroot  * No checking on area overflow.
2853678Sroot  */
2863678Sroot char *
287*3841Ssam tgetstr(id, area)
2883678Sroot 	char *id, **area;
2893678Sroot {
290*3841Ssam 	register char *bp = tbuf;
2913678Sroot 
2923678Sroot 	for (;;) {
293*3841Ssam 		bp = tskip(bp);
2943678Sroot 		if (!*bp)
2953678Sroot 			return (0);
2963678Sroot 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
2973678Sroot 			continue;
298*3841Ssam 		if (*bp == '@')
299*3841Ssam 			return(0);
3003678Sroot 		if (*bp != '=')
3013678Sroot 			continue;
3023678Sroot 		bp++;
303*3841Ssam 		return (tdecode(bp, area));
3043678Sroot 	}
3053678Sroot }
3063678Sroot 
3073678Sroot /*
3083678Sroot  * Tdecode does the grung work to decode the
3093678Sroot  * string capability escapes.
3103678Sroot  */
3113678Sroot static char *
312*3841Ssam tdecode(str, area)
3133678Sroot 	register char *str;
3143678Sroot 	char **area;
3153678Sroot {
3163678Sroot 	register char *cp;
3173678Sroot 	register int c;
3183678Sroot 	register char *dp;
3193678Sroot 	int i;
3203678Sroot 
3213678Sroot 	cp = *area;
3223678Sroot 	while ((c = *str++) && c != ':') {
3233678Sroot 		switch (c) {
3243678Sroot 
3253678Sroot 		case '^':
3263678Sroot 			c = *str++ & 037;
3273678Sroot 			break;
3283678Sroot 
3293678Sroot 		case '\\':
3303678Sroot 			dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
3313678Sroot 			c = *str++;
3323678Sroot nextc:
3333678Sroot 			if (*dp++ == c) {
3343678Sroot 				c = *dp++;
3353678Sroot 				break;
3363678Sroot 			}
3373678Sroot 			dp++;
3383678Sroot 			if (*dp)
3393678Sroot 				goto nextc;
3403678Sroot 			if (isdigit(c)) {
3413678Sroot 				c -= '0', i = 2;
3423678Sroot 				do
3433678Sroot 					c <<= 3, c |= *str++ - '0';
3443678Sroot 				while (--i && isdigit(*str));
3453678Sroot 			}
3463678Sroot 			break;
3473678Sroot 		}
3483678Sroot 		*cp++ = c;
3493678Sroot 	}
3503678Sroot 	*cp++ = 0;
3513678Sroot 	str = *area;
3523678Sroot 	*area = cp;
3533678Sroot 	return (str);
3543678Sroot }
355