1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
3*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
7*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
8*7c478bd9Sstevel@tonic-gate 
9*7c478bd9Sstevel@tonic-gate /*
10*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
11*7c478bd9Sstevel@tonic-gate  * All rights reserved.  The Berkeley software License Agreement
12*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
13*7c478bd9Sstevel@tonic-gate  */
14*7c478bd9Sstevel@tonic-gate 
15*7c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
16*7c478bd9Sstevel@tonic-gate 
17*7c478bd9Sstevel@tonic-gate #if 0
18*7c478bd9Sstevel@tonic-gate static char
19*7c478bd9Sstevel@tonic-gate sccsid[] = "@(#)termcap.c 1.11 88/02/08 SMI"; /* from UCB 5.1 6/5/85 */
20*7c478bd9Sstevel@tonic-gate #endif
21*7c478bd9Sstevel@tonic-gate 
22*7c478bd9Sstevel@tonic-gate #define	BUFSIZ		1024
23*7c478bd9Sstevel@tonic-gate #define	MAXHOP		32	/* max number of tc= indirections */
24*7c478bd9Sstevel@tonic-gate #define	E_TERMCAP	"/etc/termcap"
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
27*7c478bd9Sstevel@tonic-gate #include <unistd.h>
28*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
29*7c478bd9Sstevel@tonic-gate #include <stddef.h>
30*7c478bd9Sstevel@tonic-gate #include <string.h>
31*7c478bd9Sstevel@tonic-gate #include <strings.h>
32*7c478bd9Sstevel@tonic-gate #include <ctype.h>
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  * termcap - routines for dealing with the terminal capability data base
36*7c478bd9Sstevel@tonic-gate  *
37*7c478bd9Sstevel@tonic-gate  * BUG:		Should use a "last" pointer in tbuf, so that searching
38*7c478bd9Sstevel@tonic-gate  *		for capabilities alphabetically would not be a n**2/2
39*7c478bd9Sstevel@tonic-gate  *		process when large numbers of capabilities are given.
40*7c478bd9Sstevel@tonic-gate  * Note:	If we add a last pointer now we will screw up the
41*7c478bd9Sstevel@tonic-gate  *		tc capability. We really should compile termcap.
42*7c478bd9Sstevel@tonic-gate  *
43*7c478bd9Sstevel@tonic-gate  * Essentially all the work here is scanning and decoding escapes
44*7c478bd9Sstevel@tonic-gate  * in string capabilities.  We don't use stdio because the editor
45*7c478bd9Sstevel@tonic-gate  * doesn't, and because living w/o it is not hard.
46*7c478bd9Sstevel@tonic-gate  */
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate static	char *tbuf;
49*7c478bd9Sstevel@tonic-gate static	int hopcount;	/* detect infinite loops in termcap, init 0 */
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate /* forward declarations */
52*7c478bd9Sstevel@tonic-gate static char *tdecode(char *, char **);
53*7c478bd9Sstevel@tonic-gate static void tngetsize(char *);
54*7c478bd9Sstevel@tonic-gate static char *tskip(char *bp);
55*7c478bd9Sstevel@tonic-gate static char *appendsmalldec(char *, int);
56*7c478bd9Sstevel@tonic-gate int tnamatch(char *);
57*7c478bd9Sstevel@tonic-gate int tnchktc(void);
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate /*
60*7c478bd9Sstevel@tonic-gate  * Get an entry for terminal name in buffer bp,
61*7c478bd9Sstevel@tonic-gate  * from the termcap file.  Parse is very rudimentary;
62*7c478bd9Sstevel@tonic-gate  * we just notice escaped newlines.
63*7c478bd9Sstevel@tonic-gate  */
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate int
tgetent(char * bp,char * name)66*7c478bd9Sstevel@tonic-gate tgetent(char *bp, char *name)
67*7c478bd9Sstevel@tonic-gate {
68*7c478bd9Sstevel@tonic-gate 	char *cp;
69*7c478bd9Sstevel@tonic-gate 	int c;
70*7c478bd9Sstevel@tonic-gate 	int i = 0;
71*7c478bd9Sstevel@tonic-gate 	ssize_t cnt = 0;
72*7c478bd9Sstevel@tonic-gate 	char ibuf[BUFSIZ];
73*7c478bd9Sstevel@tonic-gate 	int tf;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	tbuf = bp;
76*7c478bd9Sstevel@tonic-gate 	tf = -1;
77*7c478bd9Sstevel@tonic-gate #ifndef V6
78*7c478bd9Sstevel@tonic-gate 	cp = getenv("TERMCAP");
79*7c478bd9Sstevel@tonic-gate 	/*
80*7c478bd9Sstevel@tonic-gate 	 * TERMCAP can have one of two things in it. It can be the
81*7c478bd9Sstevel@tonic-gate 	 * name of a file to use instead of /etc/termcap. In this
82*7c478bd9Sstevel@tonic-gate 	 * case it better start with a "/". Or it can be an entry to
83*7c478bd9Sstevel@tonic-gate 	 * use so we don't have to read the file. In this case it
84*7c478bd9Sstevel@tonic-gate 	 * has to already have the newlines crunched out.
85*7c478bd9Sstevel@tonic-gate 	 */
86*7c478bd9Sstevel@tonic-gate 	if (cp && *cp) {
87*7c478bd9Sstevel@tonic-gate 		if (*cp == '/') {
88*7c478bd9Sstevel@tonic-gate 			tf = open(cp, 0);
89*7c478bd9Sstevel@tonic-gate 		} else {
90*7c478bd9Sstevel@tonic-gate 			tbuf = cp;
91*7c478bd9Sstevel@tonic-gate 			c = tnamatch(name);
92*7c478bd9Sstevel@tonic-gate 			tbuf = bp;
93*7c478bd9Sstevel@tonic-gate 			if (c) {
94*7c478bd9Sstevel@tonic-gate 				(void) strcpy(bp, cp);
95*7c478bd9Sstevel@tonic-gate 				return (tnchktc());
96*7c478bd9Sstevel@tonic-gate 			}
97*7c478bd9Sstevel@tonic-gate 		}
98*7c478bd9Sstevel@tonic-gate 	}
99*7c478bd9Sstevel@tonic-gate 	if (tf < 0)
100*7c478bd9Sstevel@tonic-gate 		tf = open(E_TERMCAP, 0);
101*7c478bd9Sstevel@tonic-gate #else
102*7c478bd9Sstevel@tonic-gate 	tf = open(E_TERMCAP, 0);
103*7c478bd9Sstevel@tonic-gate #endif
104*7c478bd9Sstevel@tonic-gate 	if (tf < 0)
105*7c478bd9Sstevel@tonic-gate 		return (-1);
106*7c478bd9Sstevel@tonic-gate 	for (;;) {
107*7c478bd9Sstevel@tonic-gate 		cp = bp;
108*7c478bd9Sstevel@tonic-gate 		for (;;) {
109*7c478bd9Sstevel@tonic-gate 			if (i == cnt) {
110*7c478bd9Sstevel@tonic-gate 				cnt = read(tf, ibuf, BUFSIZ);
111*7c478bd9Sstevel@tonic-gate 				if (cnt <= 0) {
112*7c478bd9Sstevel@tonic-gate 					(void) close(tf);
113*7c478bd9Sstevel@tonic-gate 					return (0);
114*7c478bd9Sstevel@tonic-gate 				}
115*7c478bd9Sstevel@tonic-gate 				i = 0;
116*7c478bd9Sstevel@tonic-gate 			}
117*7c478bd9Sstevel@tonic-gate 			c = ibuf[i++];
118*7c478bd9Sstevel@tonic-gate 			if (c == '\n') {
119*7c478bd9Sstevel@tonic-gate 				if (cp > bp && cp[-1] == '\\') {
120*7c478bd9Sstevel@tonic-gate 					cp--;
121*7c478bd9Sstevel@tonic-gate 					continue;
122*7c478bd9Sstevel@tonic-gate 				}
123*7c478bd9Sstevel@tonic-gate 				break;
124*7c478bd9Sstevel@tonic-gate 			}
125*7c478bd9Sstevel@tonic-gate 			if (cp >= bp+BUFSIZ) {
126*7c478bd9Sstevel@tonic-gate 				(void) write(2, "Termcap entry too long\n", 23);
127*7c478bd9Sstevel@tonic-gate 				break;
128*7c478bd9Sstevel@tonic-gate 			} else
129*7c478bd9Sstevel@tonic-gate 				*cp++ = (char) c;
130*7c478bd9Sstevel@tonic-gate 		}
131*7c478bd9Sstevel@tonic-gate 		*cp = 0;
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 		/*
134*7c478bd9Sstevel@tonic-gate 		 * The real work for the match.
135*7c478bd9Sstevel@tonic-gate 		 */
136*7c478bd9Sstevel@tonic-gate 		if (tnamatch(name)) {
137*7c478bd9Sstevel@tonic-gate 			(void) close(tf);
138*7c478bd9Sstevel@tonic-gate 			return (tnchktc());
139*7c478bd9Sstevel@tonic-gate 		}
140*7c478bd9Sstevel@tonic-gate 	}
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate /*
144*7c478bd9Sstevel@tonic-gate  * tnchktc: check the last entry, see if it's tc=xxx. If so,
145*7c478bd9Sstevel@tonic-gate  * recursively find xxx and append that entry (minus the names)
146*7c478bd9Sstevel@tonic-gate  * to take the place of the tc=xxx entry. This allows termcap
147*7c478bd9Sstevel@tonic-gate  * entries to say "like an HP2621 but doesn't turn on the labels".
148*7c478bd9Sstevel@tonic-gate  * Note that this works because of the left to right scan.
149*7c478bd9Sstevel@tonic-gate  */
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate int
tnchktc(void)152*7c478bd9Sstevel@tonic-gate tnchktc(void)
153*7c478bd9Sstevel@tonic-gate {
154*7c478bd9Sstevel@tonic-gate 	char *p, *q;
155*7c478bd9Sstevel@tonic-gate 	char tcname[16];	/* name of similar terminal */
156*7c478bd9Sstevel@tonic-gate 	char tcbuf[BUFSIZ];
157*7c478bd9Sstevel@tonic-gate 	char *holdtbuf = tbuf;
158*7c478bd9Sstevel@tonic-gate 	ptrdiff_t l;
159*7c478bd9Sstevel@tonic-gate 
160*7c478bd9Sstevel@tonic-gate 	p = tbuf + strlen(tbuf) - 2;	/* before the last colon */
161*7c478bd9Sstevel@tonic-gate 	while (*--p != ':')
162*7c478bd9Sstevel@tonic-gate 		if (p < tbuf) {
163*7c478bd9Sstevel@tonic-gate 			(void) write(2, "Bad termcap entry\n", 18);
164*7c478bd9Sstevel@tonic-gate 			return (0);
165*7c478bd9Sstevel@tonic-gate 		}
166*7c478bd9Sstevel@tonic-gate 	p++;
167*7c478bd9Sstevel@tonic-gate 	/* p now points to beginning of last field */
168*7c478bd9Sstevel@tonic-gate 	if (p[0] != 't' || p[1] != 'c') {
169*7c478bd9Sstevel@tonic-gate 		tngetsize(tbuf);
170*7c478bd9Sstevel@tonic-gate 		return (1);
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 	(void) strcpy(tcname, p+3);
173*7c478bd9Sstevel@tonic-gate 	q = tcname;
174*7c478bd9Sstevel@tonic-gate 	while (*q && *q != ':')
175*7c478bd9Sstevel@tonic-gate 		q++;
176*7c478bd9Sstevel@tonic-gate 	*q = 0;
177*7c478bd9Sstevel@tonic-gate 	if (++hopcount > MAXHOP) {
178*7c478bd9Sstevel@tonic-gate 		(void) write(2, "Infinite tc= loop\n", 18);
179*7c478bd9Sstevel@tonic-gate 		return (0);
180*7c478bd9Sstevel@tonic-gate 	}
181*7c478bd9Sstevel@tonic-gate 	if (tgetent(tcbuf, tcname) != 1) {
182*7c478bd9Sstevel@tonic-gate 		hopcount = 0;		/* unwind recursion */
183*7c478bd9Sstevel@tonic-gate 		return (0);
184*7c478bd9Sstevel@tonic-gate 	}
185*7c478bd9Sstevel@tonic-gate 	for (q = tcbuf; *q != ':'; q++)
186*7c478bd9Sstevel@tonic-gate 		;
187*7c478bd9Sstevel@tonic-gate 	l = p - holdtbuf + strlen(q);
188*7c478bd9Sstevel@tonic-gate 	if (l > BUFSIZ) {
189*7c478bd9Sstevel@tonic-gate 		(void) write(2, "Termcap entry too long\n", 23);
190*7c478bd9Sstevel@tonic-gate 		q[BUFSIZ - (p-tbuf)] = 0;
191*7c478bd9Sstevel@tonic-gate 	}
192*7c478bd9Sstevel@tonic-gate 	(void) strcpy(p, q+1);
193*7c478bd9Sstevel@tonic-gate 	tbuf = holdtbuf;
194*7c478bd9Sstevel@tonic-gate 	hopcount = 0;			/* unwind recursion */
195*7c478bd9Sstevel@tonic-gate 	tngetsize(tbuf);
196*7c478bd9Sstevel@tonic-gate 	return (1);
197*7c478bd9Sstevel@tonic-gate }
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate /*
200*7c478bd9Sstevel@tonic-gate  * Tnamatch deals with name matching.  The first field of the termcap
201*7c478bd9Sstevel@tonic-gate  * entry is a sequence of names separated by |'s, so we compare
202*7c478bd9Sstevel@tonic-gate  * against each such name.  The normal : terminator after the last
203*7c478bd9Sstevel@tonic-gate  * name (before the first field) stops us.
204*7c478bd9Sstevel@tonic-gate  */
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate int
tnamatch(char * np)207*7c478bd9Sstevel@tonic-gate tnamatch(char *np)
208*7c478bd9Sstevel@tonic-gate {
209*7c478bd9Sstevel@tonic-gate 	char *Np, *Bp;
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 	Bp = tbuf;
212*7c478bd9Sstevel@tonic-gate 	if (*Bp == '#')
213*7c478bd9Sstevel@tonic-gate 		return (0);
214*7c478bd9Sstevel@tonic-gate 	for (;;) {
215*7c478bd9Sstevel@tonic-gate 		for (Np = np; *Np && *Bp == *Np; Bp++, Np++)
216*7c478bd9Sstevel@tonic-gate 			continue;
217*7c478bd9Sstevel@tonic-gate 		if (*Np == 0 && (*Bp == '|' || *Bp == ':' || *Bp == 0))
218*7c478bd9Sstevel@tonic-gate 			return (1);
219*7c478bd9Sstevel@tonic-gate 		while (*Bp && *Bp != ':' && *Bp != '|')
220*7c478bd9Sstevel@tonic-gate 			Bp++;
221*7c478bd9Sstevel@tonic-gate 		if (*Bp == 0 || *Bp == ':')
222*7c478bd9Sstevel@tonic-gate 			return (0);
223*7c478bd9Sstevel@tonic-gate 		Bp++;
224*7c478bd9Sstevel@tonic-gate 	}
225*7c478bd9Sstevel@tonic-gate }
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate /*
228*7c478bd9Sstevel@tonic-gate  * Skip to the next field.  Notice that this is very dumb, not
229*7c478bd9Sstevel@tonic-gate  * knowing about \: escapes or any such.  If necessary, :'s can be put
230*7c478bd9Sstevel@tonic-gate  * into the termcap file in octal.
231*7c478bd9Sstevel@tonic-gate  */
232*7c478bd9Sstevel@tonic-gate 
233*7c478bd9Sstevel@tonic-gate static char *
tskip(char * bp)234*7c478bd9Sstevel@tonic-gate tskip(char *bp)
235*7c478bd9Sstevel@tonic-gate {
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 	while (*bp && *bp != ':')
238*7c478bd9Sstevel@tonic-gate 		bp++;
239*7c478bd9Sstevel@tonic-gate 	if (*bp == ':') {
240*7c478bd9Sstevel@tonic-gate 		do {
241*7c478bd9Sstevel@tonic-gate 			bp++;
242*7c478bd9Sstevel@tonic-gate 			while (isspace(*bp))
243*7c478bd9Sstevel@tonic-gate 				bp++;
244*7c478bd9Sstevel@tonic-gate 		} while (*bp == ':');
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate 	return (bp);
247*7c478bd9Sstevel@tonic-gate }
248*7c478bd9Sstevel@tonic-gate 
249*7c478bd9Sstevel@tonic-gate /*
250*7c478bd9Sstevel@tonic-gate  * Return the (numeric) option id.
251*7c478bd9Sstevel@tonic-gate  * Numeric options look like
252*7c478bd9Sstevel@tonic-gate  *	li#80
253*7c478bd9Sstevel@tonic-gate  * i.e. the option string is separated from the numeric value by
254*7c478bd9Sstevel@tonic-gate  * a # character.  If the option is not found we return -1.
255*7c478bd9Sstevel@tonic-gate  * Note that we handle octal numbers beginning with 0.
256*7c478bd9Sstevel@tonic-gate  */
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate int
tgetnum(char * id)259*7c478bd9Sstevel@tonic-gate tgetnum(char *id)
260*7c478bd9Sstevel@tonic-gate {
261*7c478bd9Sstevel@tonic-gate 	int i, base;
262*7c478bd9Sstevel@tonic-gate 	char *bp = tbuf;
263*7c478bd9Sstevel@tonic-gate 
264*7c478bd9Sstevel@tonic-gate 	for (;;) {
265*7c478bd9Sstevel@tonic-gate 		bp = tskip(bp);
266*7c478bd9Sstevel@tonic-gate 		if (*bp == 0)
267*7c478bd9Sstevel@tonic-gate 			return (-1);
268*7c478bd9Sstevel@tonic-gate 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
269*7c478bd9Sstevel@tonic-gate 			continue;
270*7c478bd9Sstevel@tonic-gate 		if (*bp == '@')
271*7c478bd9Sstevel@tonic-gate 			return (-1);
272*7c478bd9Sstevel@tonic-gate 		if (*bp != '#')
273*7c478bd9Sstevel@tonic-gate 			continue;
274*7c478bd9Sstevel@tonic-gate 		bp++;
275*7c478bd9Sstevel@tonic-gate 		base = 10;
276*7c478bd9Sstevel@tonic-gate 		if (*bp == '0')
277*7c478bd9Sstevel@tonic-gate 			base = 8;
278*7c478bd9Sstevel@tonic-gate 		i = 0;
279*7c478bd9Sstevel@tonic-gate 		while (isdigit(*bp))
280*7c478bd9Sstevel@tonic-gate 			i *= base, i += *bp++ - '0';
281*7c478bd9Sstevel@tonic-gate 		return (i);
282*7c478bd9Sstevel@tonic-gate 	}
283*7c478bd9Sstevel@tonic-gate }
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate /*
286*7c478bd9Sstevel@tonic-gate  * Handle a flag option.
287*7c478bd9Sstevel@tonic-gate  * Flag options are given "naked", i.e. followed by a : or the end
288*7c478bd9Sstevel@tonic-gate  * of the buffer.  Return 1 if we find the option, or 0 if it is
289*7c478bd9Sstevel@tonic-gate  * not given.
290*7c478bd9Sstevel@tonic-gate  */
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate int
tgetflag(char * id)293*7c478bd9Sstevel@tonic-gate tgetflag(char *id)
294*7c478bd9Sstevel@tonic-gate {
295*7c478bd9Sstevel@tonic-gate 	char *bp = tbuf;
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate 	for (;;) {
298*7c478bd9Sstevel@tonic-gate 		bp = tskip(bp);
299*7c478bd9Sstevel@tonic-gate 		if (!*bp)
300*7c478bd9Sstevel@tonic-gate 			return (0);
301*7c478bd9Sstevel@tonic-gate 		if (*bp++ == id[0] && *bp != 0 && *bp++ == id[1]) {
302*7c478bd9Sstevel@tonic-gate 			if (!*bp || *bp == ':')
303*7c478bd9Sstevel@tonic-gate 				return (1);
304*7c478bd9Sstevel@tonic-gate 			else if (*bp == '@')
305*7c478bd9Sstevel@tonic-gate 				return (0);
306*7c478bd9Sstevel@tonic-gate 		}
307*7c478bd9Sstevel@tonic-gate 	}
308*7c478bd9Sstevel@tonic-gate }
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate /*
311*7c478bd9Sstevel@tonic-gate  * Get a string valued option.
312*7c478bd9Sstevel@tonic-gate  * These are given as
313*7c478bd9Sstevel@tonic-gate  *	cl=^Z
314*7c478bd9Sstevel@tonic-gate  * Much decoding is done on the strings, and the strings are
315*7c478bd9Sstevel@tonic-gate  * placed in area, which is a ref parameter which is updated.
316*7c478bd9Sstevel@tonic-gate  * No checking on area overflow.
317*7c478bd9Sstevel@tonic-gate  */
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate char *
tgetstr(char * id,char ** area)320*7c478bd9Sstevel@tonic-gate tgetstr(char *id, char **area)
321*7c478bd9Sstevel@tonic-gate {
322*7c478bd9Sstevel@tonic-gate 	char *bp = tbuf;
323*7c478bd9Sstevel@tonic-gate 
324*7c478bd9Sstevel@tonic-gate 	for (;;) {
325*7c478bd9Sstevel@tonic-gate 		bp = tskip(bp);
326*7c478bd9Sstevel@tonic-gate 		if (!*bp)
327*7c478bd9Sstevel@tonic-gate 			return (0);
328*7c478bd9Sstevel@tonic-gate 		if (*bp++ != id[0] || *bp == 0 || *bp++ != id[1])
329*7c478bd9Sstevel@tonic-gate 			continue;
330*7c478bd9Sstevel@tonic-gate 		if (*bp == '@')
331*7c478bd9Sstevel@tonic-gate 			return (0);
332*7c478bd9Sstevel@tonic-gate 		if (*bp != '=')
333*7c478bd9Sstevel@tonic-gate 			continue;
334*7c478bd9Sstevel@tonic-gate 		bp++;
335*7c478bd9Sstevel@tonic-gate 		return (tdecode(bp, area));
336*7c478bd9Sstevel@tonic-gate 	}
337*7c478bd9Sstevel@tonic-gate }
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate /*
340*7c478bd9Sstevel@tonic-gate  * Tdecode does the grung work to decode the
341*7c478bd9Sstevel@tonic-gate  * string capability escapes.
342*7c478bd9Sstevel@tonic-gate  */
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate static char *
tdecode(char * str,char ** area)345*7c478bd9Sstevel@tonic-gate tdecode(char *str, char **area)
346*7c478bd9Sstevel@tonic-gate {
347*7c478bd9Sstevel@tonic-gate 	char *cp;
348*7c478bd9Sstevel@tonic-gate 	int c;
349*7c478bd9Sstevel@tonic-gate 	char *dp;
350*7c478bd9Sstevel@tonic-gate 	int i;
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate 	cp = *area;
353*7c478bd9Sstevel@tonic-gate 	while (((c = *str++) != 0) && c != ':') {
354*7c478bd9Sstevel@tonic-gate 		switch (c) {
355*7c478bd9Sstevel@tonic-gate 
356*7c478bd9Sstevel@tonic-gate 		case '^':
357*7c478bd9Sstevel@tonic-gate 			c = *str++ & 037;
358*7c478bd9Sstevel@tonic-gate 			break;
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate 		case '\\':
361*7c478bd9Sstevel@tonic-gate 			dp = "E\033^^\\\\::n\nr\rt\tb\bf\f";
362*7c478bd9Sstevel@tonic-gate 			c = *str++;
363*7c478bd9Sstevel@tonic-gate nextc:
364*7c478bd9Sstevel@tonic-gate 			if (*dp++ == c) {
365*7c478bd9Sstevel@tonic-gate 				c = *dp++;
366*7c478bd9Sstevel@tonic-gate 				break;
367*7c478bd9Sstevel@tonic-gate 			}
368*7c478bd9Sstevel@tonic-gate 			dp++;
369*7c478bd9Sstevel@tonic-gate 			if (*dp)
370*7c478bd9Sstevel@tonic-gate 				goto nextc;
371*7c478bd9Sstevel@tonic-gate 			if (isdigit(c)) {
372*7c478bd9Sstevel@tonic-gate 				c -= '0', i = 2;
373*7c478bd9Sstevel@tonic-gate 				do
374*7c478bd9Sstevel@tonic-gate 					c <<= 3, c |= *str++ - '0';
375*7c478bd9Sstevel@tonic-gate 				while (--i && isdigit(*str));
376*7c478bd9Sstevel@tonic-gate 			}
377*7c478bd9Sstevel@tonic-gate 			break;
378*7c478bd9Sstevel@tonic-gate 		}
379*7c478bd9Sstevel@tonic-gate 		*cp++ = (char) c;
380*7c478bd9Sstevel@tonic-gate 	}
381*7c478bd9Sstevel@tonic-gate 	*cp++ = 0;
382*7c478bd9Sstevel@tonic-gate 	str = *area;
383*7c478bd9Sstevel@tonic-gate 	*area = cp;
384*7c478bd9Sstevel@tonic-gate 	return (str);
385*7c478bd9Sstevel@tonic-gate }
386*7c478bd9Sstevel@tonic-gate 
387*7c478bd9Sstevel@tonic-gate #include <sys/ioctl.h>
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate static void
tngetsize(char * bp)390*7c478bd9Sstevel@tonic-gate tngetsize(char *bp)
391*7c478bd9Sstevel@tonic-gate {
392*7c478bd9Sstevel@tonic-gate 	struct winsize ws;
393*7c478bd9Sstevel@tonic-gate 	char *np, *cp;
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate 	if (ioctl(1, TIOCGWINSZ, (char *)&ws) < 0)
396*7c478bd9Sstevel@tonic-gate 		return;
397*7c478bd9Sstevel@tonic-gate 	if (ws.ws_row == 0 || ws.ws_col == 0 ||
398*7c478bd9Sstevel@tonic-gate 	    ws.ws_row > 999 || ws.ws_col > 999)
399*7c478bd9Sstevel@tonic-gate 		return;
400*7c478bd9Sstevel@tonic-gate 	cp = index(bp, ':');	/* find start of description */
401*7c478bd9Sstevel@tonic-gate 	bp = rindex(bp, 0);	/* find end of description */
402*7c478bd9Sstevel@tonic-gate 	np = bp + 15;		/* allow enough room for stuff below */
403*7c478bd9Sstevel@tonic-gate 	while (bp >= cp)	/* move description right 15 chars */
404*7c478bd9Sstevel@tonic-gate 		*np-- = *bp--;
405*7c478bd9Sstevel@tonic-gate 	bp++;			/* bp now points to where ':' used to be */
406*7c478bd9Sstevel@tonic-gate 	*bp++ = ':';
407*7c478bd9Sstevel@tonic-gate 	*bp++ = 'l';
408*7c478bd9Sstevel@tonic-gate 	*bp++ = 'i';
409*7c478bd9Sstevel@tonic-gate 	*bp++ = '#';
410*7c478bd9Sstevel@tonic-gate 	bp = appendsmalldec(bp, ws.ws_row);
411*7c478bd9Sstevel@tonic-gate 	*bp++ = ':';
412*7c478bd9Sstevel@tonic-gate 	*bp++ = 'c';
413*7c478bd9Sstevel@tonic-gate 	*bp++ = 'o';
414*7c478bd9Sstevel@tonic-gate 	*bp++ = '#';
415*7c478bd9Sstevel@tonic-gate 	bp = appendsmalldec(bp, ws.ws_col);
416*7c478bd9Sstevel@tonic-gate 	*bp++ = ':';
417*7c478bd9Sstevel@tonic-gate 	while (bp <= np)	/* space fill to start of orig description */
418*7c478bd9Sstevel@tonic-gate 		*bp++ = ' ';
419*7c478bd9Sstevel@tonic-gate }
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate static char *
appendsmalldec(char * bp,int val)422*7c478bd9Sstevel@tonic-gate appendsmalldec(char *bp, int val)
423*7c478bd9Sstevel@tonic-gate {
424*7c478bd9Sstevel@tonic-gate 	int	i;
425*7c478bd9Sstevel@tonic-gate 
426*7c478bd9Sstevel@tonic-gate 	if ((i = val / 100) != 0) {
427*7c478bd9Sstevel@tonic-gate 		*bp++ = '0' + i;
428*7c478bd9Sstevel@tonic-gate 		val %= 100;
429*7c478bd9Sstevel@tonic-gate 		if (0 == val / 10)
430*7c478bd9Sstevel@tonic-gate 			*bp++ = '0'; /* place holder because next test fails */
431*7c478bd9Sstevel@tonic-gate 	}
432*7c478bd9Sstevel@tonic-gate 	if ((i = val / 10) != 0)
433*7c478bd9Sstevel@tonic-gate 		*bp++ = '0' + i;
434*7c478bd9Sstevel@tonic-gate 	*bp++ = '0' + val % 10;
435*7c478bd9Sstevel@tonic-gate 	return (bp);
436*7c478bd9Sstevel@tonic-gate }
437